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