soc-cache.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 <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. struct snd_soc_rbtree_node {
  64. struct rb_node node; /* the actual rbtree node holding this block */
  65. unsigned int base_reg; /* base register handled by this block */
  66. unsigned int word_size; /* number of bytes needed to represent the register index */
  67. void *block; /* block of adjacent registers */
  68. unsigned int blklen; /* number of registers available in the block */
  69. } __attribute__ ((packed));
  70. struct snd_soc_rbtree_ctx {
  71. struct rb_root root;
  72. struct snd_soc_rbtree_node *cached_rbnode;
  73. };
  74. static inline void snd_soc_rbtree_get_base_top_reg(
  75. struct snd_soc_rbtree_node *rbnode,
  76. unsigned int *base, unsigned int *top)
  77. {
  78. *base = rbnode->base_reg;
  79. *top = rbnode->base_reg + rbnode->blklen - 1;
  80. }
  81. static unsigned int snd_soc_rbtree_get_register(
  82. struct snd_soc_rbtree_node *rbnode, unsigned int idx)
  83. {
  84. unsigned int val;
  85. switch (rbnode->word_size) {
  86. case 1: {
  87. u8 *p = rbnode->block;
  88. val = p[idx];
  89. return val;
  90. }
  91. case 2: {
  92. u16 *p = rbnode->block;
  93. val = p[idx];
  94. return val;
  95. }
  96. default:
  97. BUG();
  98. break;
  99. }
  100. return -1;
  101. }
  102. static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode,
  103. unsigned int idx, unsigned int val)
  104. {
  105. switch (rbnode->word_size) {
  106. case 1: {
  107. u8 *p = rbnode->block;
  108. p[idx] = val;
  109. break;
  110. }
  111. case 2: {
  112. u16 *p = rbnode->block;
  113. p[idx] = val;
  114. break;
  115. }
  116. default:
  117. BUG();
  118. break;
  119. }
  120. }
  121. static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
  122. struct rb_root *root, unsigned int reg)
  123. {
  124. struct rb_node *node;
  125. struct snd_soc_rbtree_node *rbnode;
  126. unsigned int base_reg, top_reg;
  127. node = root->rb_node;
  128. while (node) {
  129. rbnode = container_of(node, struct snd_soc_rbtree_node, node);
  130. snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
  131. if (reg >= base_reg && reg <= top_reg)
  132. return rbnode;
  133. else if (reg > top_reg)
  134. node = node->rb_right;
  135. else if (reg < base_reg)
  136. node = node->rb_left;
  137. }
  138. return NULL;
  139. }
  140. static int snd_soc_rbtree_insert(struct rb_root *root,
  141. struct snd_soc_rbtree_node *rbnode)
  142. {
  143. struct rb_node **new, *parent;
  144. struct snd_soc_rbtree_node *rbnode_tmp;
  145. unsigned int base_reg_tmp, top_reg_tmp;
  146. unsigned int base_reg;
  147. parent = NULL;
  148. new = &root->rb_node;
  149. while (*new) {
  150. rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
  151. node);
  152. /* base and top registers of the current rbnode */
  153. snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
  154. &top_reg_tmp);
  155. /* base register of the rbnode to be added */
  156. base_reg = rbnode->base_reg;
  157. parent = *new;
  158. /* if this register has already been inserted, just return */
  159. if (base_reg >= base_reg_tmp &&
  160. base_reg <= top_reg_tmp)
  161. return 0;
  162. else if (base_reg > top_reg_tmp)
  163. new = &((*new)->rb_right);
  164. else if (base_reg < base_reg_tmp)
  165. new = &((*new)->rb_left);
  166. }
  167. /* insert the node into the rbtree */
  168. rb_link_node(&rbnode->node, parent, new);
  169. rb_insert_color(&rbnode->node, root);
  170. return 1;
  171. }
  172. static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
  173. {
  174. struct snd_soc_rbtree_ctx *rbtree_ctx;
  175. struct rb_node *node;
  176. struct snd_soc_rbtree_node *rbnode;
  177. unsigned int regtmp;
  178. unsigned int val, def;
  179. int ret;
  180. int i;
  181. rbtree_ctx = codec->reg_cache;
  182. for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
  183. rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
  184. for (i = 0; i < rbnode->blklen; ++i) {
  185. regtmp = rbnode->base_reg + i;
  186. WARN_ON(codec->writable_register &&
  187. codec->writable_register(codec, regtmp));
  188. val = snd_soc_rbtree_get_register(rbnode, i);
  189. def = snd_soc_get_cache_val(codec->reg_def_copy, i,
  190. rbnode->word_size);
  191. if (val == def)
  192. continue;
  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. const struct snd_soc_codec_driver *codec_drv;
  489. codec_drv = codec->driver;
  490. return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
  491. }
  492. static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
  493. {
  494. struct snd_soc_lzo_ctx **lzo_blocks;
  495. unsigned int val;
  496. int i;
  497. int ret;
  498. lzo_blocks = codec->reg_cache;
  499. for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
  500. WARN_ON(codec->writable_register &&
  501. codec->writable_register(codec, i));
  502. ret = snd_soc_cache_read(codec, i, &val);
  503. if (ret)
  504. return ret;
  505. codec->cache_bypass = 1;
  506. ret = snd_soc_write(codec, i, val);
  507. codec->cache_bypass = 0;
  508. if (ret)
  509. return ret;
  510. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  511. i, val);
  512. }
  513. return 0;
  514. }
  515. static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
  516. unsigned int reg, unsigned int value)
  517. {
  518. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  519. int ret, blkindex, blkpos;
  520. size_t blksize, tmp_dst_len;
  521. void *tmp_dst;
  522. /* index of the compressed lzo block */
  523. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  524. /* register index within the decompressed block */
  525. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  526. /* size of the compressed block */
  527. blksize = snd_soc_lzo_get_blksize(codec);
  528. lzo_blocks = codec->reg_cache;
  529. lzo_block = lzo_blocks[blkindex];
  530. /* save the pointer and length of the compressed block */
  531. tmp_dst = lzo_block->dst;
  532. tmp_dst_len = lzo_block->dst_len;
  533. /* prepare the source to be the compressed block */
  534. lzo_block->src = lzo_block->dst;
  535. lzo_block->src_len = lzo_block->dst_len;
  536. /* decompress the block */
  537. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  538. if (ret < 0) {
  539. kfree(lzo_block->dst);
  540. goto out;
  541. }
  542. /* write the new value to the cache */
  543. if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
  544. codec->driver->reg_word_size)) {
  545. kfree(lzo_block->dst);
  546. goto out;
  547. }
  548. /* prepare the source to be the decompressed block */
  549. lzo_block->src = lzo_block->dst;
  550. lzo_block->src_len = lzo_block->dst_len;
  551. /* compress the block */
  552. ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
  553. if (ret < 0) {
  554. kfree(lzo_block->dst);
  555. kfree(lzo_block->src);
  556. goto out;
  557. }
  558. /* set the bit so we know we have to sync this register */
  559. set_bit(reg, lzo_block->sync_bmp);
  560. kfree(tmp_dst);
  561. kfree(lzo_block->src);
  562. return 0;
  563. out:
  564. lzo_block->dst = tmp_dst;
  565. lzo_block->dst_len = tmp_dst_len;
  566. return ret;
  567. }
  568. static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
  569. unsigned int reg, unsigned int *value)
  570. {
  571. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  572. int ret, blkindex, blkpos;
  573. size_t blksize, tmp_dst_len;
  574. void *tmp_dst;
  575. *value = 0;
  576. /* index of the compressed lzo block */
  577. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  578. /* register index within the decompressed block */
  579. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  580. /* size of the compressed block */
  581. blksize = snd_soc_lzo_get_blksize(codec);
  582. lzo_blocks = codec->reg_cache;
  583. lzo_block = lzo_blocks[blkindex];
  584. /* save the pointer and length of the compressed block */
  585. tmp_dst = lzo_block->dst;
  586. tmp_dst_len = lzo_block->dst_len;
  587. /* prepare the source to be the compressed block */
  588. lzo_block->src = lzo_block->dst;
  589. lzo_block->src_len = lzo_block->dst_len;
  590. /* decompress the block */
  591. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  592. if (ret >= 0)
  593. /* fetch the value from the cache */
  594. *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
  595. codec->driver->reg_word_size);
  596. kfree(lzo_block->dst);
  597. /* restore the pointer and length of the compressed block */
  598. lzo_block->dst = tmp_dst;
  599. lzo_block->dst_len = tmp_dst_len;
  600. return 0;
  601. }
  602. static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
  603. {
  604. struct snd_soc_lzo_ctx **lzo_blocks;
  605. int i, blkcount;
  606. lzo_blocks = codec->reg_cache;
  607. if (!lzo_blocks)
  608. return 0;
  609. blkcount = snd_soc_lzo_block_count();
  610. /*
  611. * the pointer to the bitmap used for syncing the cache
  612. * is shared amongst all lzo_blocks. Ensure it is freed
  613. * only once.
  614. */
  615. if (lzo_blocks[0])
  616. kfree(lzo_blocks[0]->sync_bmp);
  617. for (i = 0; i < blkcount; ++i) {
  618. if (lzo_blocks[i]) {
  619. kfree(lzo_blocks[i]->wmem);
  620. kfree(lzo_blocks[i]->dst);
  621. }
  622. /* each lzo_block is a pointer returned by kmalloc or NULL */
  623. kfree(lzo_blocks[i]);
  624. }
  625. kfree(lzo_blocks);
  626. codec->reg_cache = NULL;
  627. return 0;
  628. }
  629. static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
  630. {
  631. struct snd_soc_lzo_ctx **lzo_blocks;
  632. size_t bmp_size;
  633. const struct snd_soc_codec_driver *codec_drv;
  634. int ret, tofree, i, blksize, blkcount;
  635. const char *p, *end;
  636. unsigned long *sync_bmp;
  637. ret = 0;
  638. codec_drv = codec->driver;
  639. /*
  640. * If we have not been given a default register cache
  641. * then allocate a dummy zero-ed out region, compress it
  642. * and remember to free it afterwards.
  643. */
  644. tofree = 0;
  645. if (!codec->reg_def_copy)
  646. tofree = 1;
  647. if (!codec->reg_def_copy) {
  648. codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
  649. if (!codec->reg_def_copy)
  650. return -ENOMEM;
  651. }
  652. blkcount = snd_soc_lzo_block_count();
  653. codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
  654. GFP_KERNEL);
  655. if (!codec->reg_cache) {
  656. ret = -ENOMEM;
  657. goto err_tofree;
  658. }
  659. lzo_blocks = codec->reg_cache;
  660. /*
  661. * allocate a bitmap to be used when syncing the cache with
  662. * the hardware. Each time a register is modified, the corresponding
  663. * bit is set in the bitmap, so we know that we have to sync
  664. * that register.
  665. */
  666. bmp_size = codec_drv->reg_cache_size;
  667. sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
  668. GFP_KERNEL);
  669. if (!sync_bmp) {
  670. ret = -ENOMEM;
  671. goto err;
  672. }
  673. bitmap_zero(sync_bmp, bmp_size);
  674. /* allocate the lzo blocks and initialize them */
  675. for (i = 0; i < blkcount; ++i) {
  676. lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
  677. GFP_KERNEL);
  678. if (!lzo_blocks[i]) {
  679. kfree(sync_bmp);
  680. ret = -ENOMEM;
  681. goto err;
  682. }
  683. lzo_blocks[i]->sync_bmp = sync_bmp;
  684. lzo_blocks[i]->sync_bmp_nbits = bmp_size;
  685. /* alloc the working space for the compressed block */
  686. ret = snd_soc_lzo_prepare(lzo_blocks[i]);
  687. if (ret < 0)
  688. goto err;
  689. }
  690. blksize = snd_soc_lzo_get_blksize(codec);
  691. p = codec->reg_def_copy;
  692. end = codec->reg_def_copy + codec->reg_size;
  693. /* compress the register map and fill the lzo blocks */
  694. for (i = 0; i < blkcount; ++i, p += blksize) {
  695. lzo_blocks[i]->src = p;
  696. if (p + blksize > end)
  697. lzo_blocks[i]->src_len = end - p;
  698. else
  699. lzo_blocks[i]->src_len = blksize;
  700. ret = snd_soc_lzo_compress_cache_block(codec,
  701. lzo_blocks[i]);
  702. if (ret < 0)
  703. goto err;
  704. lzo_blocks[i]->decompressed_size =
  705. lzo_blocks[i]->src_len;
  706. }
  707. if (tofree) {
  708. kfree(codec->reg_def_copy);
  709. codec->reg_def_copy = NULL;
  710. }
  711. return 0;
  712. err:
  713. snd_soc_cache_exit(codec);
  714. err_tofree:
  715. if (tofree) {
  716. kfree(codec->reg_def_copy);
  717. codec->reg_def_copy = NULL;
  718. }
  719. return ret;
  720. }
  721. #endif
  722. static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
  723. {
  724. int i;
  725. int ret;
  726. const struct snd_soc_codec_driver *codec_drv;
  727. unsigned int val;
  728. codec_drv = codec->driver;
  729. for (i = 0; i < codec_drv->reg_cache_size; ++i) {
  730. WARN_ON(codec->writable_register &&
  731. codec->writable_register(codec, i));
  732. ret = snd_soc_cache_read(codec, i, &val);
  733. if (ret)
  734. return ret;
  735. if (codec->reg_def_copy)
  736. if (snd_soc_get_cache_val(codec->reg_def_copy,
  737. i, codec_drv->reg_word_size) == val)
  738. continue;
  739. ret = snd_soc_write(codec, i, val);
  740. if (ret)
  741. return ret;
  742. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  743. i, val);
  744. }
  745. return 0;
  746. }
  747. static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
  748. unsigned int reg, unsigned int value)
  749. {
  750. snd_soc_set_cache_val(codec->reg_cache, reg, value,
  751. codec->driver->reg_word_size);
  752. return 0;
  753. }
  754. static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
  755. unsigned int reg, unsigned int *value)
  756. {
  757. *value = snd_soc_get_cache_val(codec->reg_cache, reg,
  758. codec->driver->reg_word_size);
  759. return 0;
  760. }
  761. static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
  762. {
  763. if (!codec->reg_cache)
  764. return 0;
  765. kfree(codec->reg_cache);
  766. codec->reg_cache = NULL;
  767. return 0;
  768. }
  769. static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
  770. {
  771. const struct snd_soc_codec_driver *codec_drv;
  772. codec_drv = codec->driver;
  773. if (codec->reg_def_copy)
  774. codec->reg_cache = kmemdup(codec->reg_def_copy,
  775. codec->reg_size, GFP_KERNEL);
  776. else
  777. codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
  778. if (!codec->reg_cache)
  779. return -ENOMEM;
  780. return 0;
  781. }
  782. /* an array of all supported compression types */
  783. static const struct snd_soc_cache_ops cache_types[] = {
  784. /* Flat *must* be the first entry for fallback */
  785. {
  786. .id = SND_SOC_FLAT_COMPRESSION,
  787. .name = "flat",
  788. .init = snd_soc_flat_cache_init,
  789. .exit = snd_soc_flat_cache_exit,
  790. .read = snd_soc_flat_cache_read,
  791. .write = snd_soc_flat_cache_write,
  792. .sync = snd_soc_flat_cache_sync
  793. },
  794. #ifdef CONFIG_SND_SOC_CACHE_LZO
  795. {
  796. .id = SND_SOC_LZO_COMPRESSION,
  797. .name = "LZO",
  798. .init = snd_soc_lzo_cache_init,
  799. .exit = snd_soc_lzo_cache_exit,
  800. .read = snd_soc_lzo_cache_read,
  801. .write = snd_soc_lzo_cache_write,
  802. .sync = snd_soc_lzo_cache_sync
  803. },
  804. #endif
  805. {
  806. .id = SND_SOC_RBTREE_COMPRESSION,
  807. .name = "rbtree",
  808. .init = snd_soc_rbtree_cache_init,
  809. .exit = snd_soc_rbtree_cache_exit,
  810. .read = snd_soc_rbtree_cache_read,
  811. .write = snd_soc_rbtree_cache_write,
  812. .sync = snd_soc_rbtree_cache_sync
  813. }
  814. };
  815. int snd_soc_cache_init(struct snd_soc_codec *codec)
  816. {
  817. int i;
  818. for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
  819. if (cache_types[i].id == codec->compress_type)
  820. break;
  821. /* Fall back to flat compression */
  822. if (i == ARRAY_SIZE(cache_types)) {
  823. dev_warn(codec->dev, "Could not match compress type: %d\n",
  824. codec->compress_type);
  825. i = 0;
  826. }
  827. mutex_init(&codec->cache_rw_mutex);
  828. codec->cache_ops = &cache_types[i];
  829. if (codec->cache_ops->init) {
  830. if (codec->cache_ops->name)
  831. dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
  832. codec->cache_ops->name, codec->name);
  833. return codec->cache_ops->init(codec);
  834. }
  835. return -ENOSYS;
  836. }
  837. /*
  838. * NOTE: keep in mind that this function might be called
  839. * multiple times.
  840. */
  841. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  842. {
  843. if (codec->cache_ops && codec->cache_ops->exit) {
  844. if (codec->cache_ops->name)
  845. dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
  846. codec->cache_ops->name, codec->name);
  847. return codec->cache_ops->exit(codec);
  848. }
  849. return -ENOSYS;
  850. }
  851. /**
  852. * snd_soc_cache_read: Fetch the value of a given register from the cache.
  853. *
  854. * @codec: CODEC to configure.
  855. * @reg: The register index.
  856. * @value: The value to be returned.
  857. */
  858. int snd_soc_cache_read(struct snd_soc_codec *codec,
  859. unsigned int reg, unsigned int *value)
  860. {
  861. int ret;
  862. mutex_lock(&codec->cache_rw_mutex);
  863. if (value && codec->cache_ops && codec->cache_ops->read) {
  864. ret = codec->cache_ops->read(codec, reg, value);
  865. mutex_unlock(&codec->cache_rw_mutex);
  866. return ret;
  867. }
  868. mutex_unlock(&codec->cache_rw_mutex);
  869. return -ENOSYS;
  870. }
  871. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  872. /**
  873. * snd_soc_cache_write: Set the value of a given register in the cache.
  874. *
  875. * @codec: CODEC to configure.
  876. * @reg: The register index.
  877. * @value: The new register value.
  878. */
  879. int snd_soc_cache_write(struct snd_soc_codec *codec,
  880. unsigned int reg, unsigned int value)
  881. {
  882. int ret;
  883. mutex_lock(&codec->cache_rw_mutex);
  884. if (codec->cache_ops && codec->cache_ops->write) {
  885. ret = codec->cache_ops->write(codec, reg, value);
  886. mutex_unlock(&codec->cache_rw_mutex);
  887. return ret;
  888. }
  889. mutex_unlock(&codec->cache_rw_mutex);
  890. return -ENOSYS;
  891. }
  892. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  893. /**
  894. * snd_soc_cache_sync: Sync the register cache with the hardware.
  895. *
  896. * @codec: CODEC to configure.
  897. *
  898. * Any registers that should not be synced should be marked as
  899. * volatile. In general drivers can choose not to use the provided
  900. * syncing functionality if they so require.
  901. */
  902. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  903. {
  904. int ret;
  905. const char *name;
  906. if (!codec->cache_sync) {
  907. return 0;
  908. }
  909. if (!codec->cache_ops || !codec->cache_ops->sync)
  910. return -ENOSYS;
  911. if (codec->cache_ops->name)
  912. name = codec->cache_ops->name;
  913. else
  914. name = "unknown";
  915. if (codec->cache_ops->name)
  916. dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
  917. codec->cache_ops->name, codec->name);
  918. trace_snd_soc_cache_sync(codec, name, "start");
  919. ret = codec->cache_ops->sync(codec);
  920. if (!ret)
  921. codec->cache_sync = 0;
  922. trace_snd_soc_cache_sync(codec, name, "end");
  923. return ret;
  924. }
  925. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
  926. static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
  927. unsigned int reg)
  928. {
  929. const struct snd_soc_codec_driver *codec_drv;
  930. unsigned int min, max, index;
  931. codec_drv = codec->driver;
  932. min = 0;
  933. max = codec_drv->reg_access_size - 1;
  934. do {
  935. index = (min + max) / 2;
  936. if (codec_drv->reg_access_default[index].reg == reg)
  937. return index;
  938. if (codec_drv->reg_access_default[index].reg < reg)
  939. min = index + 1;
  940. else
  941. max = index;
  942. } while (min <= max);
  943. return -1;
  944. }
  945. int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
  946. unsigned int reg)
  947. {
  948. int index;
  949. if (reg >= codec->driver->reg_cache_size)
  950. return 1;
  951. index = snd_soc_get_reg_access_index(codec, reg);
  952. if (index < 0)
  953. return 0;
  954. return codec->driver->reg_access_default[index].vol;
  955. }
  956. EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
  957. int snd_soc_default_readable_register(struct snd_soc_codec *codec,
  958. unsigned int reg)
  959. {
  960. int index;
  961. if (reg >= codec->driver->reg_cache_size)
  962. return 1;
  963. index = snd_soc_get_reg_access_index(codec, reg);
  964. if (index < 0)
  965. return 0;
  966. return codec->driver->reg_access_default[index].read;
  967. }
  968. EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
  969. int snd_soc_default_writable_register(struct snd_soc_codec *codec,
  970. unsigned int reg)
  971. {
  972. int index;
  973. if (reg >= codec->driver->reg_cache_size)
  974. return 1;
  975. index = snd_soc_get_reg_access_index(codec, reg);
  976. if (index < 0)
  977. return 0;
  978. return codec->driver->reg_access_default[index].write;
  979. }
  980. EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);