soc-io.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 <sound/soc.h>
  16. #include <trace/events/asoc.h>
  17. #ifdef CONFIG_SPI_MASTER
  18. static int do_spi_write(void *control, const char *data, int len)
  19. {
  20. struct spi_device *spi = control;
  21. int ret;
  22. ret = spi_write(spi, data, len);
  23. if (ret < 0)
  24. return ret;
  25. return len;
  26. }
  27. #endif
  28. static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
  29. unsigned int value, const void *data, int len)
  30. {
  31. int ret;
  32. if (!snd_soc_codec_volatile_register(codec, reg) &&
  33. reg < codec->driver->reg_cache_size &&
  34. !codec->cache_bypass) {
  35. ret = snd_soc_cache_write(codec, reg, value);
  36. if (ret < 0)
  37. return -1;
  38. }
  39. if (codec->cache_only) {
  40. codec->cache_sync = 1;
  41. return 0;
  42. }
  43. ret = codec->hw_write(codec->control_data, data, len);
  44. if (ret == len)
  45. return 0;
  46. if (ret < 0)
  47. return ret;
  48. else
  49. return -EIO;
  50. }
  51. static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
  52. {
  53. int ret;
  54. unsigned int val;
  55. if (reg >= codec->driver->reg_cache_size ||
  56. snd_soc_codec_volatile_register(codec, reg) ||
  57. codec->cache_bypass) {
  58. if (codec->cache_only)
  59. return -1;
  60. BUG_ON(!codec->hw_read);
  61. return codec->hw_read(codec, reg);
  62. }
  63. ret = snd_soc_cache_read(codec, reg, &val);
  64. if (ret < 0)
  65. return -1;
  66. return val;
  67. }
  68. static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
  69. unsigned int value)
  70. {
  71. u16 data;
  72. data = cpu_to_be16((reg << 12) | (value & 0xffffff));
  73. return do_hw_write(codec, reg, value, &data, 2);
  74. }
  75. static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
  76. unsigned int value)
  77. {
  78. u16 data;
  79. data = cpu_to_be16((reg << 9) | (value & 0x1ff));
  80. return do_hw_write(codec, reg, value, &data, 2);
  81. }
  82. static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
  83. unsigned int value)
  84. {
  85. u8 data[2];
  86. reg &= 0xff;
  87. data[0] = reg;
  88. data[1] = value & 0xff;
  89. return do_hw_write(codec, reg, value, data, 2);
  90. }
  91. static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
  92. unsigned int value)
  93. {
  94. u8 data[3];
  95. u16 val = cpu_to_be16(value);
  96. data[0] = reg;
  97. memcpy(&data[1], &val, sizeof(val));
  98. return do_hw_write(codec, reg, value, data, 3);
  99. }
  100. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  101. static unsigned int do_i2c_read(struct snd_soc_codec *codec,
  102. void *reg, int reglen,
  103. void *data, int datalen)
  104. {
  105. struct i2c_msg xfer[2];
  106. int ret;
  107. struct i2c_client *client = codec->control_data;
  108. /* Write register */
  109. xfer[0].addr = client->addr;
  110. xfer[0].flags = 0;
  111. xfer[0].len = reglen;
  112. xfer[0].buf = reg;
  113. /* Read data */
  114. xfer[1].addr = client->addr;
  115. xfer[1].flags = I2C_M_RD;
  116. xfer[1].len = datalen;
  117. xfer[1].buf = data;
  118. ret = i2c_transfer(client->adapter, xfer, 2);
  119. if (ret == 2)
  120. return 0;
  121. else if (ret < 0)
  122. return ret;
  123. else
  124. return -EIO;
  125. }
  126. #endif
  127. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  128. static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
  129. unsigned int r)
  130. {
  131. u8 reg = r;
  132. u8 data;
  133. int ret;
  134. ret = do_i2c_read(codec, &reg, 1, &data, 1);
  135. if (ret < 0)
  136. return 0;
  137. return data;
  138. }
  139. #else
  140. #define snd_soc_8_8_read_i2c NULL
  141. #endif
  142. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  143. static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
  144. unsigned int r)
  145. {
  146. u8 reg = r;
  147. u16 data;
  148. int ret;
  149. ret = do_i2c_read(codec, &reg, 1, &data, 2);
  150. if (ret < 0)
  151. return 0;
  152. return (data >> 8) | ((data & 0xff) << 8);
  153. }
  154. #else
  155. #define snd_soc_8_16_read_i2c NULL
  156. #endif
  157. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  158. static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
  159. unsigned int r)
  160. {
  161. u16 reg = r;
  162. u8 data;
  163. int ret;
  164. ret = do_i2c_read(codec, &reg, 2, &data, 1);
  165. if (ret < 0)
  166. return 0;
  167. return data;
  168. }
  169. #else
  170. #define snd_soc_16_8_read_i2c NULL
  171. #endif
  172. static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
  173. unsigned int value)
  174. {
  175. u8 data[3];
  176. u16 rval = cpu_to_be16(reg);
  177. memcpy(data, &rval, sizeof(rval));
  178. data[2] = value;
  179. return do_hw_write(codec, reg, value, data, 3);
  180. }
  181. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  182. static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
  183. unsigned int r)
  184. {
  185. u16 reg = cpu_to_be16(r);
  186. u16 data;
  187. int ret;
  188. ret = do_i2c_read(codec, &reg, 2, &data, 2);
  189. if (ret < 0)
  190. return 0;
  191. return be16_to_cpu(data);
  192. }
  193. #else
  194. #define snd_soc_16_16_read_i2c NULL
  195. #endif
  196. static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
  197. unsigned int value)
  198. {
  199. u16 data[2];
  200. data[0] = cpu_to_be16(reg);
  201. data[1] = cpu_to_be16(value);
  202. return do_hw_write(codec, reg, value, data, sizeof(data));
  203. }
  204. /* Primitive bulk write support for soc-cache. The data pointed to by
  205. * `data' needs to already be in the form the hardware expects
  206. * including any leading register specific data. Any data written
  207. * through this function will not go through the cache as it only
  208. * handles writing to volatile or out of bounds registers.
  209. */
  210. static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg,
  211. const void *data, size_t len)
  212. {
  213. int ret;
  214. /* To ensure that we don't get out of sync with the cache, check
  215. * whether the base register is volatile or if we've directly asked
  216. * to bypass the cache. Out of bounds registers are considered
  217. * volatile.
  218. */
  219. if (!codec->cache_bypass
  220. && !snd_soc_codec_volatile_register(codec, reg)
  221. && reg < codec->driver->reg_cache_size)
  222. return -EINVAL;
  223. switch (codec->control_type) {
  224. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  225. case SND_SOC_I2C:
  226. ret = i2c_master_send(to_i2c_client(codec->dev), data, len);
  227. break;
  228. #endif
  229. #if defined(CONFIG_SPI_MASTER)
  230. case SND_SOC_SPI:
  231. ret = spi_write(to_spi_device(codec->dev), data, len);
  232. break;
  233. #endif
  234. default:
  235. BUG();
  236. }
  237. if (ret == len)
  238. return 0;
  239. if (ret < 0)
  240. return ret;
  241. else
  242. return -EIO;
  243. }
  244. static struct {
  245. int addr_bits;
  246. int data_bits;
  247. int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
  248. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  249. unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
  250. } io_types[] = {
  251. {
  252. .addr_bits = 4, .data_bits = 12,
  253. .write = snd_soc_4_12_write,
  254. },
  255. {
  256. .addr_bits = 7, .data_bits = 9,
  257. .write = snd_soc_7_9_write,
  258. },
  259. {
  260. .addr_bits = 8, .data_bits = 8,
  261. .write = snd_soc_8_8_write,
  262. .i2c_read = snd_soc_8_8_read_i2c,
  263. },
  264. {
  265. .addr_bits = 8, .data_bits = 16,
  266. .write = snd_soc_8_16_write,
  267. .i2c_read = snd_soc_8_16_read_i2c,
  268. },
  269. {
  270. .addr_bits = 16, .data_bits = 8,
  271. .write = snd_soc_16_8_write,
  272. .i2c_read = snd_soc_16_8_read_i2c,
  273. },
  274. {
  275. .addr_bits = 16, .data_bits = 16,
  276. .write = snd_soc_16_16_write,
  277. .i2c_read = snd_soc_16_16_read_i2c,
  278. },
  279. };
  280. /**
  281. * snd_soc_codec_set_cache_io: Set up standard I/O functions.
  282. *
  283. * @codec: CODEC to configure.
  284. * @addr_bits: Number of bits of register address data.
  285. * @data_bits: Number of bits of data per register.
  286. * @control: Control bus used.
  287. *
  288. * Register formats are frequently shared between many I2C and SPI
  289. * devices. In order to promote code reuse the ASoC core provides
  290. * some standard implementations of CODEC read and write operations
  291. * which can be set up using this function.
  292. *
  293. * The caller is responsible for allocating and initialising the
  294. * actual cache.
  295. *
  296. * Note that at present this code cannot be used by CODECs with
  297. * volatile registers.
  298. */
  299. int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
  300. int addr_bits, int data_bits,
  301. enum snd_soc_control_type control)
  302. {
  303. int i;
  304. for (i = 0; i < ARRAY_SIZE(io_types); i++)
  305. if (io_types[i].addr_bits == addr_bits &&
  306. io_types[i].data_bits == data_bits)
  307. break;
  308. if (i == ARRAY_SIZE(io_types)) {
  309. printk(KERN_ERR
  310. "No I/O functions for %d bit address %d bit data\n",
  311. addr_bits, data_bits);
  312. return -EINVAL;
  313. }
  314. codec->write = io_types[i].write;
  315. codec->read = hw_read;
  316. codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
  317. switch (control) {
  318. case SND_SOC_I2C:
  319. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  320. codec->hw_write = (hw_write_t)i2c_master_send;
  321. #endif
  322. if (io_types[i].i2c_read)
  323. codec->hw_read = io_types[i].i2c_read;
  324. codec->control_data = container_of(codec->dev,
  325. struct i2c_client,
  326. dev);
  327. break;
  328. case SND_SOC_SPI:
  329. #ifdef CONFIG_SPI_MASTER
  330. codec->hw_write = do_spi_write;
  331. #endif
  332. codec->control_data = container_of(codec->dev,
  333. struct spi_device,
  334. dev);
  335. break;
  336. }
  337. return 0;
  338. }
  339. EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);