escore-i2c.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * escore-i2c.c -- I2C interface for Audience earSmart chips
  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. #define DEBUG
  14. #include "escore.h"
  15. #include "escore-i2c.h"
  16. #include "es-d202.h"
  17. int escore_i2c_read(struct escore_priv *escore, void *buf, int len)
  18. {
  19. struct i2c_msg msg[] = {
  20. {
  21. .addr = escore->i2c_client->addr,
  22. .flags = I2C_M_RD,
  23. .len = len,
  24. .buf = buf,
  25. },
  26. };
  27. int rc = 0;
  28. rc = i2c_transfer(escore->i2c_client->adapter, msg, 1);
  29. /*
  30. * i2c_transfer returns number of messages executed. Since we
  31. * are always sending only 1 msg, return value should be 1 for
  32. * success case
  33. */
  34. if (rc != 1) {
  35. pr_err("%s(): i2c_transfer() failed, rc = %d, msg_len = %d\n",
  36. __func__, rc, len);
  37. return -EIO;
  38. } else {
  39. return 0;
  40. }
  41. }
  42. int escore_i2c_write(struct escore_priv *escore, const void *buf, int len)
  43. {
  44. struct i2c_msg msg;
  45. int max_xfer_len = ES_MAX_I2C_XFER_LEN;
  46. int rc = 0, written = 0, xfer_len;
  47. msg.addr = escore->i2c_client->addr;
  48. msg.flags = 0;
  49. while (written < len) {
  50. xfer_len = min(len - written, max_xfer_len);
  51. msg.len = xfer_len;
  52. msg.buf = (void *)(buf + written);
  53. rc = i2c_transfer(escore->i2c_client->adapter, &msg, 1);
  54. if (rc != 1) {
  55. pr_err("%s(): i2c_transfer() failed, rc:%d\n",
  56. __func__, rc);
  57. return -EIO;
  58. }
  59. written += xfer_len;
  60. }
  61. return 0;
  62. }
  63. int escore_i2c_cmd(struct escore_priv *escore, u32 cmd, int sr, u32 *resp)
  64. {
  65. int err;
  66. u32 rv;
  67. pr_info("escore: cmd=0x%08x sr=%i\n", cmd, sr);
  68. cmd = cpu_to_be32(cmd);
  69. err = escore_i2c_write(escore, &cmd, sizeof(cmd));
  70. if (err || sr)
  71. return err;
  72. /* The response must be actively read. Maximum response time
  73. * is 20ms.
  74. */
  75. msleep(20);
  76. err = escore_i2c_read(escore, &rv, sizeof(rv));
  77. if (!err)
  78. *resp = be32_to_cpu(rv);
  79. pr_info("core: %s resp=0x%08x\n", __func__, *resp);
  80. return err;
  81. }
  82. int escore_i2c_boot_setup(struct escore_priv *escore)
  83. {
  84. u16 boot_cmd = ES_I2C_BOOT_CMD;
  85. u16 boot_ack = 0;
  86. char msg[2];
  87. int rc;
  88. /* Reset Board */
  89. gpio_set_value(escore->pdata->reset_gpio, 0);
  90. msleep(20);
  91. gpio_set_value(escore->pdata->reset_gpio, 1);
  92. msleep(20);
  93. pr_info("%s()\n", __func__);
  94. pr_info("%s(): write ES_BOOT_CMD = 0x%04x\n", __func__, boot_cmd);
  95. cpu_to_be16s(&boot_cmd);
  96. memcpy(msg, (char *)&boot_cmd, 2);
  97. rc = escore->dev_write(escore, msg, 2);
  98. if (rc < 0) {
  99. pr_err("%s(): firmware load failed boot write\n", __func__);
  100. goto escore_bootup_failed;
  101. }
  102. usleep_range(1000, 1000);
  103. memset(msg, 0, 2);
  104. rc = escore->dev_read(escore, msg, 2);
  105. if (rc < 0) {
  106. pr_err("%s(): firmware load failed boot ack\n", __func__);
  107. goto escore_bootup_failed;
  108. }
  109. memcpy((char *)&boot_ack, msg, 2);
  110. pr_info("%s(): boot_ack = 0x%04x\n", __func__, boot_ack);
  111. if (boot_ack != ES_I2C_BOOT_ACK) {
  112. pr_err("%s(): firmware load failed boot ack pattern", __func__);
  113. rc = -EIO;
  114. goto escore_bootup_failed;
  115. }
  116. escore_bootup_failed:
  117. return rc;
  118. }
  119. static int escore_i2c_probe(struct i2c_client *i2c,
  120. const struct i2c_device_id *id)
  121. {
  122. struct esxxx_platform_data *pdata = i2c->dev.platform_data;
  123. int rc;
  124. dev_dbg(&i2c->dev, "%s() i2c->name = %s\n", __func__, i2c->name);
  125. escore_priv.i2c_client = i2c;
  126. if (pdata == NULL) {
  127. dev_err(&i2c->dev, "%s(): pdata is NULL", __func__);
  128. rc = -EIO;
  129. goto pdata_error;
  130. }
  131. i2c_set_clientdata(i2c, &escore_priv);
  132. escore_priv.intf = ES_I2C_INTF;
  133. escore_priv.dev_read = escore_i2c_read;
  134. escore_priv.dev_write = escore_i2c_write;
  135. escore_priv.boot_setup = escore_i2c_boot_setup;
  136. escore_priv.cmd = escore_i2c_cmd;
  137. escore_priv.dev = &i2c->dev;
  138. escore_priv.streamdev = es_i2c_streamdev;
  139. rc = escore_priv.probe(&i2c->dev);
  140. if (rc) {
  141. dev_err(&i2c->dev, "%s(): escore_core_probe() failed %d\n",
  142. __func__, rc);
  143. goto escore_core_probe_error;
  144. }
  145. rc = escore_priv.bootup(&escore_priv);
  146. if (rc) {
  147. dev_err(&i2c->dev, "%s(): escore_bootup failed %d\n",
  148. __func__, rc);
  149. goto bootup_error;
  150. }
  151. release_firmware(escore_priv.fw);
  152. rc = snd_soc_register_codec(&i2c->dev,
  153. escore_priv.soc_codec_dev_escore,
  154. escore_priv.escore_dai,
  155. escore_priv.escore_dai_nr);
  156. dev_dbg(&i2c->dev, "%s(): rc = snd_soc_regsiter_codec() = %d\n",
  157. __func__, rc);
  158. es_d202_fill_cmdcache(escore_priv.codec);
  159. return rc;
  160. bootup_error:
  161. escore_core_probe_error:
  162. pdata_error:
  163. dev_dbg(&i2c->dev, "%s(): exit with error\n", __func__);
  164. return rc;
  165. }
  166. static int escore_i2c_remove(struct i2c_client *i2c)
  167. {
  168. struct esxxx_platform_data *pdata = i2c->dev.platform_data;
  169. gpio_free(pdata->reset_gpio);
  170. gpio_free(pdata->wakeup_gpio);
  171. gpio_free(pdata->gpioa_gpio);
  172. snd_soc_unregister_codec(&i2c->dev);
  173. kfree(i2c_get_clientdata(i2c));
  174. return 0;
  175. }
  176. static int escore_i2c_suspend(struct device *dev)
  177. {
  178. return 0;
  179. }
  180. static int escore_i2c_resume(struct device *dev)
  181. {
  182. return 0;
  183. }
  184. static int escore_i2c_runtime_suspend(struct device *dev)
  185. {
  186. return 0;
  187. }
  188. static int escore_i2c_runtime_resume(struct device *dev)
  189. {
  190. return 0;
  191. }
  192. static int escore_i2c_runtime_idle(struct device *dev)
  193. {
  194. return 0;
  195. }
  196. struct es_stream_device es_i2c_streamdev = {
  197. .read = escore_i2c_read,
  198. .intf = ES_I2C_INTF,
  199. };
  200. int escore_i2c_init(void)
  201. {
  202. int rc;
  203. rc = i2c_add_driver(&escore_i2c_driver);
  204. if (!rc)
  205. pr_info("%s() registered as I2C", __func__);
  206. else
  207. pr_err("%s(): i2c_add_driver failed, rc = %d", __func__, rc);
  208. return rc;
  209. }
  210. static const struct dev_pm_ops escore_i2c_dev_pm_ops = {
  211. SET_SYSTEM_SLEEP_PM_OPS(
  212. escore_i2c_suspend,
  213. escore_i2c_resume
  214. )
  215. SET_RUNTIME_PM_OPS(
  216. escore_i2c_runtime_suspend,
  217. escore_i2c_runtime_resume,
  218. escore_i2c_runtime_idle
  219. )
  220. };
  221. static const struct i2c_device_id escore_i2c_id[] = {
  222. { "earSmart", 0},
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(i2c, escore_i2c_id);
  226. struct i2c_driver escore_i2c_driver = {
  227. .driver = {
  228. .name = "earSmart-codec",
  229. .owner = THIS_MODULE,
  230. .pm = &escore_i2c_dev_pm_ops,
  231. },
  232. .probe = escore_i2c_probe,
  233. .remove = escore_i2c_remove,
  234. .id_table = escore_i2c_id,
  235. };
  236. MODULE_DESCRIPTION("Audience earSmart I2C core driver");
  237. MODULE_AUTHOR("Greg Clemson <gclemson@audience.com>");
  238. MODULE_LICENSE("GPL");
  239. MODULE_ALIAS("platform:earSmart-codec");