soc-cache.c 12 KB

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