soc-cache.c 12 KB

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