soc-io.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * soc-io.c -- ASoC register I/O helpers
  3. *
  4. * Copyright 2009-2011 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 <linux/regmap.h>
  16. #include <linux/export.h>
  17. #include <sound/soc.h>
  18. /**
  19. * snd_soc_component_read() - Read register value
  20. * @component: Component to read from
  21. * @reg: Register to read
  22. * @val: Pointer to where the read value is stored
  23. *
  24. * Return: 0 on success, a negative error code otherwise.
  25. */
  26. int snd_soc_component_read(struct snd_soc_component *component,
  27. unsigned int reg, unsigned int *val)
  28. {
  29. int ret;
  30. if (component->regmap)
  31. ret = regmap_read(component->regmap, reg, val);
  32. else if (component->read)
  33. ret = component->read(component, reg, val);
  34. else
  35. ret = -EIO;
  36. return ret;
  37. }
  38. EXPORT_SYMBOL_GPL(snd_soc_component_read);
  39. /**
  40. * snd_soc_component_write() - Write register value
  41. * @component: Component to write to
  42. * @reg: Register to write
  43. * @val: Value to write to the register
  44. *
  45. * Return: 0 on success, a negative error code otherwise.
  46. */
  47. int snd_soc_component_write(struct snd_soc_component *component,
  48. unsigned int reg, unsigned int val)
  49. {
  50. if (component->regmap)
  51. return regmap_write(component->regmap, reg, val);
  52. else if (component->write)
  53. return component->write(component, reg, val);
  54. else
  55. return -EIO;
  56. }
  57. EXPORT_SYMBOL_GPL(snd_soc_component_write);
  58. static int snd_soc_component_update_bits_legacy(
  59. struct snd_soc_component *component, unsigned int reg,
  60. unsigned int mask, unsigned int val, bool *change)
  61. {
  62. unsigned int old, new;
  63. int ret;
  64. if (!component->read || !component->write)
  65. return -EIO;
  66. mutex_lock(&component->io_mutex);
  67. ret = component->read(component, reg, &old);
  68. if (ret < 0)
  69. goto out_unlock;
  70. new = (old & ~mask) | (val & mask);
  71. *change = old != new;
  72. if (*change)
  73. ret = component->write(component, reg, new);
  74. out_unlock:
  75. mutex_unlock(&component->io_mutex);
  76. return ret;
  77. }
  78. /**
  79. * snd_soc_component_update_bits() - Perform read/modify/write cycle
  80. * @component: Component to update
  81. * @reg: Register to update
  82. * @mask: Mask that specifies which bits to update
  83. * @val: New value for the bits specified by mask
  84. *
  85. * Return: 1 if the operation was successful and the value of the register
  86. * changed, 0 if the operation was successful, but the value did not change.
  87. * Returns a negative error code otherwise.
  88. */
  89. int snd_soc_component_update_bits(struct snd_soc_component *component,
  90. unsigned int reg, unsigned int mask, unsigned int val)
  91. {
  92. bool change;
  93. int ret;
  94. if (component->regmap)
  95. ret = regmap_update_bits_check(component->regmap, reg, mask,
  96. val, &change);
  97. else
  98. ret = snd_soc_component_update_bits_legacy(component, reg,
  99. mask, val, &change);
  100. if (ret < 0)
  101. return ret;
  102. return change;
  103. }
  104. EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
  105. /**
  106. * snd_soc_component_update_bits_async() - Perform asynchronous
  107. * read/modify/write cycle
  108. * @component: Component to update
  109. * @reg: Register to update
  110. * @mask: Mask that specifies which bits to update
  111. * @val: New value for the bits specified by mask
  112. *
  113. * This function is similar to snd_soc_component_update_bits(), but the update
  114. * operation is scheduled asynchronously. This means it may not be completed
  115. * when the function returns. To make sure that all scheduled updates have been
  116. * completed snd_soc_component_async_complete() must be called.
  117. *
  118. * Return: 1 if the operation was successful and the value of the register
  119. * changed, 0 if the operation was successful, but the value did not change.
  120. * Returns a negative error code otherwise.
  121. */
  122. int snd_soc_component_update_bits_async(struct snd_soc_component *component,
  123. unsigned int reg, unsigned int mask, unsigned int val)
  124. {
  125. bool change;
  126. int ret;
  127. if (component->regmap)
  128. ret = regmap_update_bits_check_async(component->regmap, reg,
  129. mask, val, &change);
  130. else
  131. ret = snd_soc_component_update_bits_legacy(component, reg,
  132. mask, val, &change);
  133. if (ret < 0)
  134. return ret;
  135. return change;
  136. }
  137. EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
  138. /**
  139. * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
  140. * @component: Component for which to wait
  141. *
  142. * This function blocks until all asynchronous I/O which has previously been
  143. * scheduled using snd_soc_component_update_bits_async() has completed.
  144. */
  145. void snd_soc_component_async_complete(struct snd_soc_component *component)
  146. {
  147. if (component->regmap)
  148. regmap_async_complete(component->regmap);
  149. }
  150. EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
  151. /**
  152. * snd_soc_component_test_bits - Test register for change
  153. * @component: component
  154. * @reg: Register to test
  155. * @mask: Mask that specifies which bits to test
  156. * @value: Value to test against
  157. *
  158. * Tests a register with a new value and checks if the new value is
  159. * different from the old value.
  160. *
  161. * Return: 1 for change, otherwise 0.
  162. */
  163. int snd_soc_component_test_bits(struct snd_soc_component *component,
  164. unsigned int reg, unsigned int mask, unsigned int value)
  165. {
  166. unsigned int old, new;
  167. int ret;
  168. ret = snd_soc_component_read(component, reg, &old);
  169. if (ret < 0)
  170. return ret;
  171. new = (old & ~mask) | value;
  172. return old != new;
  173. }
  174. EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
  175. unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
  176. {
  177. unsigned int val;
  178. int ret;
  179. ret = snd_soc_component_read(&codec->component, reg, &val);
  180. if (ret < 0)
  181. return -1;
  182. return val;
  183. }
  184. EXPORT_SYMBOL_GPL(snd_soc_read);
  185. int snd_soc_write(struct snd_soc_codec *codec, unsigned int reg,
  186. unsigned int val)
  187. {
  188. return snd_soc_component_write(&codec->component, reg, val);
  189. }
  190. EXPORT_SYMBOL_GPL(snd_soc_write);
  191. /**
  192. * snd_soc_update_bits - update codec register bits
  193. * @codec: audio codec
  194. * @reg: codec register
  195. * @mask: register mask
  196. * @value: new value
  197. *
  198. * Writes new register value.
  199. *
  200. * Returns 1 for change, 0 for no change, or negative error code.
  201. */
  202. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned int reg,
  203. unsigned int mask, unsigned int value)
  204. {
  205. return snd_soc_component_update_bits(&codec->component, reg, mask,
  206. value);
  207. }
  208. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  209. /**
  210. * snd_soc_test_bits - test register for change
  211. * @codec: audio codec
  212. * @reg: codec register
  213. * @mask: register mask
  214. * @value: new value
  215. *
  216. * Tests a register with a new value and checks if the new value is
  217. * different from the old value.
  218. *
  219. * Returns 1 for change else 0.
  220. */
  221. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned int reg,
  222. unsigned int mask, unsigned int value)
  223. {
  224. return snd_soc_component_test_bits(&codec->component, reg, mask, value);
  225. }
  226. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  227. int snd_soc_platform_read(struct snd_soc_platform *platform,
  228. unsigned int reg)
  229. {
  230. unsigned int val;
  231. int ret;
  232. ret = snd_soc_component_read(&platform->component, reg, &val);
  233. if (ret < 0)
  234. return -1;
  235. return val;
  236. }
  237. EXPORT_SYMBOL_GPL(snd_soc_platform_read);
  238. int snd_soc_platform_write(struct snd_soc_platform *platform,
  239. unsigned int reg, unsigned int val)
  240. {
  241. return snd_soc_component_write(&platform->component, reg, val);
  242. }
  243. EXPORT_SYMBOL_GPL(snd_soc_platform_write);