soc-cache.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. #ifdef CONFIG_SPI_MASTER
  21. static int do_spi_write(void *control, const char *data, int len)
  22. {
  23. struct spi_device *spi = control;
  24. int ret;
  25. ret = spi_write(spi, data, len);
  26. if (ret < 0)
  27. return ret;
  28. return len;
  29. }
  30. #endif
  31. static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
  32. unsigned int value, const void *data, int len)
  33. {
  34. int ret;
  35. if (!snd_soc_codec_volatile_register(codec, reg) &&
  36. reg < codec->driver->reg_cache_size &&
  37. !codec->cache_bypass) {
  38. ret = snd_soc_cache_write(codec, reg, value);
  39. if (ret < 0)
  40. return -1;
  41. }
  42. if (codec->cache_only) {
  43. codec->cache_sync = 1;
  44. return 0;
  45. }
  46. ret = codec->hw_write(codec->control_data, data, len);
  47. if (ret == len)
  48. return 0;
  49. if (ret < 0)
  50. return ret;
  51. else
  52. return -EIO;
  53. }
  54. static unsigned int do_hw_read(struct snd_soc_codec *codec, unsigned int reg)
  55. {
  56. int ret;
  57. unsigned int val;
  58. if (reg >= codec->driver->reg_cache_size ||
  59. snd_soc_codec_volatile_register(codec, reg) ||
  60. codec->cache_bypass) {
  61. if (codec->cache_only)
  62. return -1;
  63. BUG_ON(!codec->hw_read);
  64. return codec->hw_read(codec, reg);
  65. }
  66. ret = snd_soc_cache_read(codec, reg, &val);
  67. if (ret < 0)
  68. return -1;
  69. return val;
  70. }
  71. static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
  72. unsigned int reg)
  73. {
  74. return do_hw_read(codec, reg);
  75. }
  76. static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
  77. unsigned int value)
  78. {
  79. u16 data;
  80. data = cpu_to_be16((reg << 12) | (value & 0xffffff));
  81. return do_hw_write(codec, reg, value, &data, 2);
  82. }
  83. static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
  84. unsigned int reg)
  85. {
  86. return do_hw_read(codec, reg);
  87. }
  88. static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
  89. unsigned int value)
  90. {
  91. u8 data[2];
  92. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  93. data[1] = value & 0x00ff;
  94. return do_hw_write(codec, reg, value, data, 2);
  95. }
  96. static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
  97. unsigned int value)
  98. {
  99. u8 data[2];
  100. reg &= 0xff;
  101. data[0] = reg;
  102. data[1] = value & 0xff;
  103. return do_hw_write(codec, reg, value, data, 2);
  104. }
  105. static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
  106. unsigned int reg)
  107. {
  108. return do_hw_read(codec, reg);
  109. }
  110. static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
  111. unsigned int value)
  112. {
  113. u8 data[3];
  114. data[0] = reg;
  115. data[1] = (value >> 8) & 0xff;
  116. data[2] = value & 0xff;
  117. return do_hw_write(codec, reg, value, data, 3);
  118. }
  119. static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
  120. unsigned int reg)
  121. {
  122. return do_hw_read(codec, reg);
  123. }
  124. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  125. static unsigned int do_i2c_read(struct snd_soc_codec *codec,
  126. void *reg, int reglen,
  127. void *data, int datalen)
  128. {
  129. struct i2c_msg xfer[2];
  130. int ret;
  131. struct i2c_client *client = codec->control_data;
  132. /* Write register */
  133. xfer[0].addr = client->addr;
  134. xfer[0].flags = 0;
  135. xfer[0].len = reglen;
  136. xfer[0].buf = reg;
  137. /* Read data */
  138. xfer[1].addr = client->addr;
  139. xfer[1].flags = I2C_M_RD;
  140. xfer[1].len = datalen;
  141. xfer[1].buf = data;
  142. ret = i2c_transfer(client->adapter, xfer, 2);
  143. if (ret == 2)
  144. return 0;
  145. else if (ret < 0)
  146. return ret;
  147. else
  148. return -EIO;
  149. }
  150. #endif
  151. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  152. static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
  153. unsigned int r)
  154. {
  155. u8 reg = r;
  156. u8 data;
  157. int ret;
  158. ret = do_i2c_read(codec, &reg, 1, &data, 1);
  159. if (ret < 0)
  160. return 0;
  161. return data;
  162. }
  163. #else
  164. #define snd_soc_8_8_read_i2c NULL
  165. #endif
  166. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  167. static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
  168. unsigned int r)
  169. {
  170. u8 reg = r;
  171. u16 data;
  172. int ret;
  173. ret = do_i2c_read(codec, &reg, 1, &data, 2);
  174. if (ret < 0)
  175. return 0;
  176. return (data >> 8) | ((data & 0xff) << 8);
  177. }
  178. #else
  179. #define snd_soc_8_16_read_i2c NULL
  180. #endif
  181. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  182. static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
  183. unsigned int r)
  184. {
  185. u16 reg = r;
  186. u8 data;
  187. int ret;
  188. ret = do_i2c_read(codec, &reg, 2, &data, 1);
  189. if (ret < 0)
  190. return 0;
  191. return data;
  192. }
  193. #else
  194. #define snd_soc_16_8_read_i2c NULL
  195. #endif
  196. static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
  197. unsigned int reg)
  198. {
  199. return do_hw_read(codec, reg);
  200. }
  201. static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
  202. unsigned int value)
  203. {
  204. u8 data[3];
  205. data[0] = (reg >> 8) & 0xff;
  206. data[1] = reg & 0xff;
  207. data[2] = value;
  208. return do_hw_write(codec, reg, value, data, 3);
  209. }
  210. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  211. static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
  212. unsigned int r)
  213. {
  214. u16 reg = cpu_to_be16(r);
  215. u16 data;
  216. int ret;
  217. ret = do_i2c_read(codec, &reg, 2, &data, 2);
  218. if (ret < 0)
  219. return 0;
  220. return be16_to_cpu(data);
  221. }
  222. #else
  223. #define snd_soc_16_16_read_i2c NULL
  224. #endif
  225. static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
  226. unsigned int reg)
  227. {
  228. return do_hw_read(codec, reg);
  229. }
  230. static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
  231. unsigned int value)
  232. {
  233. u8 data[4];
  234. data[0] = (reg >> 8) & 0xff;
  235. data[1] = reg & 0xff;
  236. data[2] = (value >> 8) & 0xff;
  237. data[3] = value & 0xff;
  238. return do_hw_write(codec, reg, value, data, 4);
  239. }
  240. /* Primitive bulk write support for soc-cache. The data pointed to by
  241. * `data' needs to already be in the form the hardware expects
  242. * including any leading register specific data. Any data written
  243. * through this function will not go through the cache as it only
  244. * handles writing to volatile or out of bounds registers.
  245. */
  246. static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg,
  247. const void *data, size_t len)
  248. {
  249. int ret;
  250. /* To ensure that we don't get out of sync with the cache, check
  251. * whether the base register is volatile or if we've directly asked
  252. * to bypass the cache. Out of bounds registers are considered
  253. * volatile.
  254. */
  255. if (!codec->cache_bypass
  256. && !snd_soc_codec_volatile_register(codec, reg)
  257. && reg < codec->driver->reg_cache_size)
  258. return -EINVAL;
  259. switch (codec->control_type) {
  260. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  261. case SND_SOC_I2C:
  262. ret = i2c_master_send(codec->control_data, data, len);
  263. break;
  264. #endif
  265. #if defined(CONFIG_SPI_MASTER)
  266. case SND_SOC_SPI:
  267. ret = spi_write(codec->control_data, data, len);
  268. break;
  269. #endif
  270. default:
  271. BUG();
  272. }
  273. if (ret == len)
  274. return 0;
  275. if (ret < 0)
  276. return ret;
  277. else
  278. return -EIO;
  279. }
  280. static struct {
  281. int addr_bits;
  282. int data_bits;
  283. int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
  284. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  285. unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
  286. } io_types[] = {
  287. {
  288. .addr_bits = 4, .data_bits = 12,
  289. .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
  290. },
  291. {
  292. .addr_bits = 7, .data_bits = 9,
  293. .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
  294. },
  295. {
  296. .addr_bits = 8, .data_bits = 8,
  297. .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
  298. .i2c_read = snd_soc_8_8_read_i2c,
  299. },
  300. {
  301. .addr_bits = 8, .data_bits = 16,
  302. .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
  303. .i2c_read = snd_soc_8_16_read_i2c,
  304. },
  305. {
  306. .addr_bits = 16, .data_bits = 8,
  307. .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
  308. .i2c_read = snd_soc_16_8_read_i2c,
  309. },
  310. {
  311. .addr_bits = 16, .data_bits = 16,
  312. .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
  313. .i2c_read = snd_soc_16_16_read_i2c,
  314. },
  315. };
  316. /**
  317. * snd_soc_codec_set_cache_io: Set up standard I/O functions.
  318. *
  319. * @codec: CODEC to configure.
  320. * @addr_bits: Number of bits of register address data.
  321. * @data_bits: Number of bits of data per register.
  322. * @control: Control bus used.
  323. *
  324. * Register formats are frequently shared between many I2C and SPI
  325. * devices. In order to promote code reuse the ASoC core provides
  326. * some standard implementations of CODEC read and write operations
  327. * which can be set up using this function.
  328. *
  329. * The caller is responsible for allocating and initialising the
  330. * actual cache.
  331. *
  332. * Note that at present this code cannot be used by CODECs with
  333. * volatile registers.
  334. */
  335. int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
  336. int addr_bits, int data_bits,
  337. enum snd_soc_control_type control)
  338. {
  339. int i;
  340. for (i = 0; i < ARRAY_SIZE(io_types); i++)
  341. if (io_types[i].addr_bits == addr_bits &&
  342. io_types[i].data_bits == data_bits)
  343. break;
  344. if (i == ARRAY_SIZE(io_types)) {
  345. printk(KERN_ERR
  346. "No I/O functions for %d bit address %d bit data\n",
  347. addr_bits, data_bits);
  348. return -EINVAL;
  349. }
  350. codec->write = io_types[i].write;
  351. codec->read = io_types[i].read;
  352. codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
  353. switch (control) {
  354. case SND_SOC_I2C:
  355. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  356. codec->hw_write = (hw_write_t)i2c_master_send;
  357. #endif
  358. if (io_types[i].i2c_read)
  359. codec->hw_read = io_types[i].i2c_read;
  360. codec->control_data = container_of(codec->dev,
  361. struct i2c_client,
  362. dev);
  363. break;
  364. case SND_SOC_SPI:
  365. #ifdef CONFIG_SPI_MASTER
  366. codec->hw_write = do_spi_write;
  367. #endif
  368. codec->control_data = container_of(codec->dev,
  369. struct spi_device,
  370. dev);
  371. break;
  372. }
  373. return 0;
  374. }
  375. EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
  376. static bool snd_soc_set_cache_val(void *base, unsigned int idx,
  377. unsigned int val, unsigned int word_size)
  378. {
  379. switch (word_size) {
  380. case 1: {
  381. u8 *cache = base;
  382. if (cache[idx] == val)
  383. return true;
  384. cache[idx] = val;
  385. break;
  386. }
  387. case 2: {
  388. u16 *cache = base;
  389. if (cache[idx] == val)
  390. return true;
  391. cache[idx] = val;
  392. break;
  393. }
  394. default:
  395. BUG();
  396. }
  397. return false;
  398. }
  399. static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
  400. unsigned int word_size)
  401. {
  402. if (!base)
  403. return -1;
  404. switch (word_size) {
  405. case 1: {
  406. const u8 *cache = base;
  407. return cache[idx];
  408. }
  409. case 2: {
  410. const u16 *cache = base;
  411. return cache[idx];
  412. }
  413. default:
  414. BUG();
  415. }
  416. /* unreachable */
  417. return -1;
  418. }
  419. struct snd_soc_rbtree_node {
  420. struct rb_node node;
  421. unsigned int reg;
  422. unsigned int value;
  423. unsigned int defval;
  424. } __attribute__ ((packed));
  425. struct snd_soc_rbtree_ctx {
  426. struct rb_root root;
  427. };
  428. static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
  429. struct rb_root *root, unsigned int reg)
  430. {
  431. struct rb_node *node;
  432. struct snd_soc_rbtree_node *rbnode;
  433. node = root->rb_node;
  434. while (node) {
  435. rbnode = container_of(node, struct snd_soc_rbtree_node, node);
  436. if (rbnode->reg < reg)
  437. node = node->rb_left;
  438. else if (rbnode->reg > reg)
  439. node = node->rb_right;
  440. else
  441. return rbnode;
  442. }
  443. return NULL;
  444. }
  445. static int snd_soc_rbtree_insert(struct rb_root *root,
  446. struct snd_soc_rbtree_node *rbnode)
  447. {
  448. struct rb_node **new, *parent;
  449. struct snd_soc_rbtree_node *rbnode_tmp;
  450. parent = NULL;
  451. new = &root->rb_node;
  452. while (*new) {
  453. rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
  454. node);
  455. parent = *new;
  456. if (rbnode_tmp->reg < rbnode->reg)
  457. new = &((*new)->rb_left);
  458. else if (rbnode_tmp->reg > rbnode->reg)
  459. new = &((*new)->rb_right);
  460. else
  461. return 0;
  462. }
  463. /* insert the node into the rbtree */
  464. rb_link_node(&rbnode->node, parent, new);
  465. rb_insert_color(&rbnode->node, root);
  466. return 1;
  467. }
  468. static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
  469. {
  470. struct snd_soc_rbtree_ctx *rbtree_ctx;
  471. struct rb_node *node;
  472. struct snd_soc_rbtree_node *rbnode;
  473. unsigned int val;
  474. int ret;
  475. rbtree_ctx = codec->reg_cache;
  476. for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
  477. rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
  478. if (rbnode->value == rbnode->defval)
  479. continue;
  480. WARN_ON(codec->writable_register &&
  481. codec->writable_register(codec, rbnode->reg));
  482. ret = snd_soc_cache_read(codec, rbnode->reg, &val);
  483. if (ret)
  484. return ret;
  485. codec->cache_bypass = 1;
  486. ret = snd_soc_write(codec, rbnode->reg, val);
  487. codec->cache_bypass = 0;
  488. if (ret)
  489. return ret;
  490. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  491. rbnode->reg, val);
  492. }
  493. return 0;
  494. }
  495. static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
  496. unsigned int reg, unsigned int value)
  497. {
  498. struct snd_soc_rbtree_ctx *rbtree_ctx;
  499. struct snd_soc_rbtree_node *rbnode;
  500. rbtree_ctx = codec->reg_cache;
  501. rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
  502. if (rbnode) {
  503. if (rbnode->value == value)
  504. return 0;
  505. rbnode->value = value;
  506. } else {
  507. /* bail out early, no need to create the rbnode yet */
  508. if (!value)
  509. return 0;
  510. /*
  511. * for uninitialized registers whose value is changed
  512. * from the default zero, create an rbnode and insert
  513. * it into the tree.
  514. */
  515. rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
  516. if (!rbnode)
  517. return -ENOMEM;
  518. rbnode->reg = reg;
  519. rbnode->value = value;
  520. snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
  521. }
  522. return 0;
  523. }
  524. static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
  525. unsigned int reg, unsigned int *value)
  526. {
  527. struct snd_soc_rbtree_ctx *rbtree_ctx;
  528. struct snd_soc_rbtree_node *rbnode;
  529. rbtree_ctx = codec->reg_cache;
  530. rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
  531. if (rbnode) {
  532. *value = rbnode->value;
  533. } else {
  534. /* uninitialized registers default to 0 */
  535. *value = 0;
  536. }
  537. return 0;
  538. }
  539. static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
  540. {
  541. struct rb_node *next;
  542. struct snd_soc_rbtree_ctx *rbtree_ctx;
  543. struct snd_soc_rbtree_node *rbtree_node;
  544. /* if we've already been called then just return */
  545. rbtree_ctx = codec->reg_cache;
  546. if (!rbtree_ctx)
  547. return 0;
  548. /* free up the rbtree */
  549. next = rb_first(&rbtree_ctx->root);
  550. while (next) {
  551. rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
  552. next = rb_next(&rbtree_node->node);
  553. rb_erase(&rbtree_node->node, &rbtree_ctx->root);
  554. kfree(rbtree_node);
  555. }
  556. /* release the resources */
  557. kfree(codec->reg_cache);
  558. codec->reg_cache = NULL;
  559. return 0;
  560. }
  561. static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
  562. {
  563. struct snd_soc_rbtree_node *rbtree_node;
  564. struct snd_soc_rbtree_ctx *rbtree_ctx;
  565. unsigned int val;
  566. unsigned int word_size;
  567. int i;
  568. int ret;
  569. codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
  570. if (!codec->reg_cache)
  571. return -ENOMEM;
  572. rbtree_ctx = codec->reg_cache;
  573. rbtree_ctx->root = RB_ROOT;
  574. if (!codec->reg_def_copy)
  575. return 0;
  576. /*
  577. * populate the rbtree with the initialized registers. All other
  578. * registers will be inserted when they are first modified.
  579. */
  580. word_size = codec->driver->reg_word_size;
  581. for (i = 0; i < codec->driver->reg_cache_size; ++i) {
  582. val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size);
  583. if (!val)
  584. continue;
  585. rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL);
  586. if (!rbtree_node) {
  587. ret = -ENOMEM;
  588. snd_soc_cache_exit(codec);
  589. break;
  590. }
  591. rbtree_node->reg = i;
  592. rbtree_node->value = val;
  593. rbtree_node->defval = val;
  594. snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node);
  595. }
  596. return 0;
  597. }
  598. #ifdef CONFIG_SND_SOC_CACHE_LZO
  599. struct snd_soc_lzo_ctx {
  600. void *wmem;
  601. void *dst;
  602. const void *src;
  603. size_t src_len;
  604. size_t dst_len;
  605. size_t decompressed_size;
  606. unsigned long *sync_bmp;
  607. int sync_bmp_nbits;
  608. };
  609. #define LZO_BLOCK_NUM 8
  610. static int snd_soc_lzo_block_count(void)
  611. {
  612. return LZO_BLOCK_NUM;
  613. }
  614. static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
  615. {
  616. lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  617. if (!lzo_ctx->wmem)
  618. return -ENOMEM;
  619. return 0;
  620. }
  621. static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
  622. {
  623. size_t compress_size;
  624. int ret;
  625. ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
  626. lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
  627. if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
  628. return -EINVAL;
  629. lzo_ctx->dst_len = compress_size;
  630. return 0;
  631. }
  632. static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
  633. {
  634. size_t dst_len;
  635. int ret;
  636. dst_len = lzo_ctx->dst_len;
  637. ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
  638. lzo_ctx->dst, &dst_len);
  639. if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
  640. return -EINVAL;
  641. return 0;
  642. }
  643. static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
  644. struct snd_soc_lzo_ctx *lzo_ctx)
  645. {
  646. int ret;
  647. lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
  648. lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
  649. if (!lzo_ctx->dst) {
  650. lzo_ctx->dst_len = 0;
  651. return -ENOMEM;
  652. }
  653. ret = snd_soc_lzo_compress(lzo_ctx);
  654. if (ret < 0)
  655. return ret;
  656. return 0;
  657. }
  658. static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
  659. struct snd_soc_lzo_ctx *lzo_ctx)
  660. {
  661. int ret;
  662. lzo_ctx->dst_len = lzo_ctx->decompressed_size;
  663. lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
  664. if (!lzo_ctx->dst) {
  665. lzo_ctx->dst_len = 0;
  666. return -ENOMEM;
  667. }
  668. ret = snd_soc_lzo_decompress(lzo_ctx);
  669. if (ret < 0)
  670. return ret;
  671. return 0;
  672. }
  673. static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
  674. unsigned int reg)
  675. {
  676. const struct snd_soc_codec_driver *codec_drv;
  677. codec_drv = codec->driver;
  678. return (reg * codec_drv->reg_word_size) /
  679. DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
  680. }
  681. static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
  682. unsigned int reg)
  683. {
  684. const struct snd_soc_codec_driver *codec_drv;
  685. codec_drv = codec->driver;
  686. return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
  687. codec_drv->reg_word_size);
  688. }
  689. static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
  690. {
  691. const struct snd_soc_codec_driver *codec_drv;
  692. codec_drv = codec->driver;
  693. return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
  694. }
  695. static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
  696. {
  697. struct snd_soc_lzo_ctx **lzo_blocks;
  698. unsigned int val;
  699. int i;
  700. int ret;
  701. lzo_blocks = codec->reg_cache;
  702. for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
  703. WARN_ON(codec->writable_register &&
  704. codec->writable_register(codec, i));
  705. ret = snd_soc_cache_read(codec, i, &val);
  706. if (ret)
  707. return ret;
  708. codec->cache_bypass = 1;
  709. ret = snd_soc_write(codec, i, val);
  710. codec->cache_bypass = 0;
  711. if (ret)
  712. return ret;
  713. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  714. i, val);
  715. }
  716. return 0;
  717. }
  718. static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
  719. unsigned int reg, unsigned int value)
  720. {
  721. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  722. int ret, blkindex, blkpos;
  723. size_t blksize, tmp_dst_len;
  724. void *tmp_dst;
  725. /* index of the compressed lzo block */
  726. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  727. /* register index within the decompressed block */
  728. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  729. /* size of the compressed block */
  730. blksize = snd_soc_lzo_get_blksize(codec);
  731. lzo_blocks = codec->reg_cache;
  732. lzo_block = lzo_blocks[blkindex];
  733. /* save the pointer and length of the compressed block */
  734. tmp_dst = lzo_block->dst;
  735. tmp_dst_len = lzo_block->dst_len;
  736. /* prepare the source to be the compressed block */
  737. lzo_block->src = lzo_block->dst;
  738. lzo_block->src_len = lzo_block->dst_len;
  739. /* decompress the block */
  740. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  741. if (ret < 0) {
  742. kfree(lzo_block->dst);
  743. goto out;
  744. }
  745. /* write the new value to the cache */
  746. if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
  747. codec->driver->reg_word_size)) {
  748. kfree(lzo_block->dst);
  749. goto out;
  750. }
  751. /* prepare the source to be the decompressed block */
  752. lzo_block->src = lzo_block->dst;
  753. lzo_block->src_len = lzo_block->dst_len;
  754. /* compress the block */
  755. ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
  756. if (ret < 0) {
  757. kfree(lzo_block->dst);
  758. kfree(lzo_block->src);
  759. goto out;
  760. }
  761. /* set the bit so we know we have to sync this register */
  762. set_bit(reg, lzo_block->sync_bmp);
  763. kfree(tmp_dst);
  764. kfree(lzo_block->src);
  765. return 0;
  766. out:
  767. lzo_block->dst = tmp_dst;
  768. lzo_block->dst_len = tmp_dst_len;
  769. return ret;
  770. }
  771. static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
  772. unsigned int reg, unsigned int *value)
  773. {
  774. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  775. int ret, blkindex, blkpos;
  776. size_t blksize, tmp_dst_len;
  777. void *tmp_dst;
  778. *value = 0;
  779. /* index of the compressed lzo block */
  780. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  781. /* register index within the decompressed block */
  782. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  783. /* size of the compressed block */
  784. blksize = snd_soc_lzo_get_blksize(codec);
  785. lzo_blocks = codec->reg_cache;
  786. lzo_block = lzo_blocks[blkindex];
  787. /* save the pointer and length of the compressed block */
  788. tmp_dst = lzo_block->dst;
  789. tmp_dst_len = lzo_block->dst_len;
  790. /* prepare the source to be the compressed block */
  791. lzo_block->src = lzo_block->dst;
  792. lzo_block->src_len = lzo_block->dst_len;
  793. /* decompress the block */
  794. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  795. if (ret >= 0)
  796. /* fetch the value from the cache */
  797. *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
  798. codec->driver->reg_word_size);
  799. kfree(lzo_block->dst);
  800. /* restore the pointer and length of the compressed block */
  801. lzo_block->dst = tmp_dst;
  802. lzo_block->dst_len = tmp_dst_len;
  803. return 0;
  804. }
  805. static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
  806. {
  807. struct snd_soc_lzo_ctx **lzo_blocks;
  808. int i, blkcount;
  809. lzo_blocks = codec->reg_cache;
  810. if (!lzo_blocks)
  811. return 0;
  812. blkcount = snd_soc_lzo_block_count();
  813. /*
  814. * the pointer to the bitmap used for syncing the cache
  815. * is shared amongst all lzo_blocks. Ensure it is freed
  816. * only once.
  817. */
  818. if (lzo_blocks[0])
  819. kfree(lzo_blocks[0]->sync_bmp);
  820. for (i = 0; i < blkcount; ++i) {
  821. if (lzo_blocks[i]) {
  822. kfree(lzo_blocks[i]->wmem);
  823. kfree(lzo_blocks[i]->dst);
  824. }
  825. /* each lzo_block is a pointer returned by kmalloc or NULL */
  826. kfree(lzo_blocks[i]);
  827. }
  828. kfree(lzo_blocks);
  829. codec->reg_cache = NULL;
  830. return 0;
  831. }
  832. static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
  833. {
  834. struct snd_soc_lzo_ctx **lzo_blocks;
  835. size_t bmp_size;
  836. const struct snd_soc_codec_driver *codec_drv;
  837. int ret, tofree, i, blksize, blkcount;
  838. const char *p, *end;
  839. unsigned long *sync_bmp;
  840. ret = 0;
  841. codec_drv = codec->driver;
  842. /*
  843. * If we have not been given a default register cache
  844. * then allocate a dummy zero-ed out region, compress it
  845. * and remember to free it afterwards.
  846. */
  847. tofree = 0;
  848. if (!codec->reg_def_copy)
  849. tofree = 1;
  850. if (!codec->reg_def_copy) {
  851. codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
  852. if (!codec->reg_def_copy)
  853. return -ENOMEM;
  854. }
  855. blkcount = snd_soc_lzo_block_count();
  856. codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
  857. GFP_KERNEL);
  858. if (!codec->reg_cache) {
  859. ret = -ENOMEM;
  860. goto err_tofree;
  861. }
  862. lzo_blocks = codec->reg_cache;
  863. /*
  864. * allocate a bitmap to be used when syncing the cache with
  865. * the hardware. Each time a register is modified, the corresponding
  866. * bit is set in the bitmap, so we know that we have to sync
  867. * that register.
  868. */
  869. bmp_size = codec_drv->reg_cache_size;
  870. sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
  871. GFP_KERNEL);
  872. if (!sync_bmp) {
  873. ret = -ENOMEM;
  874. goto err;
  875. }
  876. bitmap_zero(sync_bmp, bmp_size);
  877. /* allocate the lzo blocks and initialize them */
  878. for (i = 0; i < blkcount; ++i) {
  879. lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
  880. GFP_KERNEL);
  881. if (!lzo_blocks[i]) {
  882. kfree(sync_bmp);
  883. ret = -ENOMEM;
  884. goto err;
  885. }
  886. lzo_blocks[i]->sync_bmp = sync_bmp;
  887. lzo_blocks[i]->sync_bmp_nbits = bmp_size;
  888. /* alloc the working space for the compressed block */
  889. ret = snd_soc_lzo_prepare(lzo_blocks[i]);
  890. if (ret < 0)
  891. goto err;
  892. }
  893. blksize = snd_soc_lzo_get_blksize(codec);
  894. p = codec->reg_def_copy;
  895. end = codec->reg_def_copy + codec->reg_size;
  896. /* compress the register map and fill the lzo blocks */
  897. for (i = 0; i < blkcount; ++i, p += blksize) {
  898. lzo_blocks[i]->src = p;
  899. if (p + blksize > end)
  900. lzo_blocks[i]->src_len = end - p;
  901. else
  902. lzo_blocks[i]->src_len = blksize;
  903. ret = snd_soc_lzo_compress_cache_block(codec,
  904. lzo_blocks[i]);
  905. if (ret < 0)
  906. goto err;
  907. lzo_blocks[i]->decompressed_size =
  908. lzo_blocks[i]->src_len;
  909. }
  910. if (tofree) {
  911. kfree(codec->reg_def_copy);
  912. codec->reg_def_copy = NULL;
  913. }
  914. return 0;
  915. err:
  916. snd_soc_cache_exit(codec);
  917. err_tofree:
  918. if (tofree) {
  919. kfree(codec->reg_def_copy);
  920. codec->reg_def_copy = NULL;
  921. }
  922. return ret;
  923. }
  924. #endif
  925. static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
  926. {
  927. int i;
  928. int ret;
  929. const struct snd_soc_codec_driver *codec_drv;
  930. unsigned int val;
  931. codec_drv = codec->driver;
  932. for (i = 0; i < codec_drv->reg_cache_size; ++i) {
  933. WARN_ON(codec->writable_register &&
  934. codec->writable_register(codec, i));
  935. ret = snd_soc_cache_read(codec, i, &val);
  936. if (ret)
  937. return ret;
  938. if (codec->reg_def_copy)
  939. if (snd_soc_get_cache_val(codec->reg_def_copy,
  940. i, codec_drv->reg_word_size) == val)
  941. continue;
  942. ret = snd_soc_write(codec, i, val);
  943. if (ret)
  944. return ret;
  945. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  946. i, val);
  947. }
  948. return 0;
  949. }
  950. static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
  951. unsigned int reg, unsigned int value)
  952. {
  953. snd_soc_set_cache_val(codec->reg_cache, reg, value,
  954. codec->driver->reg_word_size);
  955. return 0;
  956. }
  957. static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
  958. unsigned int reg, unsigned int *value)
  959. {
  960. *value = snd_soc_get_cache_val(codec->reg_cache, reg,
  961. codec->driver->reg_word_size);
  962. return 0;
  963. }
  964. static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
  965. {
  966. if (!codec->reg_cache)
  967. return 0;
  968. kfree(codec->reg_cache);
  969. codec->reg_cache = NULL;
  970. return 0;
  971. }
  972. static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
  973. {
  974. const struct snd_soc_codec_driver *codec_drv;
  975. codec_drv = codec->driver;
  976. if (codec->reg_def_copy)
  977. codec->reg_cache = kmemdup(codec->reg_def_copy,
  978. codec->reg_size, GFP_KERNEL);
  979. else
  980. codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
  981. if (!codec->reg_cache)
  982. return -ENOMEM;
  983. return 0;
  984. }
  985. /* an array of all supported compression types */
  986. static const struct snd_soc_cache_ops cache_types[] = {
  987. /* Flat *must* be the first entry for fallback */
  988. {
  989. .id = SND_SOC_FLAT_COMPRESSION,
  990. .name = "flat",
  991. .init = snd_soc_flat_cache_init,
  992. .exit = snd_soc_flat_cache_exit,
  993. .read = snd_soc_flat_cache_read,
  994. .write = snd_soc_flat_cache_write,
  995. .sync = snd_soc_flat_cache_sync
  996. },
  997. #ifdef CONFIG_SND_SOC_CACHE_LZO
  998. {
  999. .id = SND_SOC_LZO_COMPRESSION,
  1000. .name = "LZO",
  1001. .init = snd_soc_lzo_cache_init,
  1002. .exit = snd_soc_lzo_cache_exit,
  1003. .read = snd_soc_lzo_cache_read,
  1004. .write = snd_soc_lzo_cache_write,
  1005. .sync = snd_soc_lzo_cache_sync
  1006. },
  1007. #endif
  1008. {
  1009. .id = SND_SOC_RBTREE_COMPRESSION,
  1010. .name = "rbtree",
  1011. .init = snd_soc_rbtree_cache_init,
  1012. .exit = snd_soc_rbtree_cache_exit,
  1013. .read = snd_soc_rbtree_cache_read,
  1014. .write = snd_soc_rbtree_cache_write,
  1015. .sync = snd_soc_rbtree_cache_sync
  1016. }
  1017. };
  1018. int snd_soc_cache_init(struct snd_soc_codec *codec)
  1019. {
  1020. int i;
  1021. for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
  1022. if (cache_types[i].id == codec->compress_type)
  1023. break;
  1024. /* Fall back to flat compression */
  1025. if (i == ARRAY_SIZE(cache_types)) {
  1026. dev_warn(codec->dev, "Could not match compress type: %d\n",
  1027. codec->compress_type);
  1028. i = 0;
  1029. }
  1030. mutex_init(&codec->cache_rw_mutex);
  1031. codec->cache_ops = &cache_types[i];
  1032. if (codec->cache_ops->init) {
  1033. if (codec->cache_ops->name)
  1034. dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
  1035. codec->cache_ops->name, codec->name);
  1036. return codec->cache_ops->init(codec);
  1037. }
  1038. return -ENOSYS;
  1039. }
  1040. /*
  1041. * NOTE: keep in mind that this function might be called
  1042. * multiple times.
  1043. */
  1044. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  1045. {
  1046. if (codec->cache_ops && codec->cache_ops->exit) {
  1047. if (codec->cache_ops->name)
  1048. dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
  1049. codec->cache_ops->name, codec->name);
  1050. return codec->cache_ops->exit(codec);
  1051. }
  1052. return -ENOSYS;
  1053. }
  1054. /**
  1055. * snd_soc_cache_read: Fetch the value of a given register from the cache.
  1056. *
  1057. * @codec: CODEC to configure.
  1058. * @reg: The register index.
  1059. * @value: The value to be returned.
  1060. */
  1061. int snd_soc_cache_read(struct snd_soc_codec *codec,
  1062. unsigned int reg, unsigned int *value)
  1063. {
  1064. int ret;
  1065. mutex_lock(&codec->cache_rw_mutex);
  1066. if (value && codec->cache_ops && codec->cache_ops->read) {
  1067. ret = codec->cache_ops->read(codec, reg, value);
  1068. mutex_unlock(&codec->cache_rw_mutex);
  1069. return ret;
  1070. }
  1071. mutex_unlock(&codec->cache_rw_mutex);
  1072. return -ENOSYS;
  1073. }
  1074. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  1075. /**
  1076. * snd_soc_cache_write: Set the value of a given register in the cache.
  1077. *
  1078. * @codec: CODEC to configure.
  1079. * @reg: The register index.
  1080. * @value: The new register value.
  1081. */
  1082. int snd_soc_cache_write(struct snd_soc_codec *codec,
  1083. unsigned int reg, unsigned int value)
  1084. {
  1085. int ret;
  1086. mutex_lock(&codec->cache_rw_mutex);
  1087. if (codec->cache_ops && codec->cache_ops->write) {
  1088. ret = codec->cache_ops->write(codec, reg, value);
  1089. mutex_unlock(&codec->cache_rw_mutex);
  1090. return ret;
  1091. }
  1092. mutex_unlock(&codec->cache_rw_mutex);
  1093. return -ENOSYS;
  1094. }
  1095. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  1096. /**
  1097. * snd_soc_cache_sync: Sync the register cache with the hardware.
  1098. *
  1099. * @codec: CODEC to configure.
  1100. *
  1101. * Any registers that should not be synced should be marked as
  1102. * volatile. In general drivers can choose not to use the provided
  1103. * syncing functionality if they so require.
  1104. */
  1105. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  1106. {
  1107. int ret;
  1108. const char *name;
  1109. if (!codec->cache_sync) {
  1110. return 0;
  1111. }
  1112. if (!codec->cache_ops || !codec->cache_ops->sync)
  1113. return -ENOSYS;
  1114. if (codec->cache_ops->name)
  1115. name = codec->cache_ops->name;
  1116. else
  1117. name = "unknown";
  1118. if (codec->cache_ops->name)
  1119. dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
  1120. codec->cache_ops->name, codec->name);
  1121. trace_snd_soc_cache_sync(codec, name, "start");
  1122. ret = codec->cache_ops->sync(codec);
  1123. if (!ret)
  1124. codec->cache_sync = 0;
  1125. trace_snd_soc_cache_sync(codec, name, "end");
  1126. return ret;
  1127. }
  1128. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
  1129. static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
  1130. unsigned int reg)
  1131. {
  1132. const struct snd_soc_codec_driver *codec_drv;
  1133. unsigned int min, max, index;
  1134. codec_drv = codec->driver;
  1135. min = 0;
  1136. max = codec_drv->reg_access_size - 1;
  1137. do {
  1138. index = (min + max) / 2;
  1139. if (codec_drv->reg_access_default[index].reg == reg)
  1140. return index;
  1141. if (codec_drv->reg_access_default[index].reg < reg)
  1142. min = index + 1;
  1143. else
  1144. max = index;
  1145. } while (min <= max);
  1146. return -1;
  1147. }
  1148. int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
  1149. unsigned int reg)
  1150. {
  1151. int index;
  1152. if (reg >= codec->driver->reg_cache_size)
  1153. return 1;
  1154. index = snd_soc_get_reg_access_index(codec, reg);
  1155. if (index < 0)
  1156. return 0;
  1157. return codec->driver->reg_access_default[index].vol;
  1158. }
  1159. EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
  1160. int snd_soc_default_readable_register(struct snd_soc_codec *codec,
  1161. unsigned int reg)
  1162. {
  1163. int index;
  1164. if (reg >= codec->driver->reg_cache_size)
  1165. return 1;
  1166. index = snd_soc_get_reg_access_index(codec, reg);
  1167. if (index < 0)
  1168. return 0;
  1169. return codec->driver->reg_access_default[index].read;
  1170. }
  1171. EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
  1172. int snd_soc_default_writable_register(struct snd_soc_codec *codec,
  1173. unsigned int reg)
  1174. {
  1175. int index;
  1176. if (reg >= codec->driver->reg_cache_size)
  1177. return 1;
  1178. index = snd_soc_get_reg_access_index(codec, reg);
  1179. if (index < 0)
  1180. return 0;
  1181. return codec->driver->reg_access_default[index].write;
  1182. }
  1183. EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);