soc-cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. if (codec->cache_only) {
  36. codec->cache_sync = 1;
  37. return 0;
  38. }
  39. ret = codec->hw_write(codec->control_data, data, 2);
  40. if (ret == 2)
  41. return 0;
  42. if (ret < 0)
  43. return ret;
  44. else
  45. return -EIO;
  46. }
  47. #if defined(CONFIG_SPI_MASTER)
  48. static int snd_soc_4_12_spi_write(void *control_data, const char *data,
  49. int len)
  50. {
  51. struct spi_device *spi = control_data;
  52. struct spi_transfer t;
  53. struct spi_message m;
  54. u8 msg[2];
  55. if (len <= 0)
  56. return 0;
  57. msg[0] = data[1];
  58. msg[1] = data[0];
  59. spi_message_init(&m);
  60. memset(&t, 0, (sizeof t));
  61. t.tx_buf = &msg[0];
  62. t.len = len;
  63. spi_message_add_tail(&t, &m);
  64. spi_sync(spi, &m);
  65. return len;
  66. }
  67. #else
  68. #define snd_soc_4_12_spi_write NULL
  69. #endif
  70. static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
  71. unsigned int reg)
  72. {
  73. u16 *cache = codec->reg_cache;
  74. if (reg >= codec->reg_cache_size)
  75. return -1;
  76. return cache[reg];
  77. }
  78. static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
  79. unsigned int value)
  80. {
  81. u16 *cache = codec->reg_cache;
  82. u8 data[2];
  83. int ret;
  84. BUG_ON(codec->volatile_register);
  85. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  86. data[1] = value & 0x00ff;
  87. if (reg < codec->reg_cache_size)
  88. cache[reg] = value;
  89. if (codec->cache_only) {
  90. codec->cache_sync = 1;
  91. return 0;
  92. }
  93. ret = codec->hw_write(codec->control_data, data, 2);
  94. if (ret == 2)
  95. return 0;
  96. if (ret < 0)
  97. return ret;
  98. else
  99. return -EIO;
  100. }
  101. #if defined(CONFIG_SPI_MASTER)
  102. static int snd_soc_7_9_spi_write(void *control_data, const char *data,
  103. int len)
  104. {
  105. struct spi_device *spi = control_data;
  106. struct spi_transfer t;
  107. struct spi_message m;
  108. u8 msg[2];
  109. if (len <= 0)
  110. return 0;
  111. msg[0] = data[0];
  112. msg[1] = data[1];
  113. spi_message_init(&m);
  114. memset(&t, 0, (sizeof t));
  115. t.tx_buf = &msg[0];
  116. t.len = len;
  117. spi_message_add_tail(&t, &m);
  118. spi_sync(spi, &m);
  119. return len;
  120. }
  121. #else
  122. #define snd_soc_7_9_spi_write NULL
  123. #endif
  124. static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
  125. unsigned int value)
  126. {
  127. u8 *cache = codec->reg_cache;
  128. u8 data[2];
  129. BUG_ON(codec->volatile_register);
  130. data[0] = reg & 0xff;
  131. data[1] = value & 0xff;
  132. if (reg < codec->reg_cache_size)
  133. cache[reg] = value;
  134. if (codec->cache_only) {
  135. codec->cache_sync = 1;
  136. return 0;
  137. }
  138. if (codec->hw_write(codec->control_data, data, 2) == 2)
  139. return 0;
  140. else
  141. return -EIO;
  142. }
  143. static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
  144. unsigned int reg)
  145. {
  146. u8 *cache = codec->reg_cache;
  147. if (reg >= codec->reg_cache_size)
  148. return -1;
  149. return cache[reg];
  150. }
  151. static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
  152. unsigned int value)
  153. {
  154. u16 *reg_cache = codec->reg_cache;
  155. u8 data[3];
  156. data[0] = reg;
  157. data[1] = (value >> 8) & 0xff;
  158. data[2] = value & 0xff;
  159. if (!snd_soc_codec_volatile_register(codec, reg))
  160. reg_cache[reg] = value;
  161. if (codec->cache_only) {
  162. codec->cache_sync = 1;
  163. return 0;
  164. }
  165. if (codec->hw_write(codec->control_data, data, 3) == 3)
  166. return 0;
  167. else
  168. return -EIO;
  169. }
  170. static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
  171. unsigned int reg)
  172. {
  173. u16 *cache = codec->reg_cache;
  174. if (reg >= codec->reg_cache_size ||
  175. snd_soc_codec_volatile_register(codec, reg)) {
  176. if (codec->cache_only)
  177. return -EINVAL;
  178. return codec->hw_read(codec, reg);
  179. } else {
  180. return cache[reg];
  181. }
  182. }
  183. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  184. static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
  185. unsigned int r)
  186. {
  187. struct i2c_msg xfer[2];
  188. u8 reg = r;
  189. u16 data;
  190. int ret;
  191. struct i2c_client *client = codec->control_data;
  192. /* Write register */
  193. xfer[0].addr = client->addr;
  194. xfer[0].flags = 0;
  195. xfer[0].len = 1;
  196. xfer[0].buf = &reg;
  197. /* Read data */
  198. xfer[1].addr = client->addr;
  199. xfer[1].flags = I2C_M_RD;
  200. xfer[1].len = 2;
  201. xfer[1].buf = (u8 *)&data;
  202. ret = i2c_transfer(client->adapter, xfer, 2);
  203. if (ret != 2) {
  204. dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
  205. return 0;
  206. }
  207. return (data >> 8) | ((data & 0xff) << 8);
  208. }
  209. #else
  210. #define snd_soc_8_16_read_i2c NULL
  211. #endif
  212. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  213. static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
  214. unsigned int r)
  215. {
  216. struct i2c_msg xfer[2];
  217. u16 reg = r;
  218. u8 data;
  219. int ret;
  220. struct i2c_client *client = codec->control_data;
  221. /* Write register */
  222. xfer[0].addr = client->addr;
  223. xfer[0].flags = 0;
  224. xfer[0].len = 2;
  225. xfer[0].buf = (u8 *)&reg;
  226. /* Read data */
  227. xfer[1].addr = client->addr;
  228. xfer[1].flags = I2C_M_RD;
  229. xfer[1].len = 1;
  230. xfer[1].buf = &data;
  231. ret = i2c_transfer(client->adapter, xfer, 2);
  232. if (ret != 2) {
  233. dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
  234. return 0;
  235. }
  236. return data;
  237. }
  238. #else
  239. #define snd_soc_16_8_read_i2c NULL
  240. #endif
  241. static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
  242. unsigned int reg)
  243. {
  244. u16 *cache = codec->reg_cache;
  245. reg &= 0xff;
  246. if (reg >= codec->reg_cache_size)
  247. return -1;
  248. return cache[reg];
  249. }
  250. static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
  251. unsigned int value)
  252. {
  253. u16 *cache = codec->reg_cache;
  254. u8 data[3];
  255. int ret;
  256. BUG_ON(codec->volatile_register);
  257. data[0] = (reg >> 8) & 0xff;
  258. data[1] = reg & 0xff;
  259. data[2] = value;
  260. reg &= 0xff;
  261. if (reg < codec->reg_cache_size)
  262. cache[reg] = value;
  263. if (codec->cache_only) {
  264. codec->cache_sync = 1;
  265. return 0;
  266. }
  267. ret = codec->hw_write(codec->control_data, data, 3);
  268. if (ret == 3)
  269. return 0;
  270. if (ret < 0)
  271. return ret;
  272. else
  273. return -EIO;
  274. }
  275. #if defined(CONFIG_SPI_MASTER)
  276. static int snd_soc_16_8_spi_write(void *control_data, const char *data,
  277. int len)
  278. {
  279. struct spi_device *spi = control_data;
  280. struct spi_transfer t;
  281. struct spi_message m;
  282. u8 msg[3];
  283. if (len <= 0)
  284. return 0;
  285. msg[0] = data[0];
  286. msg[1] = data[1];
  287. msg[2] = data[2];
  288. spi_message_init(&m);
  289. memset(&t, 0, (sizeof t));
  290. t.tx_buf = &msg[0];
  291. t.len = len;
  292. spi_message_add_tail(&t, &m);
  293. spi_sync(spi, &m);
  294. return len;
  295. }
  296. #else
  297. #define snd_soc_16_8_spi_write NULL
  298. #endif
  299. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  300. static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
  301. unsigned int r)
  302. {
  303. struct i2c_msg xfer[2];
  304. u16 reg = cpu_to_be16(r);
  305. u16 data;
  306. int ret;
  307. struct i2c_client *client = codec->control_data;
  308. /* Write register */
  309. xfer[0].addr = client->addr;
  310. xfer[0].flags = 0;
  311. xfer[0].len = 2;
  312. xfer[0].buf = (u8 *)&reg;
  313. /* Read data */
  314. xfer[1].addr = client->addr;
  315. xfer[1].flags = I2C_M_RD;
  316. xfer[1].len = 2;
  317. xfer[1].buf = (u8 *)&data;
  318. ret = i2c_transfer(client->adapter, xfer, 2);
  319. if (ret != 2) {
  320. dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
  321. return 0;
  322. }
  323. return be16_to_cpu(data);
  324. }
  325. #else
  326. #define snd_soc_16_16_read_i2c NULL
  327. #endif
  328. static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
  329. unsigned int reg)
  330. {
  331. u16 *cache = codec->reg_cache;
  332. if (reg >= codec->reg_cache_size ||
  333. snd_soc_codec_volatile_register(codec, reg)) {
  334. if (codec->cache_only)
  335. return -EINVAL;
  336. return codec->hw_read(codec, reg);
  337. }
  338. return cache[reg];
  339. }
  340. static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
  341. unsigned int value)
  342. {
  343. u16 *cache = codec->reg_cache;
  344. u8 data[4];
  345. int ret;
  346. data[0] = (reg >> 8) & 0xff;
  347. data[1] = reg & 0xff;
  348. data[2] = (value >> 8) & 0xff;
  349. data[3] = value & 0xff;
  350. if (reg < codec->reg_cache_size)
  351. cache[reg] = value;
  352. if (codec->cache_only) {
  353. codec->cache_sync = 1;
  354. return 0;
  355. }
  356. ret = codec->hw_write(codec->control_data, data, 4);
  357. if (ret == 4)
  358. return 0;
  359. if (ret < 0)
  360. return ret;
  361. else
  362. return -EIO;
  363. }
  364. static struct {
  365. int addr_bits;
  366. int data_bits;
  367. int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
  368. int (*spi_write)(void *, const char *, int);
  369. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  370. unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
  371. } io_types[] = {
  372. {
  373. .addr_bits = 4, .data_bits = 12,
  374. .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
  375. .spi_write = snd_soc_4_12_spi_write,
  376. },
  377. {
  378. .addr_bits = 7, .data_bits = 9,
  379. .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
  380. .spi_write = snd_soc_7_9_spi_write,
  381. },
  382. {
  383. .addr_bits = 8, .data_bits = 8,
  384. .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
  385. },
  386. {
  387. .addr_bits = 8, .data_bits = 16,
  388. .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
  389. .i2c_read = snd_soc_8_16_read_i2c,
  390. },
  391. {
  392. .addr_bits = 16, .data_bits = 8,
  393. .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
  394. .i2c_read = snd_soc_16_8_read_i2c,
  395. .spi_write = snd_soc_16_8_spi_write,
  396. },
  397. {
  398. .addr_bits = 16, .data_bits = 16,
  399. .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
  400. .i2c_read = snd_soc_16_16_read_i2c,
  401. },
  402. };
  403. /**
  404. * snd_soc_codec_set_cache_io: Set up standard I/O functions.
  405. *
  406. * @codec: CODEC to configure.
  407. * @type: Type of cache.
  408. * @addr_bits: Number of bits of register address data.
  409. * @data_bits: Number of bits of data per register.
  410. * @control: Control bus used.
  411. *
  412. * Register formats are frequently shared between many I2C and SPI
  413. * devices. In order to promote code reuse the ASoC core provides
  414. * some standard implementations of CODEC read and write operations
  415. * which can be set up using this function.
  416. *
  417. * The caller is responsible for allocating and initialising the
  418. * actual cache.
  419. *
  420. * Note that at present this code cannot be used by CODECs with
  421. * volatile registers.
  422. */
  423. int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
  424. int addr_bits, int data_bits,
  425. enum snd_soc_control_type control)
  426. {
  427. int i;
  428. for (i = 0; i < ARRAY_SIZE(io_types); i++)
  429. if (io_types[i].addr_bits == addr_bits &&
  430. io_types[i].data_bits == data_bits)
  431. break;
  432. if (i == ARRAY_SIZE(io_types)) {
  433. printk(KERN_ERR
  434. "No I/O functions for %d bit address %d bit data\n",
  435. addr_bits, data_bits);
  436. return -EINVAL;
  437. }
  438. codec->write = io_types[i].write;
  439. codec->read = io_types[i].read;
  440. switch (control) {
  441. case SND_SOC_CUSTOM:
  442. break;
  443. case SND_SOC_I2C:
  444. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  445. codec->hw_write = (hw_write_t)i2c_master_send;
  446. #endif
  447. if (io_types[i].i2c_read)
  448. codec->hw_read = io_types[i].i2c_read;
  449. break;
  450. case SND_SOC_SPI:
  451. if (io_types[i].spi_write)
  452. codec->hw_write = io_types[i].spi_write;
  453. break;
  454. }
  455. return 0;
  456. }
  457. EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);