soc-cache.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_7_9_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_7_9_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 << 1) | ((value >> 8) & 0x0001);
  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_7_9_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[0];
  54. msg[1] = data[1];
  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_7_9_spi_write NULL
  65. #endif
  66. static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
  67. unsigned int value)
  68. {
  69. u8 *cache = codec->reg_cache;
  70. u8 data[2];
  71. BUG_ON(codec->volatile_register);
  72. data[0] = reg & 0xff;
  73. data[1] = value & 0xff;
  74. if (reg < codec->reg_cache_size)
  75. cache[reg] = value;
  76. if (codec->hw_write(codec->control_data, data, 2) == 2)
  77. return 0;
  78. else
  79. return -EIO;
  80. }
  81. static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
  82. unsigned int reg)
  83. {
  84. u8 *cache = codec->reg_cache;
  85. if (reg >= codec->reg_cache_size)
  86. return -1;
  87. return cache[reg];
  88. }
  89. static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
  90. unsigned int value)
  91. {
  92. u16 *reg_cache = codec->reg_cache;
  93. u8 data[3];
  94. data[0] = reg;
  95. data[1] = (value >> 8) & 0xff;
  96. data[2] = value & 0xff;
  97. if (!snd_soc_codec_volatile_register(codec, reg))
  98. reg_cache[reg] = value;
  99. if (codec->hw_write(codec->control_data, data, 3) == 3)
  100. return 0;
  101. else
  102. return -EIO;
  103. }
  104. static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
  105. unsigned int reg)
  106. {
  107. u16 *cache = codec->reg_cache;
  108. if (reg >= codec->reg_cache_size ||
  109. snd_soc_codec_volatile_register(codec, reg))
  110. return codec->hw_read(codec, reg);
  111. else
  112. return cache[reg];
  113. }
  114. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  115. static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
  116. unsigned int r)
  117. {
  118. struct i2c_msg xfer[2];
  119. u8 reg = r;
  120. u16 data;
  121. int ret;
  122. struct i2c_client *client = codec->control_data;
  123. /* Write register */
  124. xfer[0].addr = client->addr;
  125. xfer[0].flags = 0;
  126. xfer[0].len = 1;
  127. xfer[0].buf = &reg;
  128. /* Read data */
  129. xfer[1].addr = client->addr;
  130. xfer[1].flags = I2C_M_RD;
  131. xfer[1].len = 2;
  132. xfer[1].buf = (u8 *)&data;
  133. ret = i2c_transfer(client->adapter, xfer, 2);
  134. if (ret != 2) {
  135. dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
  136. return 0;
  137. }
  138. return (data >> 8) | ((data & 0xff) << 8);
  139. }
  140. #else
  141. #define snd_soc_8_16_read_i2c NULL
  142. #endif
  143. static struct {
  144. int addr_bits;
  145. int data_bits;
  146. int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
  147. int (*spi_write)(void *, const char *, int);
  148. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  149. unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
  150. } io_types[] = {
  151. {
  152. .addr_bits = 7, .data_bits = 9,
  153. .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
  154. .spi_write = snd_soc_7_9_spi_write
  155. },
  156. {
  157. .addr_bits = 8, .data_bits = 8,
  158. .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
  159. },
  160. {
  161. .addr_bits = 8, .data_bits = 16,
  162. .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
  163. .i2c_read = snd_soc_8_16_read_i2c,
  164. },
  165. };
  166. /**
  167. * snd_soc_codec_set_cache_io: Set up standard I/O functions.
  168. *
  169. * @codec: CODEC to configure.
  170. * @type: Type of cache.
  171. * @addr_bits: Number of bits of register address data.
  172. * @data_bits: Number of bits of data per register.
  173. * @control: Control bus used.
  174. *
  175. * Register formats are frequently shared between many I2C and SPI
  176. * devices. In order to promote code reuse the ASoC core provides
  177. * some standard implementations of CODEC read and write operations
  178. * which can be set up using this function.
  179. *
  180. * The caller is responsible for allocating and initialising the
  181. * actual cache.
  182. *
  183. * Note that at present this code cannot be used by CODECs with
  184. * volatile registers.
  185. */
  186. int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
  187. int addr_bits, int data_bits,
  188. enum snd_soc_control_type control)
  189. {
  190. int i;
  191. for (i = 0; i < ARRAY_SIZE(io_types); i++)
  192. if (io_types[i].addr_bits == addr_bits &&
  193. io_types[i].data_bits == data_bits)
  194. break;
  195. if (i == ARRAY_SIZE(io_types)) {
  196. printk(KERN_ERR
  197. "No I/O functions for %d bit address %d bit data\n",
  198. addr_bits, data_bits);
  199. return -EINVAL;
  200. }
  201. codec->write = io_types[i].write;
  202. codec->read = io_types[i].read;
  203. switch (control) {
  204. case SND_SOC_CUSTOM:
  205. break;
  206. case SND_SOC_I2C:
  207. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  208. codec->hw_write = (hw_write_t)i2c_master_send;
  209. #endif
  210. if (io_types[i].i2c_read)
  211. codec->hw_read = io_types[i].i2c_read;
  212. break;
  213. case SND_SOC_SPI:
  214. if (io_types[i].spi_write)
  215. codec->hw_write = io_types[i].spi_write;
  216. break;
  217. }
  218. return 0;
  219. }
  220. EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);