rockchip_spdif.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* sound/soc/rockchip/rk_spdif.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. * Copyright (c) 2015 Collabora Ltd.
  8. * Author: Sjoerd Simons <sjoerd.simons@collabora.co.uk>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/clk.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/regmap.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/dmaengine_pcm.h>
  23. #include "rockchip_spdif.h"
  24. enum rk_spdif_type {
  25. RK_SPDIF_RK3066,
  26. RK_SPDIF_RK3188,
  27. RK_SPDIF_RK3288,
  28. RK_SPDIF_RK3366,
  29. };
  30. #define RK3288_GRF_SOC_CON2 0x24c
  31. struct rk_spdif_dev {
  32. struct device *dev;
  33. struct clk *mclk;
  34. struct clk *hclk;
  35. struct snd_dmaengine_dai_dma_data playback_dma_data;
  36. struct regmap *regmap;
  37. };
  38. static const struct of_device_id rk_spdif_match[] = {
  39. { .compatible = "rockchip,rk3066-spdif",
  40. .data = (void *)RK_SPDIF_RK3066 },
  41. { .compatible = "rockchip,rk3188-spdif",
  42. .data = (void *)RK_SPDIF_RK3188 },
  43. { .compatible = "rockchip,rk3288-spdif",
  44. .data = (void *)RK_SPDIF_RK3288 },
  45. { .compatible = "rockchip,rk3366-spdif",
  46. .data = (void *)RK_SPDIF_RK3366 },
  47. { .compatible = "rockchip,rk3368-spdif",
  48. .data = (void *)RK_SPDIF_RK3366 },
  49. { .compatible = "rockchip,rk3399-spdif",
  50. .data = (void *)RK_SPDIF_RK3366 },
  51. {},
  52. };
  53. MODULE_DEVICE_TABLE(of, rk_spdif_match);
  54. static int __maybe_unused rk_spdif_runtime_suspend(struct device *dev)
  55. {
  56. struct rk_spdif_dev *spdif = dev_get_drvdata(dev);
  57. regcache_cache_only(spdif->regmap, true);
  58. clk_disable_unprepare(spdif->mclk);
  59. clk_disable_unprepare(spdif->hclk);
  60. return 0;
  61. }
  62. static int __maybe_unused rk_spdif_runtime_resume(struct device *dev)
  63. {
  64. struct rk_spdif_dev *spdif = dev_get_drvdata(dev);
  65. int ret;
  66. ret = clk_prepare_enable(spdif->mclk);
  67. if (ret) {
  68. dev_err(spdif->dev, "mclk clock enable failed %d\n", ret);
  69. return ret;
  70. }
  71. ret = clk_prepare_enable(spdif->hclk);
  72. if (ret) {
  73. dev_err(spdif->dev, "hclk clock enable failed %d\n", ret);
  74. return ret;
  75. }
  76. regcache_cache_only(spdif->regmap, false);
  77. regcache_mark_dirty(spdif->regmap);
  78. ret = regcache_sync(spdif->regmap);
  79. if (ret) {
  80. clk_disable_unprepare(spdif->mclk);
  81. clk_disable_unprepare(spdif->hclk);
  82. }
  83. return ret;
  84. }
  85. static int rk_spdif_hw_params(struct snd_pcm_substream *substream,
  86. struct snd_pcm_hw_params *params,
  87. struct snd_soc_dai *dai)
  88. {
  89. struct rk_spdif_dev *spdif = snd_soc_dai_get_drvdata(dai);
  90. unsigned int val = SPDIF_CFGR_HALFWORD_ENABLE;
  91. int srate, mclk;
  92. int ret;
  93. srate = params_rate(params);
  94. mclk = srate * 128;
  95. switch (params_format(params)) {
  96. case SNDRV_PCM_FORMAT_S16_LE:
  97. val |= SPDIF_CFGR_VDW_16;
  98. break;
  99. case SNDRV_PCM_FORMAT_S20_3LE:
  100. val |= SPDIF_CFGR_VDW_20;
  101. break;
  102. case SNDRV_PCM_FORMAT_S24_LE:
  103. val |= SPDIF_CFGR_VDW_24;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. /* Set clock and calculate divider */
  109. ret = clk_set_rate(spdif->mclk, mclk);
  110. if (ret != 0) {
  111. dev_err(spdif->dev, "Failed to set module clock rate: %d\n",
  112. ret);
  113. return ret;
  114. }
  115. ret = regmap_update_bits(spdif->regmap, SPDIF_CFGR,
  116. SPDIF_CFGR_CLK_DIV_MASK | SPDIF_CFGR_HALFWORD_ENABLE |
  117. SDPIF_CFGR_VDW_MASK,
  118. val);
  119. return ret;
  120. }
  121. static int rk_spdif_trigger(struct snd_pcm_substream *substream,
  122. int cmd, struct snd_soc_dai *dai)
  123. {
  124. struct rk_spdif_dev *spdif = snd_soc_dai_get_drvdata(dai);
  125. int ret;
  126. switch (cmd) {
  127. case SNDRV_PCM_TRIGGER_START:
  128. case SNDRV_PCM_TRIGGER_RESUME:
  129. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  130. ret = regmap_update_bits(spdif->regmap, SPDIF_DMACR,
  131. SPDIF_DMACR_TDE_ENABLE |
  132. SPDIF_DMACR_TDL_MASK,
  133. SPDIF_DMACR_TDE_ENABLE |
  134. SPDIF_DMACR_TDL(16));
  135. if (ret != 0)
  136. return ret;
  137. ret = regmap_update_bits(spdif->regmap, SPDIF_XFER,
  138. SPDIF_XFER_TXS_START,
  139. SPDIF_XFER_TXS_START);
  140. break;
  141. case SNDRV_PCM_TRIGGER_SUSPEND:
  142. case SNDRV_PCM_TRIGGER_STOP:
  143. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  144. ret = regmap_update_bits(spdif->regmap, SPDIF_DMACR,
  145. SPDIF_DMACR_TDE_ENABLE,
  146. SPDIF_DMACR_TDE_DISABLE);
  147. if (ret != 0)
  148. return ret;
  149. ret = regmap_update_bits(spdif->regmap, SPDIF_XFER,
  150. SPDIF_XFER_TXS_START,
  151. SPDIF_XFER_TXS_STOP);
  152. break;
  153. default:
  154. ret = -EINVAL;
  155. break;
  156. }
  157. return ret;
  158. }
  159. static int rk_spdif_dai_probe(struct snd_soc_dai *dai)
  160. {
  161. struct rk_spdif_dev *spdif = snd_soc_dai_get_drvdata(dai);
  162. dai->playback_dma_data = &spdif->playback_dma_data;
  163. return 0;
  164. }
  165. static const struct snd_soc_dai_ops rk_spdif_dai_ops = {
  166. .hw_params = rk_spdif_hw_params,
  167. .trigger = rk_spdif_trigger,
  168. };
  169. static struct snd_soc_dai_driver rk_spdif_dai = {
  170. .probe = rk_spdif_dai_probe,
  171. .playback = {
  172. .stream_name = "Playback",
  173. .channels_min = 2,
  174. .channels_max = 2,
  175. .rates = (SNDRV_PCM_RATE_32000 |
  176. SNDRV_PCM_RATE_44100 |
  177. SNDRV_PCM_RATE_48000 |
  178. SNDRV_PCM_RATE_96000 |
  179. SNDRV_PCM_RATE_192000),
  180. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  181. SNDRV_PCM_FMTBIT_S20_3LE |
  182. SNDRV_PCM_FMTBIT_S24_LE),
  183. },
  184. .ops = &rk_spdif_dai_ops,
  185. };
  186. static const struct snd_soc_component_driver rk_spdif_component = {
  187. .name = "rockchip-spdif",
  188. };
  189. static bool rk_spdif_wr_reg(struct device *dev, unsigned int reg)
  190. {
  191. switch (reg) {
  192. case SPDIF_CFGR:
  193. case SPDIF_DMACR:
  194. case SPDIF_INTCR:
  195. case SPDIF_XFER:
  196. case SPDIF_SMPDR:
  197. return true;
  198. default:
  199. return false;
  200. }
  201. }
  202. static bool rk_spdif_rd_reg(struct device *dev, unsigned int reg)
  203. {
  204. switch (reg) {
  205. case SPDIF_CFGR:
  206. case SPDIF_SDBLR:
  207. case SPDIF_INTCR:
  208. case SPDIF_INTSR:
  209. case SPDIF_XFER:
  210. return true;
  211. default:
  212. return false;
  213. }
  214. }
  215. static bool rk_spdif_volatile_reg(struct device *dev, unsigned int reg)
  216. {
  217. switch (reg) {
  218. case SPDIF_INTSR:
  219. case SPDIF_SDBLR:
  220. return true;
  221. default:
  222. return false;
  223. }
  224. }
  225. static const struct regmap_config rk_spdif_regmap_config = {
  226. .reg_bits = 32,
  227. .reg_stride = 4,
  228. .val_bits = 32,
  229. .max_register = SPDIF_SMPDR,
  230. .writeable_reg = rk_spdif_wr_reg,
  231. .readable_reg = rk_spdif_rd_reg,
  232. .volatile_reg = rk_spdif_volatile_reg,
  233. .cache_type = REGCACHE_FLAT,
  234. };
  235. static int rk_spdif_probe(struct platform_device *pdev)
  236. {
  237. struct device_node *np = pdev->dev.of_node;
  238. struct rk_spdif_dev *spdif;
  239. const struct of_device_id *match;
  240. struct resource *res;
  241. void __iomem *regs;
  242. int ret;
  243. match = of_match_node(rk_spdif_match, np);
  244. if (match->data == (void *)RK_SPDIF_RK3288) {
  245. struct regmap *grf;
  246. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  247. if (IS_ERR(grf)) {
  248. dev_err(&pdev->dev,
  249. "rockchip_spdif missing 'rockchip,grf' \n");
  250. return PTR_ERR(grf);
  251. }
  252. /* Select the 8 channel SPDIF solution on RK3288 as
  253. * the 2 channel one does not appear to work
  254. */
  255. regmap_write(grf, RK3288_GRF_SOC_CON2, BIT(1) << 16);
  256. }
  257. spdif = devm_kzalloc(&pdev->dev, sizeof(*spdif), GFP_KERNEL);
  258. if (!spdif)
  259. return -ENOMEM;
  260. spdif->hclk = devm_clk_get(&pdev->dev, "hclk");
  261. if (IS_ERR(spdif->hclk)) {
  262. dev_err(&pdev->dev, "Can't retrieve rk_spdif bus clock\n");
  263. return PTR_ERR(spdif->hclk);
  264. }
  265. ret = clk_prepare_enable(spdif->hclk);
  266. if (ret) {
  267. dev_err(spdif->dev, "hclock enable failed %d\n", ret);
  268. return ret;
  269. }
  270. spdif->mclk = devm_clk_get(&pdev->dev, "mclk");
  271. if (IS_ERR(spdif->mclk)) {
  272. dev_err(&pdev->dev, "Can't retrieve rk_spdif master clock\n");
  273. ret = PTR_ERR(spdif->mclk);
  274. goto err_disable_hclk;
  275. }
  276. ret = clk_prepare_enable(spdif->mclk);
  277. if (ret) {
  278. dev_err(spdif->dev, "clock enable failed %d\n", ret);
  279. goto err_disable_clocks;
  280. }
  281. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  282. regs = devm_ioremap_resource(&pdev->dev, res);
  283. if (IS_ERR(regs)) {
  284. ret = PTR_ERR(regs);
  285. goto err_disable_clocks;
  286. }
  287. spdif->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "hclk", regs,
  288. &rk_spdif_regmap_config);
  289. if (IS_ERR(spdif->regmap)) {
  290. dev_err(&pdev->dev,
  291. "Failed to initialise managed register map\n");
  292. ret = PTR_ERR(spdif->regmap);
  293. goto err_disable_clocks;
  294. }
  295. spdif->playback_dma_data.addr = res->start + SPDIF_SMPDR;
  296. spdif->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  297. spdif->playback_dma_data.maxburst = 4;
  298. spdif->dev = &pdev->dev;
  299. dev_set_drvdata(&pdev->dev, spdif);
  300. pm_runtime_set_active(&pdev->dev);
  301. pm_runtime_enable(&pdev->dev);
  302. pm_request_idle(&pdev->dev);
  303. ret = devm_snd_soc_register_component(&pdev->dev,
  304. &rk_spdif_component,
  305. &rk_spdif_dai, 1);
  306. if (ret) {
  307. dev_err(&pdev->dev, "Could not register DAI\n");
  308. goto err_pm_runtime;
  309. }
  310. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  311. if (ret) {
  312. dev_err(&pdev->dev, "Could not register PCM\n");
  313. goto err_pm_runtime;
  314. }
  315. return 0;
  316. err_pm_runtime:
  317. pm_runtime_disable(&pdev->dev);
  318. err_disable_clocks:
  319. clk_disable_unprepare(spdif->mclk);
  320. err_disable_hclk:
  321. clk_disable_unprepare(spdif->hclk);
  322. return ret;
  323. }
  324. static int rk_spdif_remove(struct platform_device *pdev)
  325. {
  326. struct rk_spdif_dev *spdif = dev_get_drvdata(&pdev->dev);
  327. pm_runtime_disable(&pdev->dev);
  328. if (!pm_runtime_status_suspended(&pdev->dev))
  329. rk_spdif_runtime_suspend(&pdev->dev);
  330. clk_disable_unprepare(spdif->mclk);
  331. clk_disable_unprepare(spdif->hclk);
  332. return 0;
  333. }
  334. static const struct dev_pm_ops rk_spdif_pm_ops = {
  335. SET_RUNTIME_PM_OPS(rk_spdif_runtime_suspend, rk_spdif_runtime_resume,
  336. NULL)
  337. };
  338. static struct platform_driver rk_spdif_driver = {
  339. .probe = rk_spdif_probe,
  340. .remove = rk_spdif_remove,
  341. .driver = {
  342. .name = "rockchip-spdif",
  343. .of_match_table = of_match_ptr(rk_spdif_match),
  344. .pm = &rk_spdif_pm_ops,
  345. },
  346. };
  347. module_platform_driver(rk_spdif_driver);
  348. MODULE_ALIAS("platform:rockchip-spdif");
  349. MODULE_DESCRIPTION("ROCKCHIP SPDIF transceiver Interface");
  350. MODULE_AUTHOR("Sjoerd Simons <sjoerd.simons@collabora.co.uk>");
  351. MODULE_LICENSE("GPL v2");