es705-spi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * es705-spi.c -- Audience eS705 SPI interface
  3. *
  4. * Copyright 2011 Audience, Inc.
  5. *
  6. * Author: Hemal Meghpara <hmeghpara@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/init.h>
  14. #include <linux/err.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/firmware.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/delay.h>
  22. #include "es705.h"
  23. #include "es705-spi.h"
  24. static int es705_spi_read(struct es705_priv *es705, void *buf, int len)
  25. {
  26. struct spi_device *spi = es705->spi_client;
  27. int rc;
  28. rc = spi_read(spi, buf, len);
  29. if (rc < 0) {
  30. dev_err(&spi->dev, "%s(): error %d reading SR\n",
  31. __func__, rc);
  32. return rc;
  33. }
  34. return rc;
  35. }
  36. static int es705_spi_write(struct es705_priv *es705, const void *buf, int len)
  37. {
  38. struct spi_device *spi = es705->spi_client;
  39. int rc;
  40. rc = spi_write(spi, buf, len);
  41. if (rc != 0)
  42. dev_err(&spi->dev, "%s(): error %d writing SR\n",
  43. __func__, rc);
  44. return rc;
  45. }
  46. static int es705_spi_write_then_read(struct es705_priv *es705,
  47. const void *buf, int len,
  48. u32 *rspn, int match)
  49. {
  50. int rc;
  51. rc = es705_spi_write(es705, buf, len);
  52. if (!rc)
  53. rc = es705_spi_read(es705, rspn, match);
  54. return rc;
  55. }
  56. static int es705_spi_cmd(struct es705_priv *es705, u32 cmd, int sr, u32 *resp)
  57. {
  58. int err;
  59. u32 rv;
  60. int read_retries = 15;
  61. dev_dbg(es705->dev, "%s(): cmd=0x%08x sr=%i\n", __func__, cmd, sr);
  62. cmd = cpu_to_be32(cmd);
  63. err = es705_spi_write(es705, &cmd, sizeof(cmd));
  64. if (err || sr)
  65. return err;
  66. usleep_range(20000, 20500);
  67. do {
  68. err = es705_spi_read(es705, &rv, sizeof(rv));
  69. dev_dbg(es705->dev, "%s(): spi read err = %d, rv = 0x%08x\n",
  70. __func__, err, be32_to_cpu(rv));
  71. if (err < 0)
  72. break;
  73. usleep_range(1000, 1050);
  74. } while (rv == 0 && read_retries--);
  75. if (rv == 0 && read_retries < 0)
  76. err = -ETIMEDOUT;
  77. if (!err)
  78. *resp = be32_to_cpu(rv);
  79. dev_dbg(es705->dev, "%s(): resp=0x%08x\n", __func__, *resp);
  80. return err;
  81. }
  82. #define ES705_SPI_BOOT_MAX_RETRY 15
  83. static int es705_spi_boot_setup(struct es705_priv *es705)
  84. {
  85. u32 boot_cmd = ES705_BOOT_CMD;
  86. u32 sync_cmd = (ES705_SYNC_CMD << 16) | ES705_SYNC_POLLING;
  87. u32 sbl_rspn = ES705_SBL_ACK;
  88. u32 ack_rspn = ES705_BOOT_ACK;
  89. int match = 1;
  90. int rc;
  91. dev_dbg(es705->dev, "%s(): prepare for fw download\n", __func__);
  92. rc = es705_spi_write_then_read(es705, &sync_cmd, sizeof(sync_cmd),
  93. &sbl_rspn, match);
  94. if (rc) {
  95. dev_err(es705->dev, "%s(): SYNC_SBL fail\n", __func__);
  96. goto es705_spi_boot_setup_failed;
  97. }
  98. es705->mode = SBL;
  99. rc = es705_spi_write_then_read(es705, &boot_cmd, sizeof(boot_cmd),
  100. &ack_rspn, match);
  101. if (rc)
  102. dev_err(es705->dev, "%s(): BOOT_CMD fail\n", __func__);
  103. es705_spi_boot_setup_failed:
  104. return rc;
  105. }
  106. static int es705_spi_boot_finish(struct es705_priv *es705)
  107. {
  108. u32 sync_cmd;
  109. u32 sync_rspn;
  110. int match = 1;
  111. int rc = 0;
  112. dev_dbg(es705->dev, "%s(): finish fw download\n", __func__);
  113. if (es705->es705_power_state == ES705_SET_POWER_STATE_VS_OVERLAY) {
  114. sync_cmd = (ES705_SYNC_CMD << 16) | ES705_SYNC_INTR_RISING_EDGE;
  115. dev_dbg(es705->dev, "%s(): FW type : VOICESENSE\n", __func__);
  116. } else {
  117. sync_cmd = (ES705_SYNC_CMD << 16) | ES705_SYNC_POLLING;
  118. dev_dbg(es705->dev, "%s(): fw type : STANDARD\n", __func__);
  119. }
  120. sync_rspn = sync_cmd;
  121. /* Give the chip some time to become ready after firmware download. */
  122. msleep(20);
  123. /* finish es705 boot, check es705 readiness */
  124. rc = es705_spi_write_then_read(es705, &sync_cmd, sizeof(sync_cmd),
  125. &sync_rspn, match);
  126. if (rc)
  127. dev_err(es705->dev, "%s(): SYNC fail\n", __func__);
  128. return rc;
  129. }
  130. static int __devinit es705_spi_probe(struct spi_device *spi)
  131. {
  132. int rc;
  133. es705_priv.spi_client = spi;
  134. es705_priv.intf = ES705_SPI_INTF;
  135. es705_priv.dev_read = es705_spi_read;
  136. es705_priv.dev_write = es705_spi_write;
  137. es705_priv.dev_write_then_read = es705_spi_write_then_read;
  138. es705_priv.boot_setup = es705_spi_boot_setup;
  139. es705_priv.boot_finish = es705_spi_boot_finish;
  140. es705_priv.cmd = es705_spi_cmd;
  141. dev_info(&spi->dev, "%s()\n", __func__);
  142. es705_priv.streamdev = spi_streamdev;
  143. rc = es705_core_probe(&spi->dev);
  144. if (rc) {
  145. dev_err(&spi->dev, "%s(): es705_core_probe() failed %d\n",
  146. __func__, rc);
  147. goto es705_core_probe_error;
  148. }
  149. rc = es705_bootup(&es705_priv);
  150. if (rc) {
  151. dev_err(&spi->dev, "%s(): es705_bootup failed %d\n",
  152. __func__, rc);
  153. goto bootup_error;
  154. }
  155. return rc;
  156. bootup_error:
  157. es705_core_probe_error:
  158. dev_dbg(&spi->dev, "%s(): exit with error\n", __func__);
  159. return rc;
  160. }
  161. struct es_stream_device spi_streamdev = {
  162. .read = es705_spi_read,
  163. .intf = ES705_SPI_INTF,
  164. };
  165. static int __devexit es705_spi_remove(struct spi_device *spi)
  166. {
  167. snd_soc_unregister_codec(&spi->dev);
  168. return 0;
  169. }
  170. struct spi_driver es705_spi_driver = {
  171. .driver = {
  172. .name = "es705_spi",
  173. .bus = &spi_bus_type,
  174. .owner = THIS_MODULE,
  175. },
  176. /* .id_table = m25p_ids,*/
  177. .probe = es705_spi_probe,
  178. .remove = __devexit_p(es705_spi_remove),
  179. };
  180. static int __init es705_spi_init(void)
  181. {
  182. return 0;
  183. }
  184. static void __exit es705_spi_exit(void)
  185. {
  186. spi_unregister_driver(&es705_spi_driver);
  187. }
  188. module_init(es705_spi_init);
  189. module_exit(es705_spi_exit);
  190. MODULE_LICENSE("GPL v2");
  191. MODULE_AUTHOR("Hemal Meghpara <hmeghpara@audience.com>");
  192. MODULE_DESCRIPTION("es705 SPI driver for es705 ALSA control");
  193. MODULE_ALIAS("platform:es705-codec");