es705-i2c.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * es705-i2c.c -- Audience eS705 I2C interface
  3. *
  4. * Copyright 2011 Audience, Inc.
  5. *
  6. * Author: Greg Clemson <gclemson@audience.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/firmware.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/completion.h>
  20. #include <linux/i2c.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/err.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <sound/initval.h>
  30. #include <sound/tlv.h>
  31. #include <linux/kthread.h>
  32. #include <linux/esxxx.h>
  33. #include "es705.h"
  34. #include "es705-platform.h"
  35. #include "es705-i2c.h"
  36. static int es705_i2c_read(struct es705_priv *es705, void *buf, int len)
  37. {
  38. struct i2c_msg msg[] = {
  39. {
  40. .addr = es705->i2c_client->addr,
  41. .flags = I2C_M_RD,
  42. .len = len,
  43. .buf = buf,
  44. },
  45. };
  46. int rc = 0;
  47. rc = i2c_transfer(es705->i2c_client->adapter, msg, 1);
  48. /*
  49. * i2c_transfer returns number of messages executed. Since we
  50. * are always sending only 1 msg, return value should be 1 for
  51. * success case
  52. */
  53. if (rc != 1) {
  54. pr_err("%s(): i2c_transfer() failed, rc = %d, msg_len = %d\n",
  55. __func__, rc, len);
  56. return -EIO;
  57. } else {
  58. return 0;
  59. }
  60. }
  61. #define I2C_BUF_SIZE 512
  62. static int es705_i2c_write(struct es705_priv *es705, const void *buf, int len)
  63. {
  64. struct i2c_msg msg[] = {
  65. {
  66. .addr = es705->i2c_client->addr,
  67. .flags = 0,
  68. },
  69. };
  70. int rc = 0;
  71. int pos = 0;
  72. while (pos < len) {
  73. msg[0].len = min(len - pos, I2C_BUF_SIZE);
  74. /*
  75. * The function i2c_master_send() indicates we
  76. * can trust the i2c bus interface not to
  77. * write in this buffer.
  78. */
  79. msg[0].buf = (void *)(buf + pos);
  80. rc = i2c_transfer(es705->i2c_client->adapter, msg, 1);
  81. if (rc != 1) {
  82. dev_err(es705->dev, "%s(): i2c_transfer() failed, rc = %d, msg_len = %d\n",
  83. __func__, rc, len);
  84. return -EIO;
  85. }
  86. pos += msg[0].len;
  87. }
  88. return 0;
  89. }
  90. static int es705_i2c_write_then_read(struct es705_priv *es705,
  91. const void *buf, int len,
  92. u32 *rspn, int match)
  93. {
  94. int rc;
  95. rc = es705_i2c_write(es705, buf, len);
  96. if (!rc)
  97. rc = es705_i2c_read(es705, rspn, match);
  98. return rc;
  99. }
  100. static int es705_i2c_cmd(struct es705_priv *es705, u32 cmd, int sr, u32 *resp)
  101. {
  102. int err;
  103. u32 rv;
  104. dev_dbg(es705->dev, "%s(): cmd=0x%08x sr=%i\n", __func__, cmd, sr);
  105. cmd = cpu_to_be32(cmd);
  106. err = es705_i2c_write(es705, &cmd, sizeof(cmd));
  107. if (err || sr)
  108. return err;
  109. /* The response must be actively read. Maximum response time
  110. * is 10ms.
  111. */
  112. usleep_range(10000, 10500);
  113. err = es705_i2c_read(es705, &rv, sizeof(rv));
  114. if (!err)
  115. *resp = be32_to_cpu(rv);
  116. dev_dbg(es705->dev, "%s(): resp=0x%08x\n", __func__, *resp);
  117. return err;
  118. }
  119. static int es705_i2c_boot_setup(struct es705_priv *es705)
  120. {
  121. u16 boot_cmd = ES705_I2C_BOOT_CMD;
  122. u16 boot_ack = 0;
  123. char msg[2];
  124. int rc;
  125. dev_dbg(es705->dev, "%s(): write ES705_BOOT_CMD = 0x%04x\n",
  126. __func__, boot_cmd);
  127. cpu_to_be16s(&boot_cmd);
  128. memcpy(msg, (char *)&boot_cmd, 2);
  129. rc = es705->dev_write(es705, msg, 2);
  130. if (rc < 0) {
  131. dev_err(es705->dev, "%s(): firmware load failed boot write\n",
  132. __func__);
  133. goto es705_boot_setup_failed;
  134. }
  135. usleep_range(1000, 1000);
  136. memset(msg, 0, 2);
  137. rc = es705->dev_read(es705, msg, 2);
  138. if (rc < 0) {
  139. dev_err(es705->dev, "%s(): firmware load failed boot ack\n",
  140. __func__);
  141. goto es705_boot_setup_failed;
  142. }
  143. memcpy((char *)&boot_ack, msg, 2);
  144. dev_dbg(es705->dev, "%s(): boot_ack = 0x%04x\n", __func__, boot_ack);
  145. if (boot_ack != ES705_I2C_BOOT_ACK) {
  146. dev_err(es705->dev, "%s(): firmware load failed boot ack pattern",
  147. __func__);
  148. rc = -EIO;
  149. goto es705_boot_setup_failed;
  150. }
  151. es705_boot_setup_failed:
  152. return rc;
  153. }
  154. static int es705_i2c_boot_finish(struct es705_priv *es705)
  155. {
  156. u32 sync_cmd;
  157. u32 sync_rspn;
  158. int match = 1;
  159. int rc = 0;
  160. dev_dbg(es705->dev, "%s(): finish fw download\n", __func__);
  161. if (es705->es705_power_state == ES705_SET_POWER_STATE_VS_OVERLAY) {
  162. sync_cmd = (ES705_SYNC_CMD << 16) | ES705_SYNC_INTR_RISING_EDGE;
  163. dev_dbg(es705->dev, "%s(): FW type : VOICESENSE\n", __func__);
  164. } else {
  165. sync_cmd = (ES705_SYNC_CMD << 16) | ES705_SYNC_POLLING;
  166. dev_dbg(es705->dev, "%s(): fw type : STANDARD\n", __func__);
  167. }
  168. sync_rspn = sync_cmd;
  169. /* Give the chip some time to become ready after firmware download. */
  170. msleep(20);
  171. /* finish es705 boot, check es705 readiness */
  172. rc = es705_i2c_write_then_read(es705, &sync_cmd, sizeof(sync_cmd),
  173. &sync_rspn, match);
  174. if (rc)
  175. dev_err(es705->dev, "%s(): SYNC fail\n", __func__);
  176. return rc;
  177. }
  178. static int es705_i2c_probe(struct i2c_client *i2c,
  179. const struct i2c_device_id *id)
  180. {
  181. struct esxxx_platform_data *pdata = i2c->dev.platform_data;
  182. int rc;
  183. dev_dbg(&i2c->dev, "%s(): i2c->name = %s\n", __func__, i2c->name);
  184. es705_priv.i2c_client = i2c;
  185. if (pdata == NULL) {
  186. dev_err(&i2c->dev, "%s(): pdata is NULL", __func__);
  187. rc = -EIO;
  188. goto pdata_error;
  189. }
  190. i2c_set_clientdata(i2c, &es705_priv);
  191. es705_priv.intf = ES705_I2C_INTF;
  192. es705_priv.dev_read = es705_i2c_read;
  193. es705_priv.dev_write = es705_i2c_write;
  194. es705_priv.dev_write_then_read = es705_i2c_write_then_read;
  195. es705_priv.boot_setup = es705_i2c_boot_setup;
  196. es705_priv.boot_finish = es705_i2c_boot_finish;
  197. es705_priv.cmd = es705_i2c_cmd;
  198. es705_priv.dev = &i2c->dev;
  199. es705_priv.streamdev = i2c_streamdev;
  200. rc = es705_core_probe(&i2c->dev);
  201. if (rc) {
  202. dev_err(&i2c->dev, "%s(): es705_core_probe() failed %d\n",
  203. __func__, rc);
  204. goto es705_core_probe_error;
  205. }
  206. rc = es705_bootup(&es705_priv);
  207. if (rc) {
  208. dev_err(&i2c->dev, "%s(): es705_bootup failed %d\n",
  209. __func__, rc);
  210. goto bootup_error;
  211. }
  212. rc = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_es705, es705_dai,
  213. ES705_NUM_CODEC_DAIS);
  214. dev_dbg(&i2c->dev, "%s(): rc = snd_soc_regsiter_codec() = %d\n", __func__, rc);
  215. return rc;
  216. bootup_error:
  217. es705_core_probe_error:
  218. pdata_error:
  219. dev_dbg(&i2c->dev, "%s(): exit with error\n", __func__);
  220. return rc;
  221. }
  222. static int es705_i2c_remove(struct i2c_client *i2c)
  223. {
  224. struct esxxx_platform_data *pdata = i2c->dev.platform_data;
  225. es705_gpio_free(pdata);
  226. snd_soc_unregister_codec(&i2c->dev);
  227. kfree(i2c_get_clientdata(i2c));
  228. return 0;
  229. }
  230. struct es_stream_device i2c_streamdev = {
  231. .read = es705_i2c_read,
  232. .intf = ES705_I2C_INTF,
  233. };
  234. int es705_i2c_init(struct es705_priv *es705)
  235. {
  236. int rc;
  237. rc = i2c_add_driver(&es705_i2c_driver);
  238. if (!rc) {
  239. dev_dbg(es705->dev, "%s(): registered as I2C", __func__);
  240. es705_priv.intf = ES705_I2C_INTF;
  241. /*
  242. es705_priv.device_read = ;
  243. es705_priv.device_write = ;
  244. */
  245. }
  246. else
  247. dev_err(es705->dev, "%s(): i2c_add_driver failed, rc = %d",
  248. __func__, rc);
  249. return rc;
  250. }
  251. static const struct i2c_device_id es705_i2c_id[] = {
  252. { "es705", 0},
  253. { }
  254. };
  255. MODULE_DEVICE_TABLE(i2c, es705_i2c_id);
  256. struct i2c_driver es705_i2c_driver = {
  257. .driver = {
  258. .name = "es705-codec",
  259. .owner = THIS_MODULE,
  260. },
  261. .probe = es705_i2c_probe,
  262. .remove = es705_i2c_remove,
  263. .id_table = es705_i2c_id,
  264. };
  265. MODULE_DESCRIPTION("ASoC ES705 driver");
  266. MODULE_AUTHOR("Greg Clemson <gclemson@audience.com>");
  267. MODULE_LICENSE("GPL v2");
  268. MODULE_ALIAS("platform:es705-codec");