rt5677-spi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * rt5677-spi.c -- RT5677 ALSA SoC audio codec driver
  3. *
  4. * Copyright 2013 Realtek Semiconductor Corp.
  5. * Author: Oder Chiou <oder_chiou@realtek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/input.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/slab.h>
  20. #include <linux/gpio.h>
  21. #include <linux/sched.h>
  22. #include <linux/kthread.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/pm_qos.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/clk.h>
  29. #include <linux/firmware.h>
  30. #include "rt5677-spi.h"
  31. #define RT5677_SPI_BURST_LEN 240
  32. #define RT5677_SPI_HEADER 5
  33. #define RT5677_SPI_FREQ 6000000
  34. /* The AddressPhase and DataPhase of SPI commands are MSB first on the wire.
  35. * DataPhase word size of 16-bit commands is 2 bytes.
  36. * DataPhase word size of 32-bit commands is 4 bytes.
  37. * DataPhase word size of burst commands is 8 bytes.
  38. * The DSP CPU is little-endian.
  39. */
  40. #define RT5677_SPI_WRITE_BURST 0x5
  41. #define RT5677_SPI_READ_BURST 0x4
  42. #define RT5677_SPI_WRITE_32 0x3
  43. #define RT5677_SPI_READ_32 0x2
  44. #define RT5677_SPI_WRITE_16 0x1
  45. #define RT5677_SPI_READ_16 0x0
  46. static struct spi_device *g_spi;
  47. static DEFINE_MUTEX(spi_mutex);
  48. /* Select a suitable transfer command for the next transfer to ensure
  49. * the transfer address is always naturally aligned while minimizing
  50. * the total number of transfers required.
  51. *
  52. * 3 transfer commands are available:
  53. * RT5677_SPI_READ/WRITE_16: Transfer 2 bytes
  54. * RT5677_SPI_READ/WRITE_32: Transfer 4 bytes
  55. * RT5677_SPI_READ/WRITE_BURST: Transfer any multiples of 8 bytes
  56. *
  57. * For example, reading 260 bytes at 0x60030002 uses the following commands:
  58. * 0x60030002 RT5677_SPI_READ_16 2 bytes
  59. * 0x60030004 RT5677_SPI_READ_32 4 bytes
  60. * 0x60030008 RT5677_SPI_READ_BURST 240 bytes
  61. * 0x600300F8 RT5677_SPI_READ_BURST 8 bytes
  62. * 0x60030100 RT5677_SPI_READ_32 4 bytes
  63. * 0x60030104 RT5677_SPI_READ_16 2 bytes
  64. *
  65. * Input:
  66. * @read: true for read commands; false for write commands
  67. * @align: alignment of the next transfer address
  68. * @remain: number of bytes remaining to transfer
  69. *
  70. * Output:
  71. * @len: number of bytes to transfer with the selected command
  72. * Returns the selected command
  73. */
  74. static u8 rt5677_spi_select_cmd(bool read, u32 align, u32 remain, u32 *len)
  75. {
  76. u8 cmd;
  77. if (align == 2 || align == 6 || remain == 2) {
  78. cmd = RT5677_SPI_READ_16;
  79. *len = 2;
  80. } else if (align == 4 || remain <= 6) {
  81. cmd = RT5677_SPI_READ_32;
  82. *len = 4;
  83. } else {
  84. cmd = RT5677_SPI_READ_BURST;
  85. *len = min_t(u32, remain & ~7, RT5677_SPI_BURST_LEN);
  86. }
  87. return read ? cmd : cmd + 1;
  88. }
  89. /* Copy dstlen bytes from src to dst, while reversing byte order for each word.
  90. * If srclen < dstlen, zeros are padded.
  91. */
  92. static void rt5677_spi_reverse(u8 *dst, u32 dstlen, const u8 *src, u32 srclen)
  93. {
  94. u32 w, i, si;
  95. u32 word_size = min_t(u32, dstlen, 8);
  96. for (w = 0; w < dstlen; w += word_size) {
  97. for (i = 0; i < word_size; i++) {
  98. si = w + word_size - i - 1;
  99. dst[w + i] = si < srclen ? src[si] : 0;
  100. }
  101. }
  102. }
  103. /* Read DSP address space using SPI. addr and len have to be 2-byte aligned. */
  104. int rt5677_spi_read(u32 addr, void *rxbuf, size_t len)
  105. {
  106. u32 offset;
  107. int status = 0;
  108. struct spi_transfer t[2];
  109. struct spi_message m;
  110. /* +4 bytes is for the DummyPhase following the AddressPhase */
  111. u8 header[RT5677_SPI_HEADER + 4];
  112. u8 body[RT5677_SPI_BURST_LEN];
  113. u8 spi_cmd;
  114. u8 *cb = rxbuf;
  115. if (!g_spi)
  116. return -ENODEV;
  117. if ((addr & 1) || (len & 1)) {
  118. dev_err(&g_spi->dev, "Bad read align 0x%x(%zu)\n", addr, len);
  119. return -EACCES;
  120. }
  121. memset(t, 0, sizeof(t));
  122. t[0].tx_buf = header;
  123. t[0].len = sizeof(header);
  124. t[0].speed_hz = RT5677_SPI_FREQ;
  125. t[1].rx_buf = body;
  126. t[1].speed_hz = RT5677_SPI_FREQ;
  127. spi_message_init_with_transfers(&m, t, ARRAY_SIZE(t));
  128. for (offset = 0; offset < len; offset += t[1].len) {
  129. spi_cmd = rt5677_spi_select_cmd(true, (addr + offset) & 7,
  130. len - offset, &t[1].len);
  131. /* Construct SPI message header */
  132. header[0] = spi_cmd;
  133. header[1] = ((addr + offset) & 0xff000000) >> 24;
  134. header[2] = ((addr + offset) & 0x00ff0000) >> 16;
  135. header[3] = ((addr + offset) & 0x0000ff00) >> 8;
  136. header[4] = ((addr + offset) & 0x000000ff) >> 0;
  137. mutex_lock(&spi_mutex);
  138. status |= spi_sync(g_spi, &m);
  139. mutex_unlock(&spi_mutex);
  140. /* Copy data back to caller buffer */
  141. rt5677_spi_reverse(cb + offset, t[1].len, body, t[1].len);
  142. }
  143. return status;
  144. }
  145. EXPORT_SYMBOL_GPL(rt5677_spi_read);
  146. /* Write DSP address space using SPI. addr has to be 2-byte aligned.
  147. * If len is not 2-byte aligned, an extra byte of zero is written at the end
  148. * as padding.
  149. */
  150. int rt5677_spi_write(u32 addr, const void *txbuf, size_t len)
  151. {
  152. u32 offset, len_with_pad = len;
  153. int status = 0;
  154. struct spi_transfer t;
  155. struct spi_message m;
  156. /* +1 byte is for the DummyPhase following the DataPhase */
  157. u8 buf[RT5677_SPI_HEADER + RT5677_SPI_BURST_LEN + 1];
  158. u8 *body = buf + RT5677_SPI_HEADER;
  159. u8 spi_cmd;
  160. const u8 *cb = txbuf;
  161. if (!g_spi)
  162. return -ENODEV;
  163. if (addr & 1) {
  164. dev_err(&g_spi->dev, "Bad write align 0x%x(%zu)\n", addr, len);
  165. return -EACCES;
  166. }
  167. if (len & 1)
  168. len_with_pad = len + 1;
  169. memset(&t, 0, sizeof(t));
  170. t.tx_buf = buf;
  171. t.speed_hz = RT5677_SPI_FREQ;
  172. spi_message_init_with_transfers(&m, &t, 1);
  173. for (offset = 0; offset < len_with_pad;) {
  174. spi_cmd = rt5677_spi_select_cmd(false, (addr + offset) & 7,
  175. len_with_pad - offset, &t.len);
  176. /* Construct SPI message header */
  177. buf[0] = spi_cmd;
  178. buf[1] = ((addr + offset) & 0xff000000) >> 24;
  179. buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
  180. buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
  181. buf[4] = ((addr + offset) & 0x000000ff) >> 0;
  182. /* Fetch data from caller buffer */
  183. rt5677_spi_reverse(body, t.len, cb + offset, len - offset);
  184. offset += t.len;
  185. t.len += RT5677_SPI_HEADER + 1;
  186. mutex_lock(&spi_mutex);
  187. status |= spi_sync(g_spi, &m);
  188. mutex_unlock(&spi_mutex);
  189. }
  190. return status;
  191. }
  192. EXPORT_SYMBOL_GPL(rt5677_spi_write);
  193. int rt5677_spi_write_firmware(u32 addr, const struct firmware *fw)
  194. {
  195. return rt5677_spi_write(addr, fw->data, fw->size);
  196. }
  197. EXPORT_SYMBOL_GPL(rt5677_spi_write_firmware);
  198. static int rt5677_spi_probe(struct spi_device *spi)
  199. {
  200. g_spi = spi;
  201. return 0;
  202. }
  203. static struct spi_driver rt5677_spi_driver = {
  204. .driver = {
  205. .name = "rt5677",
  206. },
  207. .probe = rt5677_spi_probe,
  208. };
  209. module_spi_driver(rt5677_spi_driver);
  210. MODULE_DESCRIPTION("ASoC RT5677 SPI driver");
  211. MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
  212. MODULE_LICENSE("GPL v2");