soc-cache.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
  17. unsigned int reg)
  18. {
  19. u16 *cache = codec->reg_cache;
  20. if (reg >= codec->reg_cache_size)
  21. return -1;
  22. return cache[reg];
  23. }
  24. static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
  25. unsigned int value)
  26. {
  27. u16 *cache = codec->reg_cache;
  28. u8 data[2];
  29. int ret;
  30. BUG_ON(codec->volatile_register);
  31. data[0] = (reg << 4) | ((value >> 8) & 0x000f);
  32. data[1] = value & 0x00ff;
  33. if (reg < codec->reg_cache_size)
  34. cache[reg] = value;
  35. ret = codec->hw_write(codec->control_data, data, 2);
  36. if (ret == 2)
  37. return 0;
  38. if (ret < 0)
  39. return ret;
  40. else
  41. return -EIO;
  42. }
  43. #if defined(CONFIG_SPI_MASTER)
  44. static int snd_soc_4_12_spi_write(void *control_data, const char *data,
  45. int len)
  46. {
  47. struct spi_device *spi = control_data;
  48. struct spi_transfer t;
  49. struct spi_message m;
  50. u8 msg[2];
  51. if (len <= 0)
  52. return 0;
  53. msg[0] = data[1];
  54. msg[1] = data[0];
  55. spi_message_init(&m);
  56. memset(&t, 0, (sizeof t));
  57. t.tx_buf = &msg[0];
  58. t.len = len;
  59. spi_message_add_tail(&t, &m);
  60. spi_sync(spi, &m);
  61. return len;
  62. }
  63. #else
  64. #define snd_soc_4_12_spi_write NULL
  65. #endif
  66. static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
  67. unsigned int reg)
  68. {
  69. u16 *cache = codec->reg_cache;
  70. if (reg >= codec->reg_cache_size)
  71. return -1;
  72. return cache[reg];
  73. }
  74. static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
  75. unsigned int value)
  76. {
  77. u16 *cache = codec->reg_cache;
  78. u8 data[2];
  79. int ret;
  80. BUG_ON(codec->volatile_register);
  81. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  82. data[1] = value & 0x00ff;
  83. if (reg < codec->reg_cache_size)
  84. cache[reg] = value;
  85. ret = codec->hw_write(codec->control_data, data, 2);
  86. if (ret == 2)
  87. return 0;
  88. if (ret < 0)
  89. return ret;
  90. else
  91. return -EIO;
  92. }
  93. #if defined(CONFIG_SPI_MASTER)
  94. static int snd_soc_7_9_spi_write(void *control_data, const char *data,
  95. int len)
  96. {
  97. struct spi_device *spi = control_data;
  98. struct spi_transfer t;
  99. struct spi_message m;
  100. u8 msg[2];
  101. if (len <= 0)
  102. return 0;
  103. msg[0] = data[0];
  104. msg[1] = data[1];
  105. spi_message_init(&m);
  106. memset(&t, 0, (sizeof t));
  107. t.tx_buf = &msg[0];
  108. t.len = len;
  109. spi_message_add_tail(&t, &m);
  110. spi_sync(spi, &m);
  111. return len;
  112. }
  113. #else
  114. #define snd_soc_7_9_spi_write NULL
  115. #endif
  116. static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
  117. unsigned int value)
  118. {
  119. u8 *cache = codec->reg_cache;
  120. u8 data[2];
  121. BUG_ON(codec->volatile_register);
  122. data[0] = reg & 0xff;
  123. data[1] = value & 0xff;
  124. if (reg < codec->reg_cache_size)
  125. cache[reg] = value;
  126. if (codec->hw_write(codec->control_data, data, 2) == 2)
  127. return 0;
  128. else
  129. return -EIO;
  130. }
  131. static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
  132. unsigned int reg)
  133. {
  134. u8 *cache = codec->reg_cache;
  135. if (reg >= codec->reg_cache_size)
  136. return -1;
  137. return cache[reg];
  138. }
  139. static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
  140. unsigned int value)
  141. {
  142. u16 *reg_cache = codec->reg_cache;
  143. u8 data[3];
  144. data[0] = reg;
  145. data[1] = (value >> 8) & 0xff;
  146. data[2] = value & 0xff;
  147. if (!snd_soc_codec_volatile_register(codec, reg))
  148. reg_cache[reg] = value;
  149. if (codec->hw_write(codec->control_data, data, 3) == 3)
  150. return 0;
  151. else
  152. return -EIO;
  153. }
  154. static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
  155. unsigned int reg)
  156. {
  157. u16 *cache = codec->reg_cache;
  158. if (reg >= codec->reg_cache_size ||
  159. snd_soc_codec_volatile_register(codec, reg))
  160. return codec->hw_read(codec, reg);
  161. else
  162. return cache[reg];
  163. }
  164. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  165. static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
  166. unsigned int r)
  167. {
  168. struct i2c_msg xfer[2];
  169. u8 reg = r;
  170. u16 data;
  171. int ret;
  172. struct i2c_client *client = codec->control_data;
  173. /* Write register */
  174. xfer[0].addr = client->addr;
  175. xfer[0].flags = 0;
  176. xfer[0].len = 1;
  177. xfer[0].buf = &reg;
  178. /* Read data */
  179. xfer[1].addr = client->addr;
  180. xfer[1].flags = I2C_M_RD;
  181. xfer[1].len = 2;
  182. xfer[1].buf = (u8 *)&data;
  183. ret = i2c_transfer(client->adapter, xfer, 2);
  184. if (ret != 2) {
  185. dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
  186. return 0;
  187. }
  188. return (data >> 8) | ((data & 0xff) << 8);
  189. }
  190. #else
  191. #define snd_soc_8_16_read_i2c NULL
  192. #endif
  193. static struct {
  194. int addr_bits;
  195. int data_bits;
  196. int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
  197. int (*spi_write)(void *, const char *, int);
  198. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  199. unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
  200. } io_types[] = {
  201. {
  202. .addr_bits = 4, .data_bits = 12,
  203. .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
  204. .spi_write = snd_soc_4_12_spi_write,
  205. },
  206. {
  207. .addr_bits = 7, .data_bits = 9,
  208. .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
  209. .spi_write = snd_soc_7_9_spi_write,
  210. },
  211. {
  212. .addr_bits = 8, .data_bits = 8,
  213. .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
  214. },
  215. {
  216. .addr_bits = 8, .data_bits = 16,
  217. .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
  218. .i2c_read = snd_soc_8_16_read_i2c,
  219. },
  220. };
  221. /**
  222. * snd_soc_codec_set_cache_io: Set up standard I/O functions.
  223. *
  224. * @codec: CODEC to configure.
  225. * @type: Type of cache.
  226. * @addr_bits: Number of bits of register address data.
  227. * @data_bits: Number of bits of data per register.
  228. * @control: Control bus used.
  229. *
  230. * Register formats are frequently shared between many I2C and SPI
  231. * devices. In order to promote code reuse the ASoC core provides
  232. * some standard implementations of CODEC read and write operations
  233. * which can be set up using this function.
  234. *
  235. * The caller is responsible for allocating and initialising the
  236. * actual cache.
  237. *
  238. * Note that at present this code cannot be used by CODECs with
  239. * volatile registers.
  240. */
  241. int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
  242. int addr_bits, int data_bits,
  243. enum snd_soc_control_type control)
  244. {
  245. int i;
  246. for (i = 0; i < ARRAY_SIZE(io_types); i++)
  247. if (io_types[i].addr_bits == addr_bits &&
  248. io_types[i].data_bits == data_bits)
  249. break;
  250. if (i == ARRAY_SIZE(io_types)) {
  251. printk(KERN_ERR
  252. "No I/O functions for %d bit address %d bit data\n",
  253. addr_bits, data_bits);
  254. return -EINVAL;
  255. }
  256. codec->write = io_types[i].write;
  257. codec->read = io_types[i].read;
  258. switch (control) {
  259. case SND_SOC_CUSTOM:
  260. break;
  261. case SND_SOC_I2C:
  262. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  263. codec->hw_write = (hw_write_t)i2c_master_send;
  264. #endif
  265. if (io_types[i].i2c_read)
  266. codec->hw_read = io_types[i].i2c_read;
  267. break;
  268. case SND_SOC_SPI:
  269. if (io_types[i].spi_write)
  270. codec->hw_write = io_types[i].spi_write;
  271. break;
  272. }
  273. return 0;
  274. }
  275. EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);