soc-cache.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. if (codec->reg_def_copy)
  772. codec->reg_cache = kmemdup(codec->reg_def_copy,
  773. codec->reg_size, GFP_KERNEL);
  774. else
  775. codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
  776. if (!codec->reg_cache)
  777. return -ENOMEM;
  778. return 0;
  779. }
  780. /* an array of all supported compression types */
  781. static const struct snd_soc_cache_ops cache_types[] = {
  782. /* Flat *must* be the first entry for fallback */
  783. {
  784. .id = SND_SOC_FLAT_COMPRESSION,
  785. .name = "flat",
  786. .init = snd_soc_flat_cache_init,
  787. .exit = snd_soc_flat_cache_exit,
  788. .read = snd_soc_flat_cache_read,
  789. .write = snd_soc_flat_cache_write,
  790. .sync = snd_soc_flat_cache_sync
  791. },
  792. #ifdef CONFIG_SND_SOC_CACHE_LZO
  793. {
  794. .id = SND_SOC_LZO_COMPRESSION,
  795. .name = "LZO",
  796. .init = snd_soc_lzo_cache_init,
  797. .exit = snd_soc_lzo_cache_exit,
  798. .read = snd_soc_lzo_cache_read,
  799. .write = snd_soc_lzo_cache_write,
  800. .sync = snd_soc_lzo_cache_sync
  801. },
  802. #endif
  803. {
  804. .id = SND_SOC_RBTREE_COMPRESSION,
  805. .name = "rbtree",
  806. .init = snd_soc_rbtree_cache_init,
  807. .exit = snd_soc_rbtree_cache_exit,
  808. .read = snd_soc_rbtree_cache_read,
  809. .write = snd_soc_rbtree_cache_write,
  810. .sync = snd_soc_rbtree_cache_sync
  811. }
  812. };
  813. int snd_soc_cache_init(struct snd_soc_codec *codec)
  814. {
  815. int i;
  816. for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
  817. if (cache_types[i].id == codec->compress_type)
  818. break;
  819. /* Fall back to flat compression */
  820. if (i == ARRAY_SIZE(cache_types)) {
  821. dev_warn(codec->dev, "Could not match compress type: %d\n",
  822. codec->compress_type);
  823. i = 0;
  824. }
  825. mutex_init(&codec->cache_rw_mutex);
  826. codec->cache_ops = &cache_types[i];
  827. if (codec->cache_ops->init) {
  828. if (codec->cache_ops->name)
  829. dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
  830. codec->cache_ops->name, codec->name);
  831. return codec->cache_ops->init(codec);
  832. }
  833. return -ENOSYS;
  834. }
  835. /*
  836. * NOTE: keep in mind that this function might be called
  837. * multiple times.
  838. */
  839. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  840. {
  841. if (codec->cache_ops && codec->cache_ops->exit) {
  842. if (codec->cache_ops->name)
  843. dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
  844. codec->cache_ops->name, codec->name);
  845. return codec->cache_ops->exit(codec);
  846. }
  847. return -ENOSYS;
  848. }
  849. /**
  850. * snd_soc_cache_read: Fetch the value of a given register from the cache.
  851. *
  852. * @codec: CODEC to configure.
  853. * @reg: The register index.
  854. * @value: The value to be returned.
  855. */
  856. int snd_soc_cache_read(struct snd_soc_codec *codec,
  857. unsigned int reg, unsigned int *value)
  858. {
  859. int ret;
  860. mutex_lock(&codec->cache_rw_mutex);
  861. if (value && codec->cache_ops && codec->cache_ops->read) {
  862. ret = codec->cache_ops->read(codec, reg, value);
  863. mutex_unlock(&codec->cache_rw_mutex);
  864. return ret;
  865. }
  866. mutex_unlock(&codec->cache_rw_mutex);
  867. return -ENOSYS;
  868. }
  869. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  870. /**
  871. * snd_soc_cache_write: Set the value of a given register in the cache.
  872. *
  873. * @codec: CODEC to configure.
  874. * @reg: The register index.
  875. * @value: The new register value.
  876. */
  877. int snd_soc_cache_write(struct snd_soc_codec *codec,
  878. unsigned int reg, unsigned int value)
  879. {
  880. int ret;
  881. mutex_lock(&codec->cache_rw_mutex);
  882. if (codec->cache_ops && codec->cache_ops->write) {
  883. ret = codec->cache_ops->write(codec, reg, value);
  884. mutex_unlock(&codec->cache_rw_mutex);
  885. return ret;
  886. }
  887. mutex_unlock(&codec->cache_rw_mutex);
  888. return -ENOSYS;
  889. }
  890. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  891. /**
  892. * snd_soc_cache_sync: Sync the register cache with the hardware.
  893. *
  894. * @codec: CODEC to configure.
  895. *
  896. * Any registers that should not be synced should be marked as
  897. * volatile. In general drivers can choose not to use the provided
  898. * syncing functionality if they so require.
  899. */
  900. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  901. {
  902. int ret;
  903. const char *name;
  904. if (!codec->cache_sync) {
  905. return 0;
  906. }
  907. if (!codec->cache_ops || !codec->cache_ops->sync)
  908. return -ENOSYS;
  909. if (codec->cache_ops->name)
  910. name = codec->cache_ops->name;
  911. else
  912. name = "unknown";
  913. if (codec->cache_ops->name)
  914. dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
  915. codec->cache_ops->name, codec->name);
  916. trace_snd_soc_cache_sync(codec, name, "start");
  917. ret = codec->cache_ops->sync(codec);
  918. if (!ret)
  919. codec->cache_sync = 0;
  920. trace_snd_soc_cache_sync(codec, name, "end");
  921. return ret;
  922. }
  923. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
  924. static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
  925. unsigned int reg)
  926. {
  927. const struct snd_soc_codec_driver *codec_drv;
  928. unsigned int min, max, index;
  929. codec_drv = codec->driver;
  930. min = 0;
  931. max = codec_drv->reg_access_size - 1;
  932. do {
  933. index = (min + max) / 2;
  934. if (codec_drv->reg_access_default[index].reg == reg)
  935. return index;
  936. if (codec_drv->reg_access_default[index].reg < reg)
  937. min = index + 1;
  938. else
  939. max = index;
  940. } while (min <= max);
  941. return -1;
  942. }
  943. int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
  944. unsigned int reg)
  945. {
  946. int index;
  947. if (reg >= codec->driver->reg_cache_size)
  948. return 1;
  949. index = snd_soc_get_reg_access_index(codec, reg);
  950. if (index < 0)
  951. return 0;
  952. return codec->driver->reg_access_default[index].vol;
  953. }
  954. EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
  955. int snd_soc_default_readable_register(struct snd_soc_codec *codec,
  956. unsigned int reg)
  957. {
  958. int index;
  959. if (reg >= codec->driver->reg_cache_size)
  960. return 1;
  961. index = snd_soc_get_reg_access_index(codec, reg);
  962. if (index < 0)
  963. return 0;
  964. return codec->driver->reg_access_default[index].read;
  965. }
  966. EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
  967. int snd_soc_default_writable_register(struct snd_soc_codec *codec,
  968. unsigned int reg)
  969. {
  970. int index;
  971. if (reg >= codec->driver->reg_cache_size)
  972. return 1;
  973. index = snd_soc_get_reg_access_index(codec, reg);
  974. if (index < 0)
  975. return 0;
  976. return codec->driver->reg_access_default[index].write;
  977. }
  978. EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);