rockchip_i2s.c 16 KB

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