zx296702-spdif.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (C) 2015 Linaro
  3. *
  4. * Author: Jun Nie <jun.nie@linaro.org>
  5. *
  6. * License terms: GNU General Public License (GPL) version 2
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/device.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/init.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <sound/asoundef.h>
  17. #include <sound/core.h>
  18. #include <sound/dmaengine_pcm.h>
  19. #include <sound/initval.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <sound/soc-dai.h>
  24. #define ZX_CTRL 0x04
  25. #define ZX_FIFOCTRL 0x08
  26. #define ZX_INT_STATUS 0x10
  27. #define ZX_INT_MASK 0x14
  28. #define ZX_DATA 0x18
  29. #define ZX_VALID_BIT 0x1c
  30. #define ZX_CH_STA_1 0x20
  31. #define ZX_CH_STA_2 0x24
  32. #define ZX_CH_STA_3 0x28
  33. #define ZX_CH_STA_4 0x2c
  34. #define ZX_CH_STA_5 0x30
  35. #define ZX_CH_STA_6 0x34
  36. #define ZX_CTRL_MODA_16 (0 << 6)
  37. #define ZX_CTRL_MODA_18 BIT(6)
  38. #define ZX_CTRL_MODA_20 (2 << 6)
  39. #define ZX_CTRL_MODA_24 (3 << 6)
  40. #define ZX_CTRL_MODA_MASK (3 << 6)
  41. #define ZX_CTRL_ENB BIT(4)
  42. #define ZX_CTRL_DNB (0 << 4)
  43. #define ZX_CTRL_ENB_MASK BIT(4)
  44. #define ZX_CTRL_TX_OPEN BIT(0)
  45. #define ZX_CTRL_TX_CLOSE (0 << 0)
  46. #define ZX_CTRL_TX_MASK BIT(0)
  47. #define ZX_CTRL_OPEN (ZX_CTRL_TX_OPEN | ZX_CTRL_ENB)
  48. #define ZX_CTRL_CLOSE (ZX_CTRL_TX_CLOSE | ZX_CTRL_DNB)
  49. #define ZX_CTRL_DOUBLE_TRACK (0 << 8)
  50. #define ZX_CTRL_LEFT_TRACK BIT(8)
  51. #define ZX_CTRL_RIGHT_TRACK (2 << 8)
  52. #define ZX_CTRL_TRACK_MASK (3 << 8)
  53. #define ZX_FIFOCTRL_TXTH_MASK (0x1f << 8)
  54. #define ZX_FIFOCTRL_TXTH(x) (x << 8)
  55. #define ZX_FIFOCTRL_TX_DMA_EN BIT(2)
  56. #define ZX_FIFOCTRL_TX_DMA_DIS (0 << 2)
  57. #define ZX_FIFOCTRL_TX_DMA_EN_MASK BIT(2)
  58. #define ZX_FIFOCTRL_TX_FIFO_RST BIT(0)
  59. #define ZX_FIFOCTRL_TX_FIFO_RST_MASK BIT(0)
  60. #define ZX_VALID_DOUBLE_TRACK (0 << 0)
  61. #define ZX_VALID_LEFT_TRACK BIT(1)
  62. #define ZX_VALID_RIGHT_TRACK (2 << 0)
  63. #define ZX_VALID_TRACK_MASK (3 << 0)
  64. #define ZX_SPDIF_CLK_RAT (4 * 32)
  65. struct zx_spdif_info {
  66. struct snd_dmaengine_dai_dma_data dma_data;
  67. struct clk *dai_clk;
  68. void __iomem *reg_base;
  69. resource_size_t mapbase;
  70. };
  71. static int zx_spdif_dai_probe(struct snd_soc_dai *dai)
  72. {
  73. struct zx_spdif_info *zx_spdif = dev_get_drvdata(dai->dev);
  74. snd_soc_dai_set_drvdata(dai, zx_spdif);
  75. zx_spdif->dma_data.addr = zx_spdif->mapbase + ZX_DATA;
  76. zx_spdif->dma_data.maxburst = 8;
  77. snd_soc_dai_init_dma_data(dai, &zx_spdif->dma_data, NULL);
  78. return 0;
  79. }
  80. static int zx_spdif_chanstats(void __iomem *base, unsigned int rate)
  81. {
  82. u32 cstas1;
  83. switch (rate) {
  84. case 22050:
  85. cstas1 = IEC958_AES3_CON_FS_22050;
  86. break;
  87. case 24000:
  88. cstas1 = IEC958_AES3_CON_FS_24000;
  89. break;
  90. case 32000:
  91. cstas1 = IEC958_AES3_CON_FS_32000;
  92. break;
  93. case 44100:
  94. cstas1 = IEC958_AES3_CON_FS_44100;
  95. break;
  96. case 48000:
  97. cstas1 = IEC958_AES3_CON_FS_48000;
  98. break;
  99. case 88200:
  100. cstas1 = IEC958_AES3_CON_FS_88200;
  101. break;
  102. case 96000:
  103. cstas1 = IEC958_AES3_CON_FS_96000;
  104. break;
  105. case 176400:
  106. cstas1 = IEC958_AES3_CON_FS_176400;
  107. break;
  108. case 192000:
  109. cstas1 = IEC958_AES3_CON_FS_192000;
  110. break;
  111. default:
  112. return -EINVAL;
  113. }
  114. cstas1 = cstas1 << 24;
  115. cstas1 |= IEC958_AES0_CON_NOT_COPYRIGHT;
  116. writel_relaxed(cstas1, base + ZX_CH_STA_1);
  117. return 0;
  118. }
  119. static int zx_spdif_hw_params(struct snd_pcm_substream *substream,
  120. struct snd_pcm_hw_params *params,
  121. struct snd_soc_dai *socdai)
  122. {
  123. struct zx_spdif_info *zx_spdif = dev_get_drvdata(socdai->dev);
  124. struct zx_spdif_info *spdif = snd_soc_dai_get_drvdata(socdai);
  125. struct snd_dmaengine_dai_dma_data *dma_data = &zx_spdif->dma_data;
  126. u32 val, ch_num, rate;
  127. int ret;
  128. dma_data = snd_soc_dai_get_dma_data(socdai, substream);
  129. dma_data->addr_width = params_width(params) >> 3;
  130. val = readl_relaxed(zx_spdif->reg_base + ZX_CTRL);
  131. val &= ~ZX_CTRL_MODA_MASK;
  132. switch (params_format(params)) {
  133. case SNDRV_PCM_FORMAT_S16_LE:
  134. val |= ZX_CTRL_MODA_16;
  135. break;
  136. case SNDRV_PCM_FORMAT_S18_3LE:
  137. val |= ZX_CTRL_MODA_18;
  138. break;
  139. case SNDRV_PCM_FORMAT_S20_3LE:
  140. val |= ZX_CTRL_MODA_20;
  141. break;
  142. case SNDRV_PCM_FORMAT_S24_LE:
  143. val |= ZX_CTRL_MODA_24;
  144. break;
  145. default:
  146. dev_err(socdai->dev, "Format not support!\n");
  147. return -EINVAL;
  148. }
  149. ch_num = params_channels(params);
  150. if (ch_num == 2)
  151. val |= ZX_CTRL_DOUBLE_TRACK;
  152. else
  153. val |= ZX_CTRL_LEFT_TRACK;
  154. writel_relaxed(val, zx_spdif->reg_base + ZX_CTRL);
  155. val = readl_relaxed(zx_spdif->reg_base + ZX_VALID_BIT);
  156. val &= ~ZX_VALID_TRACK_MASK;
  157. if (ch_num == 2)
  158. val |= ZX_VALID_DOUBLE_TRACK;
  159. else
  160. val |= ZX_VALID_RIGHT_TRACK;
  161. writel_relaxed(val, zx_spdif->reg_base + ZX_VALID_BIT);
  162. rate = params_rate(params);
  163. ret = zx_spdif_chanstats(zx_spdif->reg_base, rate);
  164. if (ret)
  165. return ret;
  166. return clk_set_rate(spdif->dai_clk, rate * ch_num * ZX_SPDIF_CLK_RAT);
  167. }
  168. static void zx_spdif_cfg_tx(void __iomem *base, int on)
  169. {
  170. u32 val;
  171. val = readl_relaxed(base + ZX_CTRL);
  172. val &= ~(ZX_CTRL_ENB_MASK | ZX_CTRL_TX_MASK);
  173. val |= on ? ZX_CTRL_OPEN : ZX_CTRL_CLOSE;
  174. writel_relaxed(val, base + ZX_CTRL);
  175. val = readl_relaxed(base + ZX_FIFOCTRL);
  176. val &= ~ZX_FIFOCTRL_TX_DMA_EN_MASK;
  177. if (on)
  178. val |= ZX_FIFOCTRL_TX_DMA_EN;
  179. writel_relaxed(val, base + ZX_FIFOCTRL);
  180. }
  181. static int zx_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
  182. struct snd_soc_dai *dai)
  183. {
  184. u32 val;
  185. struct zx_spdif_info *zx_spdif = dev_get_drvdata(dai->dev);
  186. int ret = 0;
  187. switch (cmd) {
  188. case SNDRV_PCM_TRIGGER_START:
  189. val = readl_relaxed(zx_spdif->reg_base + ZX_FIFOCTRL);
  190. val |= ZX_FIFOCTRL_TX_FIFO_RST;
  191. writel_relaxed(val, zx_spdif->reg_base + ZX_FIFOCTRL);
  192. /* fall thru */
  193. case SNDRV_PCM_TRIGGER_RESUME:
  194. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  195. zx_spdif_cfg_tx(zx_spdif->reg_base, true);
  196. break;
  197. case SNDRV_PCM_TRIGGER_STOP:
  198. case SNDRV_PCM_TRIGGER_SUSPEND:
  199. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  200. zx_spdif_cfg_tx(zx_spdif->reg_base, false);
  201. break;
  202. default:
  203. ret = -EINVAL;
  204. break;
  205. }
  206. return ret;
  207. }
  208. static int zx_spdif_startup(struct snd_pcm_substream *substream,
  209. struct snd_soc_dai *dai)
  210. {
  211. struct zx_spdif_info *zx_spdif = dev_get_drvdata(dai->dev);
  212. return clk_prepare_enable(zx_spdif->dai_clk);
  213. }
  214. static void zx_spdif_shutdown(struct snd_pcm_substream *substream,
  215. struct snd_soc_dai *dai)
  216. {
  217. struct zx_spdif_info *zx_spdif = dev_get_drvdata(dai->dev);
  218. clk_disable_unprepare(zx_spdif->dai_clk);
  219. }
  220. #define ZX_RATES \
  221. (SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  222. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |\
  223. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
  224. #define ZX_FORMAT \
  225. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE \
  226. | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE)
  227. static struct snd_soc_dai_ops zx_spdif_dai_ops = {
  228. .trigger = zx_spdif_trigger,
  229. .startup = zx_spdif_startup,
  230. .shutdown = zx_spdif_shutdown,
  231. .hw_params = zx_spdif_hw_params,
  232. };
  233. static struct snd_soc_dai_driver zx_spdif_dai = {
  234. .name = "spdif",
  235. .id = 0,
  236. .probe = zx_spdif_dai_probe,
  237. .playback = {
  238. .channels_min = 1,
  239. .channels_max = 2,
  240. .rates = ZX_RATES,
  241. .formats = ZX_FORMAT,
  242. },
  243. .ops = &zx_spdif_dai_ops,
  244. };
  245. static const struct snd_soc_component_driver zx_spdif_component = {
  246. .name = "spdif",
  247. };
  248. static void zx_spdif_dev_init(void __iomem *base)
  249. {
  250. u32 val;
  251. writel_relaxed(0, base + ZX_CTRL);
  252. writel_relaxed(0, base + ZX_INT_MASK);
  253. writel_relaxed(0xf, base + ZX_INT_STATUS);
  254. writel_relaxed(0x1, base + ZX_FIFOCTRL);
  255. val = readl_relaxed(base + ZX_FIFOCTRL);
  256. val &= ~(ZX_FIFOCTRL_TXTH_MASK | ZX_FIFOCTRL_TX_FIFO_RST_MASK);
  257. val |= ZX_FIFOCTRL_TXTH(8);
  258. writel_relaxed(val, base + ZX_FIFOCTRL);
  259. }
  260. static int zx_spdif_probe(struct platform_device *pdev)
  261. {
  262. struct resource *res;
  263. struct zx_spdif_info *zx_spdif;
  264. int ret;
  265. zx_spdif = devm_kzalloc(&pdev->dev, sizeof(*zx_spdif), GFP_KERNEL);
  266. if (!zx_spdif)
  267. return -ENOMEM;
  268. zx_spdif->dai_clk = devm_clk_get(&pdev->dev, "tx");
  269. if (IS_ERR(zx_spdif->dai_clk)) {
  270. dev_err(&pdev->dev, "Fail to get clk\n");
  271. return PTR_ERR(zx_spdif->dai_clk);
  272. }
  273. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  274. zx_spdif->mapbase = res->start;
  275. zx_spdif->reg_base = devm_ioremap_resource(&pdev->dev, res);
  276. if (IS_ERR(zx_spdif->reg_base)) {
  277. dev_err(&pdev->dev, "ioremap failed!\n");
  278. return PTR_ERR(zx_spdif->reg_base);
  279. }
  280. zx_spdif_dev_init(zx_spdif->reg_base);
  281. platform_set_drvdata(pdev, zx_spdif);
  282. ret = devm_snd_soc_register_component(&pdev->dev, &zx_spdif_component,
  283. &zx_spdif_dai, 1);
  284. if (ret) {
  285. dev_err(&pdev->dev, "Register DAI failed: %d\n", ret);
  286. return ret;
  287. }
  288. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  289. if (ret)
  290. dev_err(&pdev->dev, "Register platform PCM failed: %d\n", ret);
  291. return ret;
  292. }
  293. static const struct of_device_id zx_spdif_dt_ids[] = {
  294. { .compatible = "zte,zx296702-spdif", },
  295. {}
  296. };
  297. MODULE_DEVICE_TABLE(of, zx_spdif_dt_ids);
  298. static struct platform_driver spdif_driver = {
  299. .probe = zx_spdif_probe,
  300. .driver = {
  301. .name = "zx-spdif",
  302. .of_match_table = zx_spdif_dt_ids,
  303. },
  304. };
  305. module_platform_driver(spdif_driver);
  306. MODULE_AUTHOR("Jun Nie <jun.nie@linaro.org>");
  307. MODULE_DESCRIPTION("ZTE SPDIF SoC DAI");
  308. MODULE_LICENSE("GPL");