sirf-usp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * SiRF USP in I2S/DSP mode
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/io.h>
  10. #include <linux/of.h>
  11. #include <linux/clk.h>
  12. #include <linux/pm_runtime.h>
  13. #include <sound/soc.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/dmaengine_pcm.h>
  16. #include "sirf-usp.h"
  17. struct sirf_usp {
  18. struct regmap *regmap;
  19. struct clk *clk;
  20. u32 mode1_reg;
  21. u32 mode2_reg;
  22. int daifmt_format;
  23. struct snd_dmaengine_dai_dma_data playback_dma_data;
  24. struct snd_dmaengine_dai_dma_data capture_dma_data;
  25. };
  26. static void sirf_usp_tx_enable(struct sirf_usp *usp)
  27. {
  28. regmap_update_bits(usp->regmap, USP_TX_FIFO_OP,
  29. USP_TX_FIFO_RESET, USP_TX_FIFO_RESET);
  30. regmap_write(usp->regmap, USP_TX_FIFO_OP, 0);
  31. regmap_update_bits(usp->regmap, USP_TX_FIFO_OP,
  32. USP_TX_FIFO_START, USP_TX_FIFO_START);
  33. regmap_update_bits(usp->regmap, USP_TX_RX_ENABLE,
  34. USP_TX_ENA, USP_TX_ENA);
  35. }
  36. static void sirf_usp_tx_disable(struct sirf_usp *usp)
  37. {
  38. regmap_update_bits(usp->regmap, USP_TX_RX_ENABLE,
  39. USP_TX_ENA, ~USP_TX_ENA);
  40. /* FIFO stop */
  41. regmap_write(usp->regmap, USP_TX_FIFO_OP, 0);
  42. }
  43. static void sirf_usp_rx_enable(struct sirf_usp *usp)
  44. {
  45. regmap_update_bits(usp->regmap, USP_RX_FIFO_OP,
  46. USP_RX_FIFO_RESET, USP_RX_FIFO_RESET);
  47. regmap_write(usp->regmap, USP_RX_FIFO_OP, 0);
  48. regmap_update_bits(usp->regmap, USP_RX_FIFO_OP,
  49. USP_RX_FIFO_START, USP_RX_FIFO_START);
  50. regmap_update_bits(usp->regmap, USP_TX_RX_ENABLE,
  51. USP_RX_ENA, USP_RX_ENA);
  52. }
  53. static void sirf_usp_rx_disable(struct sirf_usp *usp)
  54. {
  55. regmap_update_bits(usp->regmap, USP_TX_RX_ENABLE,
  56. USP_RX_ENA, ~USP_RX_ENA);
  57. /* FIFO stop */
  58. regmap_write(usp->regmap, USP_RX_FIFO_OP, 0);
  59. }
  60. static int sirf_usp_pcm_dai_probe(struct snd_soc_dai *dai)
  61. {
  62. struct sirf_usp *usp = snd_soc_dai_get_drvdata(dai);
  63. snd_soc_dai_init_dma_data(dai, &usp->playback_dma_data,
  64. &usp->capture_dma_data);
  65. return 0;
  66. }
  67. static int sirf_usp_pcm_set_dai_fmt(struct snd_soc_dai *dai,
  68. unsigned int fmt)
  69. {
  70. struct sirf_usp *usp = snd_soc_dai_get_drvdata(dai);
  71. /* set master/slave audio interface */
  72. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  73. case SND_SOC_DAIFMT_CBM_CFM:
  74. break;
  75. default:
  76. dev_err(dai->dev, "Only CBM and CFM supported\n");
  77. return -EINVAL;
  78. }
  79. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  80. case SND_SOC_DAIFMT_I2S:
  81. case SND_SOC_DAIFMT_DSP_A:
  82. usp->daifmt_format = (fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  83. break;
  84. default:
  85. dev_err(dai->dev, "Only I2S and DSP_A format supported\n");
  86. return -EINVAL;
  87. }
  88. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  89. case SND_SOC_DAIFMT_NB_NF:
  90. break;
  91. case SND_SOC_DAIFMT_IB_NF:
  92. usp->daifmt_format |= (fmt & SND_SOC_DAIFMT_INV_MASK);
  93. break;
  94. default:
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. static void sirf_usp_i2s_init(struct sirf_usp *usp)
  100. {
  101. /* Configure RISC mode */
  102. regmap_update_bits(usp->regmap, USP_RISC_DSP_MODE,
  103. USP_RISC_DSP_SEL, ~USP_RISC_DSP_SEL);
  104. /*
  105. * Configure DMA IO Length register
  106. * Set no limit, USP can receive data continuously until it is diabled
  107. */
  108. regmap_write(usp->regmap, USP_TX_DMA_IO_LEN, 0);
  109. regmap_write(usp->regmap, USP_RX_DMA_IO_LEN, 0);
  110. /* Configure Mode2 register */
  111. regmap_write(usp->regmap, USP_MODE2, (1 << USP_RXD_DELAY_LEN_OFFSET) |
  112. (0 << USP_TXD_DELAY_LEN_OFFSET) |
  113. USP_TFS_CLK_SLAVE_MODE | USP_RFS_CLK_SLAVE_MODE);
  114. /* Configure Mode1 register */
  115. regmap_write(usp->regmap, USP_MODE1,
  116. USP_SYNC_MODE | USP_EN | USP_TXD_ACT_EDGE_FALLING |
  117. USP_RFS_ACT_LEVEL_LOGIC1 | USP_TFS_ACT_LEVEL_LOGIC1 |
  118. USP_TX_UFLOW_REPEAT_ZERO | USP_CLOCK_MODE_SLAVE);
  119. /* Configure RX DMA IO Control register */
  120. regmap_write(usp->regmap, USP_RX_DMA_IO_CTRL, 0);
  121. /* Congiure RX FIFO Control register */
  122. regmap_write(usp->regmap, USP_RX_FIFO_CTRL,
  123. (USP_RX_FIFO_THRESHOLD << USP_RX_FIFO_THD_OFFSET) |
  124. (USP_TX_RX_FIFO_WIDTH_DWORD << USP_RX_FIFO_WIDTH_OFFSET));
  125. /* Congiure RX FIFO Level Check register */
  126. regmap_write(usp->regmap, USP_RX_FIFO_LEVEL_CHK,
  127. RX_FIFO_SC(0x04) | RX_FIFO_LC(0x0E) | RX_FIFO_HC(0x1B));
  128. /* Configure TX DMA IO Control register*/
  129. regmap_write(usp->regmap, USP_TX_DMA_IO_CTRL, 0);
  130. /* Configure TX FIFO Control register */
  131. regmap_write(usp->regmap, USP_TX_FIFO_CTRL,
  132. (USP_TX_FIFO_THRESHOLD << USP_TX_FIFO_THD_OFFSET) |
  133. (USP_TX_RX_FIFO_WIDTH_DWORD << USP_TX_FIFO_WIDTH_OFFSET));
  134. /* Congiure TX FIFO Level Check register */
  135. regmap_write(usp->regmap, USP_TX_FIFO_LEVEL_CHK,
  136. TX_FIFO_SC(0x1B) | TX_FIFO_LC(0x0E) | TX_FIFO_HC(0x04));
  137. }
  138. static int sirf_usp_pcm_hw_params(struct snd_pcm_substream *substream,
  139. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  140. {
  141. struct sirf_usp *usp = snd_soc_dai_get_drvdata(dai);
  142. u32 data_len, frame_len, shifter_len;
  143. switch (params_format(params)) {
  144. case SNDRV_PCM_FORMAT_S16_LE:
  145. data_len = 16;
  146. frame_len = 16;
  147. break;
  148. case SNDRV_PCM_FORMAT_S24_LE:
  149. data_len = 24;
  150. frame_len = 32;
  151. break;
  152. case SNDRV_PCM_FORMAT_S24_3LE:
  153. data_len = 24;
  154. frame_len = 24;
  155. break;
  156. default:
  157. dev_err(dai->dev, "Format unsupported\n");
  158. return -EINVAL;
  159. }
  160. shifter_len = data_len;
  161. switch (usp->daifmt_format & SND_SOC_DAIFMT_FORMAT_MASK) {
  162. case SND_SOC_DAIFMT_I2S:
  163. regmap_update_bits(usp->regmap, USP_RX_FRAME_CTRL,
  164. USP_I2S_SYNC_CHG, USP_I2S_SYNC_CHG);
  165. break;
  166. case SND_SOC_DAIFMT_DSP_A:
  167. regmap_update_bits(usp->regmap, USP_RX_FRAME_CTRL,
  168. USP_I2S_SYNC_CHG, 0);
  169. frame_len = data_len * params_channels(params);
  170. data_len = frame_len;
  171. break;
  172. default:
  173. dev_err(dai->dev, "Only support I2S and DSP_A mode\n");
  174. return -EINVAL;
  175. }
  176. switch (usp->daifmt_format & SND_SOC_DAIFMT_INV_MASK) {
  177. case SND_SOC_DAIFMT_NB_NF:
  178. break;
  179. case SND_SOC_DAIFMT_IB_NF:
  180. regmap_update_bits(usp->regmap, USP_MODE1,
  181. USP_RXD_ACT_EDGE_FALLING | USP_TXD_ACT_EDGE_FALLING,
  182. USP_RXD_ACT_EDGE_FALLING);
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  188. regmap_update_bits(usp->regmap, USP_TX_FRAME_CTRL,
  189. USP_TXC_DATA_LEN_MASK | USP_TXC_FRAME_LEN_MASK
  190. | USP_TXC_SHIFTER_LEN_MASK | USP_TXC_SLAVE_CLK_SAMPLE,
  191. ((data_len - 1) << USP_TXC_DATA_LEN_OFFSET)
  192. | ((frame_len - 1) << USP_TXC_FRAME_LEN_OFFSET)
  193. | ((shifter_len - 1) << USP_TXC_SHIFTER_LEN_OFFSET)
  194. | USP_TXC_SLAVE_CLK_SAMPLE);
  195. else
  196. regmap_update_bits(usp->regmap, USP_RX_FRAME_CTRL,
  197. USP_RXC_DATA_LEN_MASK | USP_RXC_FRAME_LEN_MASK
  198. | USP_RXC_SHIFTER_LEN_MASK | USP_SINGLE_SYNC_MODE,
  199. ((data_len - 1) << USP_RXC_DATA_LEN_OFFSET)
  200. | ((frame_len - 1) << USP_RXC_FRAME_LEN_OFFSET)
  201. | ((shifter_len - 1) << USP_RXC_SHIFTER_LEN_OFFSET)
  202. | USP_SINGLE_SYNC_MODE);
  203. return 0;
  204. }
  205. static int sirf_usp_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
  206. struct snd_soc_dai *dai)
  207. {
  208. struct sirf_usp *usp = snd_soc_dai_get_drvdata(dai);
  209. switch (cmd) {
  210. case SNDRV_PCM_TRIGGER_START:
  211. case SNDRV_PCM_TRIGGER_RESUME:
  212. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  213. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  214. sirf_usp_tx_enable(usp);
  215. else
  216. sirf_usp_rx_enable(usp);
  217. break;
  218. case SNDRV_PCM_TRIGGER_STOP:
  219. case SNDRV_PCM_TRIGGER_SUSPEND:
  220. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  221. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  222. sirf_usp_tx_disable(usp);
  223. else
  224. sirf_usp_rx_disable(usp);
  225. break;
  226. }
  227. return 0;
  228. }
  229. static const struct snd_soc_dai_ops sirf_usp_pcm_dai_ops = {
  230. .trigger = sirf_usp_pcm_trigger,
  231. .set_fmt = sirf_usp_pcm_set_dai_fmt,
  232. .hw_params = sirf_usp_pcm_hw_params,
  233. };
  234. static struct snd_soc_dai_driver sirf_usp_pcm_dai = {
  235. .probe = sirf_usp_pcm_dai_probe,
  236. .name = "sirf-usp-pcm",
  237. .id = 0,
  238. .playback = {
  239. .stream_name = "SiRF USP PCM Playback",
  240. .channels_min = 1,
  241. .channels_max = 2,
  242. .rates = SNDRV_PCM_RATE_8000_192000,
  243. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  244. SNDRV_PCM_FMTBIT_S24_3LE,
  245. },
  246. .capture = {
  247. .stream_name = "SiRF USP PCM Capture",
  248. .channels_min = 1,
  249. .channels_max = 2,
  250. .rates = SNDRV_PCM_RATE_8000_192000,
  251. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  252. SNDRV_PCM_FMTBIT_S24_3LE,
  253. },
  254. .ops = &sirf_usp_pcm_dai_ops,
  255. };
  256. static int sirf_usp_pcm_runtime_suspend(struct device *dev)
  257. {
  258. struct sirf_usp *usp = dev_get_drvdata(dev);
  259. clk_disable_unprepare(usp->clk);
  260. return 0;
  261. }
  262. static int sirf_usp_pcm_runtime_resume(struct device *dev)
  263. {
  264. struct sirf_usp *usp = dev_get_drvdata(dev);
  265. int ret;
  266. ret = clk_prepare_enable(usp->clk);
  267. if (ret) {
  268. dev_err(dev, "clk_enable failed: %d\n", ret);
  269. return ret;
  270. }
  271. sirf_usp_i2s_init(usp);
  272. return 0;
  273. }
  274. #ifdef CONFIG_PM_SLEEP
  275. static int sirf_usp_pcm_suspend(struct device *dev)
  276. {
  277. struct sirf_usp *usp = dev_get_drvdata(dev);
  278. if (!pm_runtime_status_suspended(dev)) {
  279. regmap_read(usp->regmap, USP_MODE1, &usp->mode1_reg);
  280. regmap_read(usp->regmap, USP_MODE2, &usp->mode2_reg);
  281. sirf_usp_pcm_runtime_suspend(dev);
  282. }
  283. return 0;
  284. }
  285. static int sirf_usp_pcm_resume(struct device *dev)
  286. {
  287. struct sirf_usp *usp = dev_get_drvdata(dev);
  288. int ret;
  289. if (!pm_runtime_status_suspended(dev)) {
  290. ret = sirf_usp_pcm_runtime_resume(dev);
  291. if (ret)
  292. return ret;
  293. regmap_write(usp->regmap, USP_MODE1, usp->mode1_reg);
  294. regmap_write(usp->regmap, USP_MODE2, usp->mode2_reg);
  295. }
  296. return 0;
  297. }
  298. #endif
  299. static const struct snd_soc_component_driver sirf_usp_component = {
  300. .name = "sirf-usp",
  301. };
  302. static const struct regmap_config sirf_usp_regmap_config = {
  303. .reg_bits = 32,
  304. .reg_stride = 4,
  305. .val_bits = 32,
  306. .max_register = USP_RX_FIFO_DATA,
  307. .cache_type = REGCACHE_NONE,
  308. };
  309. static int sirf_usp_pcm_probe(struct platform_device *pdev)
  310. {
  311. int ret;
  312. struct sirf_usp *usp;
  313. void __iomem *base;
  314. struct resource *mem_res;
  315. usp = devm_kzalloc(&pdev->dev, sizeof(struct sirf_usp),
  316. GFP_KERNEL);
  317. if (!usp)
  318. return -ENOMEM;
  319. platform_set_drvdata(pdev, usp);
  320. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  321. base = devm_ioremap(&pdev->dev, mem_res->start,
  322. resource_size(mem_res));
  323. if (base == NULL)
  324. return -ENOMEM;
  325. usp->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  326. &sirf_usp_regmap_config);
  327. if (IS_ERR(usp->regmap))
  328. return PTR_ERR(usp->regmap);
  329. usp->clk = devm_clk_get(&pdev->dev, NULL);
  330. if (IS_ERR(usp->clk)) {
  331. dev_err(&pdev->dev, "Get clock failed.\n");
  332. return PTR_ERR(usp->clk);
  333. }
  334. pm_runtime_enable(&pdev->dev);
  335. if (!pm_runtime_enabled(&pdev->dev)) {
  336. ret = sirf_usp_pcm_runtime_resume(&pdev->dev);
  337. if (ret)
  338. return ret;
  339. }
  340. ret = devm_snd_soc_register_component(&pdev->dev, &sirf_usp_component,
  341. &sirf_usp_pcm_dai, 1);
  342. if (ret) {
  343. dev_err(&pdev->dev, "Register Audio SoC dai failed.\n");
  344. return ret;
  345. }
  346. return devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  347. }
  348. static int sirf_usp_pcm_remove(struct platform_device *pdev)
  349. {
  350. if (!pm_runtime_enabled(&pdev->dev))
  351. sirf_usp_pcm_runtime_suspend(&pdev->dev);
  352. else
  353. pm_runtime_disable(&pdev->dev);
  354. return 0;
  355. }
  356. static const struct of_device_id sirf_usp_pcm_of_match[] = {
  357. { .compatible = "sirf,prima2-usp-pcm", },
  358. {}
  359. };
  360. MODULE_DEVICE_TABLE(of, sirf_usp_pcm_of_match);
  361. static const struct dev_pm_ops sirf_usp_pcm_pm_ops = {
  362. SET_RUNTIME_PM_OPS(sirf_usp_pcm_runtime_suspend,
  363. sirf_usp_pcm_runtime_resume, NULL)
  364. SET_SYSTEM_SLEEP_PM_OPS(sirf_usp_pcm_suspend, sirf_usp_pcm_resume)
  365. };
  366. static struct platform_driver sirf_usp_pcm_driver = {
  367. .driver = {
  368. .name = "sirf-usp-pcm",
  369. .of_match_table = sirf_usp_pcm_of_match,
  370. .pm = &sirf_usp_pcm_pm_ops,
  371. },
  372. .probe = sirf_usp_pcm_probe,
  373. .remove = sirf_usp_pcm_remove,
  374. };
  375. module_platform_driver(sirf_usp_pcm_driver);
  376. MODULE_DESCRIPTION("SiRF SoC USP PCM bus driver");
  377. MODULE_AUTHOR("RongJun Ying <Rongjun.Ying@csr.com>");
  378. MODULE_LICENSE("GPL v2");