soc-cache.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
  488. }
  489. static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
  490. {
  491. struct snd_soc_lzo_ctx **lzo_blocks;
  492. unsigned int val;
  493. int i;
  494. int ret;
  495. lzo_blocks = codec->reg_cache;
  496. for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
  497. WARN_ON(!snd_soc_codec_writable_register(codec, i));
  498. ret = snd_soc_cache_read(codec, i, &val);
  499. if (ret)
  500. return ret;
  501. codec->cache_bypass = 1;
  502. ret = snd_soc_write(codec, i, val);
  503. codec->cache_bypass = 0;
  504. if (ret)
  505. return ret;
  506. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  507. i, val);
  508. }
  509. return 0;
  510. }
  511. static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
  512. unsigned int reg, unsigned int value)
  513. {
  514. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  515. int ret, blkindex, blkpos;
  516. size_t blksize, tmp_dst_len;
  517. void *tmp_dst;
  518. /* index of the compressed lzo block */
  519. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  520. /* register index within the decompressed block */
  521. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  522. /* size of the compressed block */
  523. blksize = snd_soc_lzo_get_blksize(codec);
  524. lzo_blocks = codec->reg_cache;
  525. lzo_block = lzo_blocks[blkindex];
  526. /* save the pointer and length of the compressed block */
  527. tmp_dst = lzo_block->dst;
  528. tmp_dst_len = lzo_block->dst_len;
  529. /* prepare the source to be the compressed block */
  530. lzo_block->src = lzo_block->dst;
  531. lzo_block->src_len = lzo_block->dst_len;
  532. /* decompress the block */
  533. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  534. if (ret < 0) {
  535. kfree(lzo_block->dst);
  536. goto out;
  537. }
  538. /* write the new value to the cache */
  539. if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
  540. codec->driver->reg_word_size)) {
  541. kfree(lzo_block->dst);
  542. goto out;
  543. }
  544. /* prepare the source to be the decompressed block */
  545. lzo_block->src = lzo_block->dst;
  546. lzo_block->src_len = lzo_block->dst_len;
  547. /* compress the block */
  548. ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
  549. if (ret < 0) {
  550. kfree(lzo_block->dst);
  551. kfree(lzo_block->src);
  552. goto out;
  553. }
  554. /* set the bit so we know we have to sync this register */
  555. set_bit(reg, lzo_block->sync_bmp);
  556. kfree(tmp_dst);
  557. kfree(lzo_block->src);
  558. return 0;
  559. out:
  560. lzo_block->dst = tmp_dst;
  561. lzo_block->dst_len = tmp_dst_len;
  562. return ret;
  563. }
  564. static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
  565. unsigned int reg, unsigned int *value)
  566. {
  567. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  568. int ret, blkindex, blkpos;
  569. size_t blksize, tmp_dst_len;
  570. void *tmp_dst;
  571. *value = 0;
  572. /* index of the compressed lzo block */
  573. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  574. /* register index within the decompressed block */
  575. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  576. /* size of the compressed block */
  577. blksize = snd_soc_lzo_get_blksize(codec);
  578. lzo_blocks = codec->reg_cache;
  579. lzo_block = lzo_blocks[blkindex];
  580. /* save the pointer and length of the compressed block */
  581. tmp_dst = lzo_block->dst;
  582. tmp_dst_len = lzo_block->dst_len;
  583. /* prepare the source to be the compressed block */
  584. lzo_block->src = lzo_block->dst;
  585. lzo_block->src_len = lzo_block->dst_len;
  586. /* decompress the block */
  587. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  588. if (ret >= 0)
  589. /* fetch the value from the cache */
  590. *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
  591. codec->driver->reg_word_size);
  592. kfree(lzo_block->dst);
  593. /* restore the pointer and length of the compressed block */
  594. lzo_block->dst = tmp_dst;
  595. lzo_block->dst_len = tmp_dst_len;
  596. return 0;
  597. }
  598. static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
  599. {
  600. struct snd_soc_lzo_ctx **lzo_blocks;
  601. int i, blkcount;
  602. lzo_blocks = codec->reg_cache;
  603. if (!lzo_blocks)
  604. return 0;
  605. blkcount = snd_soc_lzo_block_count();
  606. /*
  607. * the pointer to the bitmap used for syncing the cache
  608. * is shared amongst all lzo_blocks. Ensure it is freed
  609. * only once.
  610. */
  611. if (lzo_blocks[0])
  612. kfree(lzo_blocks[0]->sync_bmp);
  613. for (i = 0; i < blkcount; ++i) {
  614. if (lzo_blocks[i]) {
  615. kfree(lzo_blocks[i]->wmem);
  616. kfree(lzo_blocks[i]->dst);
  617. }
  618. /* each lzo_block is a pointer returned by kmalloc or NULL */
  619. kfree(lzo_blocks[i]);
  620. }
  621. kfree(lzo_blocks);
  622. codec->reg_cache = NULL;
  623. return 0;
  624. }
  625. static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
  626. {
  627. struct snd_soc_lzo_ctx **lzo_blocks;
  628. size_t bmp_size;
  629. const struct snd_soc_codec_driver *codec_drv;
  630. int ret, tofree, i, blksize, blkcount;
  631. const char *p, *end;
  632. unsigned long *sync_bmp;
  633. ret = 0;
  634. codec_drv = codec->driver;
  635. /*
  636. * If we have not been given a default register cache
  637. * then allocate a dummy zero-ed out region, compress it
  638. * and remember to free it afterwards.
  639. */
  640. tofree = 0;
  641. if (!codec->reg_def_copy)
  642. tofree = 1;
  643. if (!codec->reg_def_copy) {
  644. codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
  645. if (!codec->reg_def_copy)
  646. return -ENOMEM;
  647. }
  648. blkcount = snd_soc_lzo_block_count();
  649. codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
  650. GFP_KERNEL);
  651. if (!codec->reg_cache) {
  652. ret = -ENOMEM;
  653. goto err_tofree;
  654. }
  655. lzo_blocks = codec->reg_cache;
  656. /*
  657. * allocate a bitmap to be used when syncing the cache with
  658. * the hardware. Each time a register is modified, the corresponding
  659. * bit is set in the bitmap, so we know that we have to sync
  660. * that register.
  661. */
  662. bmp_size = codec_drv->reg_cache_size;
  663. sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
  664. GFP_KERNEL);
  665. if (!sync_bmp) {
  666. ret = -ENOMEM;
  667. goto err;
  668. }
  669. bitmap_zero(sync_bmp, bmp_size);
  670. /* allocate the lzo blocks and initialize them */
  671. for (i = 0; i < blkcount; ++i) {
  672. lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
  673. GFP_KERNEL);
  674. if (!lzo_blocks[i]) {
  675. kfree(sync_bmp);
  676. ret = -ENOMEM;
  677. goto err;
  678. }
  679. lzo_blocks[i]->sync_bmp = sync_bmp;
  680. lzo_blocks[i]->sync_bmp_nbits = bmp_size;
  681. /* alloc the working space for the compressed block */
  682. ret = snd_soc_lzo_prepare(lzo_blocks[i]);
  683. if (ret < 0)
  684. goto err;
  685. }
  686. blksize = snd_soc_lzo_get_blksize(codec);
  687. p = codec->reg_def_copy;
  688. end = codec->reg_def_copy + codec->reg_size;
  689. /* compress the register map and fill the lzo blocks */
  690. for (i = 0; i < blkcount; ++i, p += blksize) {
  691. lzo_blocks[i]->src = p;
  692. if (p + blksize > end)
  693. lzo_blocks[i]->src_len = end - p;
  694. else
  695. lzo_blocks[i]->src_len = blksize;
  696. ret = snd_soc_lzo_compress_cache_block(codec,
  697. lzo_blocks[i]);
  698. if (ret < 0)
  699. goto err;
  700. lzo_blocks[i]->decompressed_size =
  701. lzo_blocks[i]->src_len;
  702. }
  703. if (tofree) {
  704. kfree(codec->reg_def_copy);
  705. codec->reg_def_copy = NULL;
  706. }
  707. return 0;
  708. err:
  709. snd_soc_cache_exit(codec);
  710. err_tofree:
  711. if (tofree) {
  712. kfree(codec->reg_def_copy);
  713. codec->reg_def_copy = NULL;
  714. }
  715. return ret;
  716. }
  717. #endif
  718. static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
  719. {
  720. int i;
  721. int ret;
  722. const struct snd_soc_codec_driver *codec_drv;
  723. unsigned int val;
  724. codec_drv = codec->driver;
  725. for (i = 0; i < codec_drv->reg_cache_size; ++i) {
  726. ret = snd_soc_cache_read(codec, i, &val);
  727. if (ret)
  728. return ret;
  729. if (codec->reg_def_copy)
  730. if (snd_soc_get_cache_val(codec->reg_def_copy,
  731. i, codec_drv->reg_word_size) == val)
  732. continue;
  733. WARN_ON(!snd_soc_codec_writable_register(codec, i));
  734. ret = snd_soc_write(codec, i, val);
  735. if (ret)
  736. return ret;
  737. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  738. i, val);
  739. }
  740. return 0;
  741. }
  742. static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
  743. unsigned int reg, unsigned int value)
  744. {
  745. snd_soc_set_cache_val(codec->reg_cache, reg, value,
  746. codec->driver->reg_word_size);
  747. return 0;
  748. }
  749. static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
  750. unsigned int reg, unsigned int *value)
  751. {
  752. *value = snd_soc_get_cache_val(codec->reg_cache, reg,
  753. codec->driver->reg_word_size);
  754. return 0;
  755. }
  756. static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
  757. {
  758. if (!codec->reg_cache)
  759. return 0;
  760. kfree(codec->reg_cache);
  761. codec->reg_cache = NULL;
  762. return 0;
  763. }
  764. static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
  765. {
  766. if (codec->reg_def_copy)
  767. codec->reg_cache = kmemdup(codec->reg_def_copy,
  768. codec->reg_size, GFP_KERNEL);
  769. else
  770. codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
  771. if (!codec->reg_cache)
  772. return -ENOMEM;
  773. return 0;
  774. }
  775. /* an array of all supported compression types */
  776. static const struct snd_soc_cache_ops cache_types[] = {
  777. /* Flat *must* be the first entry for fallback */
  778. {
  779. .id = SND_SOC_FLAT_COMPRESSION,
  780. .name = "flat",
  781. .init = snd_soc_flat_cache_init,
  782. .exit = snd_soc_flat_cache_exit,
  783. .read = snd_soc_flat_cache_read,
  784. .write = snd_soc_flat_cache_write,
  785. .sync = snd_soc_flat_cache_sync
  786. },
  787. #ifdef CONFIG_SND_SOC_CACHE_LZO
  788. {
  789. .id = SND_SOC_LZO_COMPRESSION,
  790. .name = "LZO",
  791. .init = snd_soc_lzo_cache_init,
  792. .exit = snd_soc_lzo_cache_exit,
  793. .read = snd_soc_lzo_cache_read,
  794. .write = snd_soc_lzo_cache_write,
  795. .sync = snd_soc_lzo_cache_sync
  796. },
  797. #endif
  798. {
  799. .id = SND_SOC_RBTREE_COMPRESSION,
  800. .name = "rbtree",
  801. .init = snd_soc_rbtree_cache_init,
  802. .exit = snd_soc_rbtree_cache_exit,
  803. .read = snd_soc_rbtree_cache_read,
  804. .write = snd_soc_rbtree_cache_write,
  805. .sync = snd_soc_rbtree_cache_sync
  806. }
  807. };
  808. int snd_soc_cache_init(struct snd_soc_codec *codec)
  809. {
  810. int i;
  811. for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
  812. if (cache_types[i].id == codec->compress_type)
  813. break;
  814. /* Fall back to flat compression */
  815. if (i == ARRAY_SIZE(cache_types)) {
  816. dev_warn(codec->dev, "Could not match compress type: %d\n",
  817. codec->compress_type);
  818. i = 0;
  819. }
  820. mutex_init(&codec->cache_rw_mutex);
  821. codec->cache_ops = &cache_types[i];
  822. if (codec->cache_ops->init) {
  823. if (codec->cache_ops->name)
  824. dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
  825. codec->cache_ops->name, codec->name);
  826. return codec->cache_ops->init(codec);
  827. }
  828. return -ENOSYS;
  829. }
  830. /*
  831. * NOTE: keep in mind that this function might be called
  832. * multiple times.
  833. */
  834. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  835. {
  836. if (codec->cache_ops && codec->cache_ops->exit) {
  837. if (codec->cache_ops->name)
  838. dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
  839. codec->cache_ops->name, codec->name);
  840. return codec->cache_ops->exit(codec);
  841. }
  842. return -ENOSYS;
  843. }
  844. /**
  845. * snd_soc_cache_read: Fetch the value of a given register from the cache.
  846. *
  847. * @codec: CODEC to configure.
  848. * @reg: The register index.
  849. * @value: The value to be returned.
  850. */
  851. int snd_soc_cache_read(struct snd_soc_codec *codec,
  852. unsigned int reg, unsigned int *value)
  853. {
  854. int ret;
  855. mutex_lock(&codec->cache_rw_mutex);
  856. if (value && codec->cache_ops && codec->cache_ops->read) {
  857. ret = codec->cache_ops->read(codec, reg, value);
  858. mutex_unlock(&codec->cache_rw_mutex);
  859. return ret;
  860. }
  861. mutex_unlock(&codec->cache_rw_mutex);
  862. return -ENOSYS;
  863. }
  864. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  865. /**
  866. * snd_soc_cache_write: Set the value of a given register in the cache.
  867. *
  868. * @codec: CODEC to configure.
  869. * @reg: The register index.
  870. * @value: The new register value.
  871. */
  872. int snd_soc_cache_write(struct snd_soc_codec *codec,
  873. unsigned int reg, unsigned int value)
  874. {
  875. int ret;
  876. mutex_lock(&codec->cache_rw_mutex);
  877. if (codec->cache_ops && codec->cache_ops->write) {
  878. ret = codec->cache_ops->write(codec, reg, value);
  879. mutex_unlock(&codec->cache_rw_mutex);
  880. return ret;
  881. }
  882. mutex_unlock(&codec->cache_rw_mutex);
  883. return -ENOSYS;
  884. }
  885. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  886. /**
  887. * snd_soc_cache_sync: Sync the register cache with the hardware.
  888. *
  889. * @codec: CODEC to configure.
  890. *
  891. * Any registers that should not be synced should be marked as
  892. * volatile. In general drivers can choose not to use the provided
  893. * syncing functionality if they so require.
  894. */
  895. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  896. {
  897. int ret;
  898. const char *name;
  899. if (!codec->cache_sync) {
  900. return 0;
  901. }
  902. if (!codec->cache_ops || !codec->cache_ops->sync)
  903. return -ENOSYS;
  904. if (codec->cache_ops->name)
  905. name = codec->cache_ops->name;
  906. else
  907. name = "unknown";
  908. if (codec->cache_ops->name)
  909. dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
  910. codec->cache_ops->name, codec->name);
  911. trace_snd_soc_cache_sync(codec, name, "start");
  912. ret = codec->cache_ops->sync(codec);
  913. if (!ret)
  914. codec->cache_sync = 0;
  915. trace_snd_soc_cache_sync(codec, name, "end");
  916. return ret;
  917. }
  918. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
  919. static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
  920. unsigned int reg)
  921. {
  922. const struct snd_soc_codec_driver *codec_drv;
  923. unsigned int min, max, index;
  924. codec_drv = codec->driver;
  925. min = 0;
  926. max = codec_drv->reg_access_size - 1;
  927. do {
  928. index = (min + max) / 2;
  929. if (codec_drv->reg_access_default[index].reg == reg)
  930. return index;
  931. if (codec_drv->reg_access_default[index].reg < reg)
  932. min = index + 1;
  933. else
  934. max = index;
  935. } while (min <= max);
  936. return -1;
  937. }
  938. int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
  939. unsigned int reg)
  940. {
  941. int index;
  942. if (reg >= codec->driver->reg_cache_size)
  943. return 1;
  944. index = snd_soc_get_reg_access_index(codec, reg);
  945. if (index < 0)
  946. return 0;
  947. return codec->driver->reg_access_default[index].vol;
  948. }
  949. EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
  950. int snd_soc_default_readable_register(struct snd_soc_codec *codec,
  951. unsigned int reg)
  952. {
  953. int index;
  954. if (reg >= codec->driver->reg_cache_size)
  955. return 1;
  956. index = snd_soc_get_reg_access_index(codec, reg);
  957. if (index < 0)
  958. return 0;
  959. return codec->driver->reg_access_default[index].read;
  960. }
  961. EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
  962. int snd_soc_default_writable_register(struct snd_soc_codec *codec,
  963. unsigned int reg)
  964. {
  965. int index;
  966. if (reg >= codec->driver->reg_cache_size)
  967. return 1;
  968. index = snd_soc_get_reg_access_index(codec, reg);
  969. if (index < 0)
  970. return 0;
  971. return codec->driver->reg_access_default[index].write;
  972. }
  973. EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);