rockchip_i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /* sound/soc/rockchip/rockchip_i2s.c
  2. *
  3. * ALSA SoC Audio Layer - Rockchip I2S Controller driver
  4. *
  5. * Copyright (c) 2014 Rockchip Electronics Co. Ltd.
  6. * Author: Jianqun <jay.xu@rock-chips.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/clk.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/regmap.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/dmaengine_pcm.h>
  20. #include "rockchip_i2s.h"
  21. #define DRV_NAME "rockchip-i2s"
  22. struct rk_i2s_dev {
  23. struct device *dev;
  24. struct clk *hclk;
  25. struct clk *mclk;
  26. struct snd_dmaengine_dai_dma_data capture_dma_data;
  27. struct snd_dmaengine_dai_dma_data playback_dma_data;
  28. struct regmap *regmap;
  29. /*
  30. * Used to indicate the tx/rx status.
  31. * I2S controller hopes to start the tx and rx together,
  32. * also to stop them when they are both try to stop.
  33. */
  34. bool tx_start;
  35. bool rx_start;
  36. };
  37. static int i2s_runtime_suspend(struct device *dev)
  38. {
  39. struct rk_i2s_dev *i2s = dev_get_drvdata(dev);
  40. clk_disable_unprepare(i2s->mclk);
  41. return 0;
  42. }
  43. static int i2s_runtime_resume(struct device *dev)
  44. {
  45. struct rk_i2s_dev *i2s = dev_get_drvdata(dev);
  46. int ret;
  47. ret = clk_prepare_enable(i2s->mclk);
  48. if (ret) {
  49. dev_err(i2s->dev, "clock enable failed %d\n", ret);
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. static inline struct rk_i2s_dev *to_info(struct snd_soc_dai *dai)
  55. {
  56. return snd_soc_dai_get_drvdata(dai);
  57. }
  58. static void rockchip_snd_txctrl(struct rk_i2s_dev *i2s, int on)
  59. {
  60. unsigned int val = 0;
  61. int retry = 10;
  62. if (on) {
  63. regmap_update_bits(i2s->regmap, I2S_DMACR,
  64. I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_ENABLE);
  65. regmap_update_bits(i2s->regmap, I2S_XFER,
  66. I2S_XFER_TXS_START | I2S_XFER_RXS_START,
  67. I2S_XFER_TXS_START | I2S_XFER_RXS_START);
  68. i2s->tx_start = true;
  69. } else {
  70. i2s->tx_start = false;
  71. regmap_update_bits(i2s->regmap, I2S_DMACR,
  72. I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_DISABLE);
  73. if (!i2s->rx_start) {
  74. regmap_update_bits(i2s->regmap, I2S_XFER,
  75. I2S_XFER_TXS_START |
  76. I2S_XFER_RXS_START,
  77. I2S_XFER_TXS_STOP |
  78. I2S_XFER_RXS_STOP);
  79. regmap_update_bits(i2s->regmap, I2S_CLR,
  80. I2S_CLR_TXC | I2S_CLR_RXC,
  81. I2S_CLR_TXC | I2S_CLR_RXC);
  82. regmap_read(i2s->regmap, I2S_CLR, &val);
  83. /* Should wait for clear operation to finish */
  84. while (val) {
  85. regmap_read(i2s->regmap, I2S_CLR, &val);
  86. retry--;
  87. if (!retry) {
  88. dev_warn(i2s->dev, "fail to clear\n");
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. static void rockchip_snd_rxctrl(struct rk_i2s_dev *i2s, int on)
  96. {
  97. unsigned int val = 0;
  98. int retry = 10;
  99. if (on) {
  100. regmap_update_bits(i2s->regmap, I2S_DMACR,
  101. I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_ENABLE);
  102. regmap_update_bits(i2s->regmap, I2S_XFER,
  103. I2S_XFER_TXS_START | I2S_XFER_RXS_START,
  104. I2S_XFER_TXS_START | I2S_XFER_RXS_START);
  105. i2s->rx_start = true;
  106. } else {
  107. i2s->rx_start = false;
  108. regmap_update_bits(i2s->regmap, I2S_DMACR,
  109. I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_DISABLE);
  110. if (!i2s->tx_start) {
  111. regmap_update_bits(i2s->regmap, I2S_XFER,
  112. I2S_XFER_TXS_START |
  113. I2S_XFER_RXS_START,
  114. I2S_XFER_TXS_STOP |
  115. I2S_XFER_RXS_STOP);
  116. regmap_update_bits(i2s->regmap, I2S_CLR,
  117. I2S_CLR_TXC | I2S_CLR_RXC,
  118. I2S_CLR_TXC | I2S_CLR_RXC);
  119. regmap_read(i2s->regmap, I2S_CLR, &val);
  120. /* Should wait for clear operation to finish */
  121. while (val) {
  122. regmap_read(i2s->regmap, I2S_CLR, &val);
  123. retry--;
  124. if (!retry) {
  125. dev_warn(i2s->dev, "fail to clear\n");
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. static int rockchip_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
  133. unsigned int fmt)
  134. {
  135. struct rk_i2s_dev *i2s = to_info(cpu_dai);
  136. unsigned int mask = 0, val = 0;
  137. mask = I2S_CKR_MSS_MASK;
  138. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  139. case SND_SOC_DAIFMT_CBS_CFS:
  140. /* Set source clock in Master mode */
  141. val = I2S_CKR_MSS_MASTER;
  142. break;
  143. case SND_SOC_DAIFMT_CBM_CFM:
  144. val = I2S_CKR_MSS_SLAVE;
  145. break;
  146. default:
  147. return -EINVAL;
  148. }
  149. regmap_update_bits(i2s->regmap, I2S_CKR, mask, val);
  150. mask = I2S_TXCR_IBM_MASK;
  151. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  152. case SND_SOC_DAIFMT_RIGHT_J:
  153. val = I2S_TXCR_IBM_RSJM;
  154. break;
  155. case SND_SOC_DAIFMT_LEFT_J:
  156. val = I2S_TXCR_IBM_LSJM;
  157. break;
  158. case SND_SOC_DAIFMT_I2S:
  159. val = I2S_TXCR_IBM_NORMAL;
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. regmap_update_bits(i2s->regmap, I2S_TXCR, mask, val);
  165. mask = I2S_RXCR_IBM_MASK;
  166. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  167. case SND_SOC_DAIFMT_RIGHT_J:
  168. val = I2S_RXCR_IBM_RSJM;
  169. break;
  170. case SND_SOC_DAIFMT_LEFT_J:
  171. val = I2S_RXCR_IBM_LSJM;
  172. break;
  173. case SND_SOC_DAIFMT_I2S:
  174. val = I2S_RXCR_IBM_NORMAL;
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. regmap_update_bits(i2s->regmap, I2S_RXCR, mask, val);
  180. return 0;
  181. }
  182. static int rockchip_i2s_hw_params(struct snd_pcm_substream *substream,
  183. struct snd_pcm_hw_params *params,
  184. struct snd_soc_dai *dai)
  185. {
  186. struct rk_i2s_dev *i2s = to_info(dai);
  187. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  188. unsigned int val = 0;
  189. switch (params_format(params)) {
  190. case SNDRV_PCM_FORMAT_S8:
  191. val |= I2S_TXCR_VDW(8);
  192. break;
  193. case SNDRV_PCM_FORMAT_S16_LE:
  194. val |= I2S_TXCR_VDW(16);
  195. break;
  196. case SNDRV_PCM_FORMAT_S20_3LE:
  197. val |= I2S_TXCR_VDW(20);
  198. break;
  199. case SNDRV_PCM_FORMAT_S24_LE:
  200. val |= I2S_TXCR_VDW(24);
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. switch (params_channels(params)) {
  206. case 8:
  207. val |= I2S_CHN_8;
  208. break;
  209. case 6:
  210. val |= I2S_CHN_6;
  211. break;
  212. case 4:
  213. val |= I2S_CHN_4;
  214. break;
  215. case 2:
  216. val |= I2S_CHN_2;
  217. break;
  218. default:
  219. dev_err(i2s->dev, "invalid channel: %d\n",
  220. params_channels(params));
  221. return -EINVAL;
  222. }
  223. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  224. regmap_update_bits(i2s->regmap, I2S_RXCR,
  225. I2S_RXCR_VDW_MASK | I2S_RXCR_CSR_MASK,
  226. val);
  227. else
  228. regmap_update_bits(i2s->regmap, I2S_TXCR,
  229. I2S_TXCR_VDW_MASK | I2S_TXCR_CSR_MASK,
  230. val);
  231. regmap_update_bits(i2s->regmap, I2S_DMACR, I2S_DMACR_TDL_MASK,
  232. I2S_DMACR_TDL(16));
  233. regmap_update_bits(i2s->regmap, I2S_DMACR, I2S_DMACR_RDL_MASK,
  234. I2S_DMACR_RDL(16));
  235. val = I2S_CKR_TRCM_TXRX;
  236. if (dai->driver->symmetric_rates || rtd->dai_link->symmetric_rates)
  237. val = I2S_CKR_TRCM_TXSHARE;
  238. regmap_update_bits(i2s->regmap, I2S_CKR,
  239. I2S_CKR_TRCM_MASK,
  240. val);
  241. return 0;
  242. }
  243. static int rockchip_i2s_trigger(struct snd_pcm_substream *substream,
  244. int cmd, struct snd_soc_dai *dai)
  245. {
  246. struct rk_i2s_dev *i2s = to_info(dai);
  247. int ret = 0;
  248. switch (cmd) {
  249. case SNDRV_PCM_TRIGGER_START:
  250. case SNDRV_PCM_TRIGGER_RESUME:
  251. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  252. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  253. rockchip_snd_rxctrl(i2s, 1);
  254. else
  255. rockchip_snd_txctrl(i2s, 1);
  256. break;
  257. case SNDRV_PCM_TRIGGER_SUSPEND:
  258. case SNDRV_PCM_TRIGGER_STOP:
  259. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  260. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  261. rockchip_snd_rxctrl(i2s, 0);
  262. else
  263. rockchip_snd_txctrl(i2s, 0);
  264. break;
  265. default:
  266. ret = -EINVAL;
  267. break;
  268. }
  269. return ret;
  270. }
  271. static int rockchip_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
  272. unsigned int freq, int dir)
  273. {
  274. struct rk_i2s_dev *i2s = to_info(cpu_dai);
  275. int ret;
  276. ret = clk_set_rate(i2s->mclk, freq);
  277. if (ret)
  278. dev_err(i2s->dev, "Fail to set mclk %d\n", ret);
  279. return ret;
  280. }
  281. static int rockchip_i2s_dai_probe(struct snd_soc_dai *dai)
  282. {
  283. struct rk_i2s_dev *i2s = snd_soc_dai_get_drvdata(dai);
  284. dai->capture_dma_data = &i2s->capture_dma_data;
  285. dai->playback_dma_data = &i2s->playback_dma_data;
  286. return 0;
  287. }
  288. static const struct snd_soc_dai_ops rockchip_i2s_dai_ops = {
  289. .hw_params = rockchip_i2s_hw_params,
  290. .set_sysclk = rockchip_i2s_set_sysclk,
  291. .set_fmt = rockchip_i2s_set_fmt,
  292. .trigger = rockchip_i2s_trigger,
  293. };
  294. static struct snd_soc_dai_driver rockchip_i2s_dai = {
  295. .probe = rockchip_i2s_dai_probe,
  296. .playback = {
  297. .stream_name = "Playback",
  298. .channels_min = 2,
  299. .channels_max = 8,
  300. .rates = SNDRV_PCM_RATE_8000_192000,
  301. .formats = (SNDRV_PCM_FMTBIT_S8 |
  302. SNDRV_PCM_FMTBIT_S16_LE |
  303. SNDRV_PCM_FMTBIT_S20_3LE |
  304. SNDRV_PCM_FMTBIT_S24_LE),
  305. },
  306. .capture = {
  307. .stream_name = "Capture",
  308. .channels_min = 2,
  309. .channels_max = 2,
  310. .rates = SNDRV_PCM_RATE_8000_192000,
  311. .formats = (SNDRV_PCM_FMTBIT_S8 |
  312. SNDRV_PCM_FMTBIT_S16_LE |
  313. SNDRV_PCM_FMTBIT_S20_3LE |
  314. SNDRV_PCM_FMTBIT_S24_LE),
  315. },
  316. .ops = &rockchip_i2s_dai_ops,
  317. .symmetric_rates = 1,
  318. };
  319. static const struct snd_soc_component_driver rockchip_i2s_component = {
  320. .name = DRV_NAME,
  321. };
  322. static bool rockchip_i2s_wr_reg(struct device *dev, unsigned int reg)
  323. {
  324. switch (reg) {
  325. case I2S_TXCR:
  326. case I2S_RXCR:
  327. case I2S_CKR:
  328. case I2S_DMACR:
  329. case I2S_INTCR:
  330. case I2S_XFER:
  331. case I2S_CLR:
  332. case I2S_TXDR:
  333. return true;
  334. default:
  335. return false;
  336. }
  337. }
  338. static bool rockchip_i2s_rd_reg(struct device *dev, unsigned int reg)
  339. {
  340. switch (reg) {
  341. case I2S_TXCR:
  342. case I2S_RXCR:
  343. case I2S_CKR:
  344. case I2S_DMACR:
  345. case I2S_INTCR:
  346. case I2S_XFER:
  347. case I2S_CLR:
  348. case I2S_RXDR:
  349. case I2S_FIFOLR:
  350. case I2S_INTSR:
  351. return true;
  352. default:
  353. return false;
  354. }
  355. }
  356. static bool rockchip_i2s_volatile_reg(struct device *dev, unsigned int reg)
  357. {
  358. switch (reg) {
  359. case I2S_INTSR:
  360. case I2S_CLR:
  361. return true;
  362. default:
  363. return false;
  364. }
  365. }
  366. static bool rockchip_i2s_precious_reg(struct device *dev, unsigned int reg)
  367. {
  368. switch (reg) {
  369. default:
  370. return false;
  371. }
  372. }
  373. static const struct regmap_config rockchip_i2s_regmap_config = {
  374. .reg_bits = 32,
  375. .reg_stride = 4,
  376. .val_bits = 32,
  377. .max_register = I2S_RXDR,
  378. .writeable_reg = rockchip_i2s_wr_reg,
  379. .readable_reg = rockchip_i2s_rd_reg,
  380. .volatile_reg = rockchip_i2s_volatile_reg,
  381. .precious_reg = rockchip_i2s_precious_reg,
  382. .cache_type = REGCACHE_FLAT,
  383. };
  384. static int rockchip_i2s_probe(struct platform_device *pdev)
  385. {
  386. struct device_node *node = pdev->dev.of_node;
  387. struct rk_i2s_dev *i2s;
  388. struct resource *res;
  389. void __iomem *regs;
  390. int ret;
  391. int val;
  392. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  393. if (!i2s) {
  394. dev_err(&pdev->dev, "Can't allocate rk_i2s_dev\n");
  395. return -ENOMEM;
  396. }
  397. /* try to prepare related clocks */
  398. i2s->hclk = devm_clk_get(&pdev->dev, "i2s_hclk");
  399. if (IS_ERR(i2s->hclk)) {
  400. dev_err(&pdev->dev, "Can't retrieve i2s bus clock\n");
  401. return PTR_ERR(i2s->hclk);
  402. }
  403. ret = clk_prepare_enable(i2s->hclk);
  404. if (ret) {
  405. dev_err(i2s->dev, "hclock enable failed %d\n", ret);
  406. return ret;
  407. }
  408. i2s->mclk = devm_clk_get(&pdev->dev, "i2s_clk");
  409. if (IS_ERR(i2s->mclk)) {
  410. dev_err(&pdev->dev, "Can't retrieve i2s master clock\n");
  411. return PTR_ERR(i2s->mclk);
  412. }
  413. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  414. regs = devm_ioremap_resource(&pdev->dev, res);
  415. if (IS_ERR(regs))
  416. return PTR_ERR(regs);
  417. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  418. &rockchip_i2s_regmap_config);
  419. if (IS_ERR(i2s->regmap)) {
  420. dev_err(&pdev->dev,
  421. "Failed to initialise managed register map\n");
  422. return PTR_ERR(i2s->regmap);
  423. }
  424. i2s->playback_dma_data.addr = res->start + I2S_TXDR;
  425. i2s->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  426. i2s->playback_dma_data.maxburst = 4;
  427. i2s->capture_dma_data.addr = res->start + I2S_RXDR;
  428. i2s->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  429. i2s->capture_dma_data.maxburst = 4;
  430. i2s->dev = &pdev->dev;
  431. dev_set_drvdata(&pdev->dev, i2s);
  432. pm_runtime_enable(&pdev->dev);
  433. if (!pm_runtime_enabled(&pdev->dev)) {
  434. ret = i2s_runtime_resume(&pdev->dev);
  435. if (ret)
  436. goto err_pm_disable;
  437. }
  438. /* refine capture channels */
  439. if (!of_property_read_u32(node, "rockchip,capture-channels", &val)) {
  440. if (val >= 2 && val <= 8)
  441. rockchip_i2s_dai.capture.channels_max = val;
  442. else
  443. rockchip_i2s_dai.capture.channels_max = 2;
  444. }
  445. ret = devm_snd_soc_register_component(&pdev->dev,
  446. &rockchip_i2s_component,
  447. &rockchip_i2s_dai, 1);
  448. if (ret) {
  449. dev_err(&pdev->dev, "Could not register DAI\n");
  450. goto err_suspend;
  451. }
  452. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  453. if (ret) {
  454. dev_err(&pdev->dev, "Could not register PCM\n");
  455. return ret;
  456. }
  457. return 0;
  458. err_suspend:
  459. if (!pm_runtime_status_suspended(&pdev->dev))
  460. i2s_runtime_suspend(&pdev->dev);
  461. err_pm_disable:
  462. pm_runtime_disable(&pdev->dev);
  463. return ret;
  464. }
  465. static int rockchip_i2s_remove(struct platform_device *pdev)
  466. {
  467. struct rk_i2s_dev *i2s = dev_get_drvdata(&pdev->dev);
  468. pm_runtime_disable(&pdev->dev);
  469. if (!pm_runtime_status_suspended(&pdev->dev))
  470. i2s_runtime_suspend(&pdev->dev);
  471. clk_disable_unprepare(i2s->mclk);
  472. clk_disable_unprepare(i2s->hclk);
  473. return 0;
  474. }
  475. static const struct of_device_id rockchip_i2s_match[] = {
  476. { .compatible = "rockchip,rk3066-i2s", },
  477. {},
  478. };
  479. static const struct dev_pm_ops rockchip_i2s_pm_ops = {
  480. SET_RUNTIME_PM_OPS(i2s_runtime_suspend, i2s_runtime_resume,
  481. NULL)
  482. };
  483. static struct platform_driver rockchip_i2s_driver = {
  484. .probe = rockchip_i2s_probe,
  485. .remove = rockchip_i2s_remove,
  486. .driver = {
  487. .name = DRV_NAME,
  488. .of_match_table = of_match_ptr(rockchip_i2s_match),
  489. .pm = &rockchip_i2s_pm_ops,
  490. },
  491. };
  492. module_platform_driver(rockchip_i2s_driver);
  493. MODULE_DESCRIPTION("ROCKCHIP IIS ASoC Interface");
  494. MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
  495. MODULE_LICENSE("GPL v2");
  496. MODULE_ALIAS("platform:" DRV_NAME);
  497. MODULE_DEVICE_TABLE(of, rockchip_i2s_match);