rt5514-spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * rt5514-spi.c -- RT5514 SPI driver
  3. *
  4. * Copyright 2015 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 <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include <sound/soc-dapm.h>
  34. #include <sound/initval.h>
  35. #include <sound/tlv.h>
  36. #include "rt5514-spi.h"
  37. static struct spi_device *rt5514_spi;
  38. struct rt5514_dsp {
  39. struct device *dev;
  40. struct delayed_work copy_work;
  41. struct mutex dma_lock;
  42. struct snd_pcm_substream *substream;
  43. unsigned int buf_base, buf_limit, buf_rp;
  44. size_t buf_size;
  45. size_t dma_offset;
  46. size_t dsp_offset;
  47. };
  48. static const struct snd_pcm_hardware rt5514_spi_pcm_hardware = {
  49. .info = SNDRV_PCM_INFO_MMAP |
  50. SNDRV_PCM_INFO_MMAP_VALID |
  51. SNDRV_PCM_INFO_INTERLEAVED,
  52. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  53. .period_bytes_min = PAGE_SIZE,
  54. .period_bytes_max = 0x20000 / 8,
  55. .periods_min = 8,
  56. .periods_max = 8,
  57. .channels_min = 1,
  58. .channels_max = 1,
  59. .buffer_bytes_max = 0x20000,
  60. };
  61. static struct snd_soc_dai_driver rt5514_spi_dai = {
  62. .name = "rt5514-dsp-cpu-dai",
  63. .id = 0,
  64. .capture = {
  65. .stream_name = "DSP Capture",
  66. .channels_min = 1,
  67. .channels_max = 1,
  68. .rates = SNDRV_PCM_RATE_16000,
  69. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  70. },
  71. };
  72. static void rt5514_spi_copy_work(struct work_struct *work)
  73. {
  74. struct rt5514_dsp *rt5514_dsp =
  75. container_of(work, struct rt5514_dsp, copy_work.work);
  76. struct snd_pcm_runtime *runtime;
  77. size_t period_bytes, truncated_bytes = 0;
  78. mutex_lock(&rt5514_dsp->dma_lock);
  79. if (!rt5514_dsp->substream) {
  80. dev_err(rt5514_dsp->dev, "No pcm substream\n");
  81. goto done;
  82. }
  83. runtime = rt5514_dsp->substream->runtime;
  84. period_bytes = snd_pcm_lib_period_bytes(rt5514_dsp->substream);
  85. if (rt5514_dsp->buf_size - rt5514_dsp->dsp_offset < period_bytes)
  86. period_bytes = rt5514_dsp->buf_size - rt5514_dsp->dsp_offset;
  87. if (rt5514_dsp->buf_rp + period_bytes <= rt5514_dsp->buf_limit) {
  88. rt5514_spi_burst_read(rt5514_dsp->buf_rp,
  89. runtime->dma_area + rt5514_dsp->dma_offset,
  90. period_bytes);
  91. if (rt5514_dsp->buf_rp + period_bytes == rt5514_dsp->buf_limit)
  92. rt5514_dsp->buf_rp = rt5514_dsp->buf_base;
  93. else
  94. rt5514_dsp->buf_rp += period_bytes;
  95. } else {
  96. truncated_bytes = rt5514_dsp->buf_limit - rt5514_dsp->buf_rp;
  97. rt5514_spi_burst_read(rt5514_dsp->buf_rp,
  98. runtime->dma_area + rt5514_dsp->dma_offset,
  99. truncated_bytes);
  100. rt5514_spi_burst_read(rt5514_dsp->buf_base,
  101. runtime->dma_area + rt5514_dsp->dma_offset +
  102. truncated_bytes, period_bytes - truncated_bytes);
  103. rt5514_dsp->buf_rp = rt5514_dsp->buf_base +
  104. period_bytes - truncated_bytes;
  105. }
  106. rt5514_dsp->dma_offset += period_bytes;
  107. if (rt5514_dsp->dma_offset >= runtime->dma_bytes)
  108. rt5514_dsp->dma_offset = 0;
  109. rt5514_dsp->dsp_offset += period_bytes;
  110. snd_pcm_period_elapsed(rt5514_dsp->substream);
  111. if (rt5514_dsp->dsp_offset < rt5514_dsp->buf_size)
  112. schedule_delayed_work(&rt5514_dsp->copy_work, 5);
  113. done:
  114. mutex_unlock(&rt5514_dsp->dma_lock);
  115. }
  116. /* PCM for streaming audio from the DSP buffer */
  117. static int rt5514_spi_pcm_open(struct snd_pcm_substream *substream)
  118. {
  119. snd_soc_set_runtime_hwparams(substream, &rt5514_spi_pcm_hardware);
  120. return 0;
  121. }
  122. static int rt5514_spi_hw_params(struct snd_pcm_substream *substream,
  123. struct snd_pcm_hw_params *hw_params)
  124. {
  125. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  126. struct rt5514_dsp *rt5514_dsp =
  127. snd_soc_platform_get_drvdata(rtd->platform);
  128. int ret;
  129. mutex_lock(&rt5514_dsp->dma_lock);
  130. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  131. params_buffer_bytes(hw_params));
  132. rt5514_dsp->substream = substream;
  133. mutex_unlock(&rt5514_dsp->dma_lock);
  134. return ret;
  135. }
  136. static int rt5514_spi_hw_free(struct snd_pcm_substream *substream)
  137. {
  138. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  139. struct rt5514_dsp *rt5514_dsp =
  140. snd_soc_platform_get_drvdata(rtd->platform);
  141. mutex_lock(&rt5514_dsp->dma_lock);
  142. rt5514_dsp->substream = NULL;
  143. mutex_unlock(&rt5514_dsp->dma_lock);
  144. cancel_delayed_work_sync(&rt5514_dsp->copy_work);
  145. return snd_pcm_lib_free_vmalloc_buffer(substream);
  146. }
  147. static int rt5514_spi_prepare(struct snd_pcm_substream *substream)
  148. {
  149. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  150. struct rt5514_dsp *rt5514_dsp =
  151. snd_soc_platform_get_drvdata(rtd->platform);
  152. u8 buf[8];
  153. rt5514_dsp->dma_offset = 0;
  154. rt5514_dsp->dsp_offset = 0;
  155. /**
  156. * The address area x1800XXXX is the register address, and it cannot
  157. * support spi burst read perfectly. So we use the spi burst read
  158. * individually to make sure the data correctly.
  159. */
  160. rt5514_spi_burst_read(RT5514_BUFFER_VOICE_BASE, (u8 *)&buf,
  161. sizeof(buf));
  162. rt5514_dsp->buf_base = buf[0] | buf[1] << 8 | buf[2] << 16 |
  163. buf[3] << 24;
  164. rt5514_spi_burst_read(RT5514_BUFFER_VOICE_LIMIT, (u8 *)&buf,
  165. sizeof(buf));
  166. rt5514_dsp->buf_limit = buf[0] | buf[1] << 8 | buf[2] << 16 |
  167. buf[3] << 24;
  168. rt5514_spi_burst_read(RT5514_BUFFER_VOICE_RP, (u8 *)&buf,
  169. sizeof(buf));
  170. rt5514_dsp->buf_rp = buf[0] | buf[1] << 8 | buf[2] << 16 |
  171. buf[3] << 24;
  172. rt5514_spi_burst_read(RT5514_BUFFER_VOICE_SIZE, (u8 *)&buf,
  173. sizeof(buf));
  174. rt5514_dsp->buf_size = buf[0] | buf[1] << 8 | buf[2] << 16 |
  175. buf[3] << 24;
  176. return 0;
  177. }
  178. static int rt5514_spi_trigger(struct snd_pcm_substream *substream, int cmd)
  179. {
  180. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  181. struct rt5514_dsp *rt5514_dsp =
  182. snd_soc_platform_get_drvdata(rtd->platform);
  183. if (cmd == SNDRV_PCM_TRIGGER_START) {
  184. if (rt5514_dsp->buf_base && rt5514_dsp->buf_limit &&
  185. rt5514_dsp->buf_rp && rt5514_dsp->buf_size)
  186. schedule_delayed_work(&rt5514_dsp->copy_work, 0);
  187. }
  188. return 0;
  189. }
  190. static snd_pcm_uframes_t rt5514_spi_pcm_pointer(
  191. struct snd_pcm_substream *substream)
  192. {
  193. struct snd_pcm_runtime *runtime = substream->runtime;
  194. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  195. struct rt5514_dsp *rt5514_dsp =
  196. snd_soc_platform_get_drvdata(rtd->platform);
  197. return bytes_to_frames(runtime, rt5514_dsp->dma_offset);
  198. }
  199. static const struct snd_pcm_ops rt5514_spi_pcm_ops = {
  200. .open = rt5514_spi_pcm_open,
  201. .hw_params = rt5514_spi_hw_params,
  202. .hw_free = rt5514_spi_hw_free,
  203. .trigger = rt5514_spi_trigger,
  204. .prepare = rt5514_spi_prepare,
  205. .pointer = rt5514_spi_pcm_pointer,
  206. .mmap = snd_pcm_lib_mmap_vmalloc,
  207. .page = snd_pcm_lib_get_vmalloc_page,
  208. };
  209. static int rt5514_spi_pcm_probe(struct snd_soc_platform *platform)
  210. {
  211. struct rt5514_dsp *rt5514_dsp;
  212. rt5514_dsp = devm_kzalloc(platform->dev, sizeof(*rt5514_dsp),
  213. GFP_KERNEL);
  214. rt5514_dsp->dev = &rt5514_spi->dev;
  215. mutex_init(&rt5514_dsp->dma_lock);
  216. INIT_DELAYED_WORK(&rt5514_dsp->copy_work, rt5514_spi_copy_work);
  217. snd_soc_platform_set_drvdata(platform, rt5514_dsp);
  218. return 0;
  219. }
  220. static struct snd_soc_platform_driver rt5514_spi_platform = {
  221. .probe = rt5514_spi_pcm_probe,
  222. .ops = &rt5514_spi_pcm_ops,
  223. };
  224. static const struct snd_soc_component_driver rt5514_spi_dai_component = {
  225. .name = "rt5514-spi-dai",
  226. };
  227. /**
  228. * rt5514_spi_burst_read - Read data from SPI by rt5514 address.
  229. * @addr: Start address.
  230. * @rxbuf: Data Buffer for reading.
  231. * @len: Data length, it must be a multiple of 8.
  232. *
  233. *
  234. * Returns true for success.
  235. */
  236. int rt5514_spi_burst_read(unsigned int addr, u8 *rxbuf, size_t len)
  237. {
  238. u8 spi_cmd = RT5514_SPI_CMD_BURST_READ;
  239. int status;
  240. u8 write_buf[8];
  241. unsigned int i, end, offset = 0;
  242. struct spi_message message;
  243. struct spi_transfer x[3];
  244. while (offset < len) {
  245. if (offset + RT5514_SPI_BUF_LEN <= len)
  246. end = RT5514_SPI_BUF_LEN;
  247. else
  248. end = len % RT5514_SPI_BUF_LEN;
  249. write_buf[0] = spi_cmd;
  250. write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
  251. write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
  252. write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
  253. write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
  254. spi_message_init(&message);
  255. memset(x, 0, sizeof(x));
  256. x[0].len = 5;
  257. x[0].tx_buf = write_buf;
  258. spi_message_add_tail(&x[0], &message);
  259. x[1].len = 4;
  260. x[1].tx_buf = write_buf;
  261. spi_message_add_tail(&x[1], &message);
  262. x[2].len = end;
  263. x[2].rx_buf = rxbuf + offset;
  264. spi_message_add_tail(&x[2], &message);
  265. status = spi_sync(rt5514_spi, &message);
  266. if (status)
  267. return false;
  268. offset += RT5514_SPI_BUF_LEN;
  269. }
  270. for (i = 0; i < len; i += 8) {
  271. write_buf[0] = rxbuf[i + 0];
  272. write_buf[1] = rxbuf[i + 1];
  273. write_buf[2] = rxbuf[i + 2];
  274. write_buf[3] = rxbuf[i + 3];
  275. write_buf[4] = rxbuf[i + 4];
  276. write_buf[5] = rxbuf[i + 5];
  277. write_buf[6] = rxbuf[i + 6];
  278. write_buf[7] = rxbuf[i + 7];
  279. rxbuf[i + 0] = write_buf[7];
  280. rxbuf[i + 1] = write_buf[6];
  281. rxbuf[i + 2] = write_buf[5];
  282. rxbuf[i + 3] = write_buf[4];
  283. rxbuf[i + 4] = write_buf[3];
  284. rxbuf[i + 5] = write_buf[2];
  285. rxbuf[i + 6] = write_buf[1];
  286. rxbuf[i + 7] = write_buf[0];
  287. }
  288. return true;
  289. }
  290. /**
  291. * rt5514_spi_burst_write - Write data to SPI by rt5514 address.
  292. * @addr: Start address.
  293. * @txbuf: Data Buffer for writng.
  294. * @len: Data length, it must be a multiple of 8.
  295. *
  296. *
  297. * Returns true for success.
  298. */
  299. int rt5514_spi_burst_write(u32 addr, const u8 *txbuf, size_t len)
  300. {
  301. u8 spi_cmd = RT5514_SPI_CMD_BURST_WRITE;
  302. u8 *write_buf;
  303. unsigned int i, end, offset = 0;
  304. write_buf = kmalloc(RT5514_SPI_BUF_LEN + 6, GFP_KERNEL);
  305. if (write_buf == NULL)
  306. return -ENOMEM;
  307. while (offset < len) {
  308. if (offset + RT5514_SPI_BUF_LEN <= len)
  309. end = RT5514_SPI_BUF_LEN;
  310. else
  311. end = len % RT5514_SPI_BUF_LEN;
  312. write_buf[0] = spi_cmd;
  313. write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
  314. write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
  315. write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
  316. write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
  317. for (i = 0; i < end; i += 8) {
  318. write_buf[i + 12] = txbuf[offset + i + 0];
  319. write_buf[i + 11] = txbuf[offset + i + 1];
  320. write_buf[i + 10] = txbuf[offset + i + 2];
  321. write_buf[i + 9] = txbuf[offset + i + 3];
  322. write_buf[i + 8] = txbuf[offset + i + 4];
  323. write_buf[i + 7] = txbuf[offset + i + 5];
  324. write_buf[i + 6] = txbuf[offset + i + 6];
  325. write_buf[i + 5] = txbuf[offset + i + 7];
  326. }
  327. write_buf[end + 5] = spi_cmd;
  328. spi_write(rt5514_spi, write_buf, end + 6);
  329. offset += RT5514_SPI_BUF_LEN;
  330. }
  331. kfree(write_buf);
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(rt5514_spi_burst_write);
  335. static int rt5514_spi_probe(struct spi_device *spi)
  336. {
  337. int ret;
  338. rt5514_spi = spi;
  339. ret = devm_snd_soc_register_platform(&spi->dev, &rt5514_spi_platform);
  340. if (ret < 0) {
  341. dev_err(&spi->dev, "Failed to register platform.\n");
  342. return ret;
  343. }
  344. ret = devm_snd_soc_register_component(&spi->dev,
  345. &rt5514_spi_dai_component,
  346. &rt5514_spi_dai, 1);
  347. if (ret < 0) {
  348. dev_err(&spi->dev, "Failed to register component.\n");
  349. return ret;
  350. }
  351. return 0;
  352. }
  353. static const struct of_device_id rt5514_of_match[] = {
  354. { .compatible = "realtek,rt5514", },
  355. {},
  356. };
  357. MODULE_DEVICE_TABLE(of, rt5514_of_match);
  358. static struct spi_driver rt5514_spi_driver = {
  359. .driver = {
  360. .name = "rt5514",
  361. .of_match_table = of_match_ptr(rt5514_of_match),
  362. },
  363. .probe = rt5514_spi_probe,
  364. };
  365. module_spi_driver(rt5514_spi_driver);
  366. MODULE_DESCRIPTION("RT5514 SPI driver");
  367. MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
  368. MODULE_LICENSE("GPL v2");