sun4i-spdif.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * ALSA SoC SPDIF Audio Layer
  3. *
  4. * Copyright 2015 Andrea Venturi <be17068@iperbole.bo.it>
  5. * Copyright 2015 Marcus Cooper <codekipper@gmail.com>
  6. *
  7. * Based on the Allwinner SDK driver, released under the GPL.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/regmap.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_device.h>
  27. #include <linux/ioport.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/reset.h>
  32. #include <sound/dmaengine_pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #define SUN4I_SPDIF_CTL (0x00)
  36. #define SUN4I_SPDIF_CTL_MCLKDIV(v) ((v) << 4) /* v even */
  37. #define SUN4I_SPDIF_CTL_MCLKOUTEN BIT(2)
  38. #define SUN4I_SPDIF_CTL_GEN BIT(1)
  39. #define SUN4I_SPDIF_CTL_RESET BIT(0)
  40. #define SUN4I_SPDIF_TXCFG (0x04)
  41. #define SUN4I_SPDIF_TXCFG_SINGLEMOD BIT(31)
  42. #define SUN4I_SPDIF_TXCFG_ASS BIT(17)
  43. #define SUN4I_SPDIF_TXCFG_NONAUDIO BIT(16)
  44. #define SUN4I_SPDIF_TXCFG_TXRATIO(v) ((v) << 4)
  45. #define SUN4I_SPDIF_TXCFG_TXRATIO_MASK GENMASK(8, 4)
  46. #define SUN4I_SPDIF_TXCFG_FMTRVD GENMASK(3, 2)
  47. #define SUN4I_SPDIF_TXCFG_FMT16BIT (0 << 2)
  48. #define SUN4I_SPDIF_TXCFG_FMT20BIT (1 << 2)
  49. #define SUN4I_SPDIF_TXCFG_FMT24BIT (2 << 2)
  50. #define SUN4I_SPDIF_TXCFG_CHSTMODE BIT(1)
  51. #define SUN4I_SPDIF_TXCFG_TXEN BIT(0)
  52. #define SUN4I_SPDIF_RXCFG (0x08)
  53. #define SUN4I_SPDIF_RXCFG_LOCKFLAG BIT(4)
  54. #define SUN4I_SPDIF_RXCFG_CHSTSRC BIT(3)
  55. #define SUN4I_SPDIF_RXCFG_CHSTCP BIT(1)
  56. #define SUN4I_SPDIF_RXCFG_RXEN BIT(0)
  57. #define SUN4I_SPDIF_TXFIFO (0x0C)
  58. #define SUN4I_SPDIF_RXFIFO (0x10)
  59. #define SUN4I_SPDIF_FCTL (0x14)
  60. #define SUN4I_SPDIF_FCTL_FIFOSRC BIT(31)
  61. #define SUN4I_SPDIF_FCTL_FTX BIT(17)
  62. #define SUN4I_SPDIF_FCTL_FRX BIT(16)
  63. #define SUN4I_SPDIF_FCTL_TXTL(v) ((v) << 8)
  64. #define SUN4I_SPDIF_FCTL_TXTL_MASK GENMASK(12, 8)
  65. #define SUN4I_SPDIF_FCTL_RXTL(v) ((v) << 3)
  66. #define SUN4I_SPDIF_FCTL_RXTL_MASK GENMASK(7, 3)
  67. #define SUN4I_SPDIF_FCTL_TXIM BIT(2)
  68. #define SUN4I_SPDIF_FCTL_RXOM(v) ((v) << 0)
  69. #define SUN4I_SPDIF_FCTL_RXOM_MASK GENMASK(1, 0)
  70. #define SUN4I_SPDIF_FSTA (0x18)
  71. #define SUN4I_SPDIF_FSTA_TXE BIT(14)
  72. #define SUN4I_SPDIF_FSTA_TXECNTSHT (8)
  73. #define SUN4I_SPDIF_FSTA_RXA BIT(6)
  74. #define SUN4I_SPDIF_FSTA_RXACNTSHT (0)
  75. #define SUN4I_SPDIF_INT (0x1C)
  76. #define SUN4I_SPDIF_INT_RXLOCKEN BIT(18)
  77. #define SUN4I_SPDIF_INT_RXUNLOCKEN BIT(17)
  78. #define SUN4I_SPDIF_INT_RXPARERREN BIT(16)
  79. #define SUN4I_SPDIF_INT_TXDRQEN BIT(7)
  80. #define SUN4I_SPDIF_INT_TXUIEN BIT(6)
  81. #define SUN4I_SPDIF_INT_TXOIEN BIT(5)
  82. #define SUN4I_SPDIF_INT_TXEIEN BIT(4)
  83. #define SUN4I_SPDIF_INT_RXDRQEN BIT(2)
  84. #define SUN4I_SPDIF_INT_RXOIEN BIT(1)
  85. #define SUN4I_SPDIF_INT_RXAIEN BIT(0)
  86. #define SUN4I_SPDIF_ISTA (0x20)
  87. #define SUN4I_SPDIF_ISTA_RXLOCKSTA BIT(18)
  88. #define SUN4I_SPDIF_ISTA_RXUNLOCKSTA BIT(17)
  89. #define SUN4I_SPDIF_ISTA_RXPARERRSTA BIT(16)
  90. #define SUN4I_SPDIF_ISTA_TXUSTA BIT(6)
  91. #define SUN4I_SPDIF_ISTA_TXOSTA BIT(5)
  92. #define SUN4I_SPDIF_ISTA_TXESTA BIT(4)
  93. #define SUN4I_SPDIF_ISTA_RXOSTA BIT(1)
  94. #define SUN4I_SPDIF_ISTA_RXASTA BIT(0)
  95. #define SUN4I_SPDIF_TXCNT (0x24)
  96. #define SUN4I_SPDIF_RXCNT (0x28)
  97. #define SUN4I_SPDIF_TXCHSTA0 (0x2C)
  98. #define SUN4I_SPDIF_TXCHSTA0_CLK(v) ((v) << 28)
  99. #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ(v) ((v) << 24)
  100. #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ_MASK GENMASK(27, 24)
  101. #define SUN4I_SPDIF_TXCHSTA0_CHNUM(v) ((v) << 20)
  102. #define SUN4I_SPDIF_TXCHSTA0_CHNUM_MASK GENMASK(23, 20)
  103. #define SUN4I_SPDIF_TXCHSTA0_SRCNUM(v) ((v) << 16)
  104. #define SUN4I_SPDIF_TXCHSTA0_CATACOD(v) ((v) << 8)
  105. #define SUN4I_SPDIF_TXCHSTA0_MODE(v) ((v) << 6)
  106. #define SUN4I_SPDIF_TXCHSTA0_EMPHASIS(v) ((v) << 3)
  107. #define SUN4I_SPDIF_TXCHSTA0_CP BIT(2)
  108. #define SUN4I_SPDIF_TXCHSTA0_AUDIO BIT(1)
  109. #define SUN4I_SPDIF_TXCHSTA0_PRO BIT(0)
  110. #define SUN4I_SPDIF_TXCHSTA1 (0x30)
  111. #define SUN4I_SPDIF_TXCHSTA1_CGMSA(v) ((v) << 8)
  112. #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ(v) ((v) << 4)
  113. #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ_MASK GENMASK(7, 4)
  114. #define SUN4I_SPDIF_TXCHSTA1_SAMWORDLEN(v) ((v) << 1)
  115. #define SUN4I_SPDIF_TXCHSTA1_MAXWORDLEN BIT(0)
  116. #define SUN4I_SPDIF_RXCHSTA0 (0x34)
  117. #define SUN4I_SPDIF_RXCHSTA0_CLK(v) ((v) << 28)
  118. #define SUN4I_SPDIF_RXCHSTA0_SAMFREQ(v) ((v) << 24)
  119. #define SUN4I_SPDIF_RXCHSTA0_CHNUM(v) ((v) << 20)
  120. #define SUN4I_SPDIF_RXCHSTA0_SRCNUM(v) ((v) << 16)
  121. #define SUN4I_SPDIF_RXCHSTA0_CATACOD(v) ((v) << 8)
  122. #define SUN4I_SPDIF_RXCHSTA0_MODE(v) ((v) << 6)
  123. #define SUN4I_SPDIF_RXCHSTA0_EMPHASIS(v) ((v) << 3)
  124. #define SUN4I_SPDIF_RXCHSTA0_CP BIT(2)
  125. #define SUN4I_SPDIF_RXCHSTA0_AUDIO BIT(1)
  126. #define SUN4I_SPDIF_RXCHSTA0_PRO BIT(0)
  127. #define SUN4I_SPDIF_RXCHSTA1 (0x38)
  128. #define SUN4I_SPDIF_RXCHSTA1_CGMSA(v) ((v) << 8)
  129. #define SUN4I_SPDIF_RXCHSTA1_ORISAMFREQ(v) ((v) << 4)
  130. #define SUN4I_SPDIF_RXCHSTA1_SAMWORDLEN(v) ((v) << 1)
  131. #define SUN4I_SPDIF_RXCHSTA1_MAXWORDLEN BIT(0)
  132. /* Defines for Sampling Frequency */
  133. #define SUN4I_SPDIF_SAMFREQ_44_1KHZ 0x0
  134. #define SUN4I_SPDIF_SAMFREQ_NOT_INDICATED 0x1
  135. #define SUN4I_SPDIF_SAMFREQ_48KHZ 0x2
  136. #define SUN4I_SPDIF_SAMFREQ_32KHZ 0x3
  137. #define SUN4I_SPDIF_SAMFREQ_22_05KHZ 0x4
  138. #define SUN4I_SPDIF_SAMFREQ_24KHZ 0x6
  139. #define SUN4I_SPDIF_SAMFREQ_88_2KHZ 0x8
  140. #define SUN4I_SPDIF_SAMFREQ_76_8KHZ 0x9
  141. #define SUN4I_SPDIF_SAMFREQ_96KHZ 0xa
  142. #define SUN4I_SPDIF_SAMFREQ_176_4KHZ 0xc
  143. #define SUN4I_SPDIF_SAMFREQ_192KHZ 0xe
  144. struct sun4i_spdif_dev {
  145. struct platform_device *pdev;
  146. struct clk *spdif_clk;
  147. struct clk *apb_clk;
  148. struct reset_control *rst;
  149. struct snd_soc_dai_driver cpu_dai_drv;
  150. struct regmap *regmap;
  151. struct snd_dmaengine_dai_dma_data dma_params_tx;
  152. };
  153. static void sun4i_spdif_configure(struct sun4i_spdif_dev *host)
  154. {
  155. /* soft reset SPDIF */
  156. regmap_write(host->regmap, SUN4I_SPDIF_CTL, SUN4I_SPDIF_CTL_RESET);
  157. /* flush TX FIFO */
  158. regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
  159. SUN4I_SPDIF_FCTL_FTX, SUN4I_SPDIF_FCTL_FTX);
  160. /* clear TX counter */
  161. regmap_write(host->regmap, SUN4I_SPDIF_TXCNT, 0);
  162. }
  163. static void sun4i_snd_txctrl_on(struct snd_pcm_substream *substream,
  164. struct sun4i_spdif_dev *host)
  165. {
  166. if (substream->runtime->channels == 1)
  167. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  168. SUN4I_SPDIF_TXCFG_SINGLEMOD,
  169. SUN4I_SPDIF_TXCFG_SINGLEMOD);
  170. /* SPDIF TX ENABLE */
  171. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  172. SUN4I_SPDIF_TXCFG_TXEN, SUN4I_SPDIF_TXCFG_TXEN);
  173. /* DRQ ENABLE */
  174. regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
  175. SUN4I_SPDIF_INT_TXDRQEN, SUN4I_SPDIF_INT_TXDRQEN);
  176. /* Global enable */
  177. regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
  178. SUN4I_SPDIF_CTL_GEN, SUN4I_SPDIF_CTL_GEN);
  179. }
  180. static void sun4i_snd_txctrl_off(struct snd_pcm_substream *substream,
  181. struct sun4i_spdif_dev *host)
  182. {
  183. /* SPDIF TX DISABLE */
  184. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  185. SUN4I_SPDIF_TXCFG_TXEN, 0);
  186. /* DRQ DISABLE */
  187. regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
  188. SUN4I_SPDIF_INT_TXDRQEN, 0);
  189. /* Global disable */
  190. regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
  191. SUN4I_SPDIF_CTL_GEN, 0);
  192. }
  193. static int sun4i_spdif_startup(struct snd_pcm_substream *substream,
  194. struct snd_soc_dai *cpu_dai)
  195. {
  196. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  197. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  198. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  199. return -EINVAL;
  200. sun4i_spdif_configure(host);
  201. return 0;
  202. }
  203. static int sun4i_spdif_hw_params(struct snd_pcm_substream *substream,
  204. struct snd_pcm_hw_params *params,
  205. struct snd_soc_dai *cpu_dai)
  206. {
  207. int ret = 0;
  208. int fmt;
  209. unsigned long rate = params_rate(params);
  210. u32 mclk_div = 0;
  211. unsigned int mclk = 0;
  212. u32 reg_val;
  213. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  214. struct platform_device *pdev = host->pdev;
  215. /* Add the PCM and raw data select interface */
  216. switch (params_channels(params)) {
  217. case 1: /* PCM mode */
  218. case 2:
  219. fmt = 0;
  220. break;
  221. case 4: /* raw data mode */
  222. fmt = SUN4I_SPDIF_TXCFG_NONAUDIO;
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. switch (params_format(params)) {
  228. case SNDRV_PCM_FORMAT_S16_LE:
  229. fmt |= SUN4I_SPDIF_TXCFG_FMT16BIT;
  230. break;
  231. case SNDRV_PCM_FORMAT_S20_3LE:
  232. fmt |= SUN4I_SPDIF_TXCFG_FMT20BIT;
  233. break;
  234. case SNDRV_PCM_FORMAT_S24_LE:
  235. fmt |= SUN4I_SPDIF_TXCFG_FMT24BIT;
  236. break;
  237. default:
  238. return -EINVAL;
  239. }
  240. switch (rate) {
  241. case 22050:
  242. case 44100:
  243. case 88200:
  244. case 176400:
  245. mclk = 22579200;
  246. break;
  247. case 24000:
  248. case 32000:
  249. case 48000:
  250. case 96000:
  251. case 192000:
  252. mclk = 24576000;
  253. break;
  254. default:
  255. return -EINVAL;
  256. }
  257. ret = clk_set_rate(host->spdif_clk, mclk);
  258. if (ret < 0) {
  259. dev_err(&pdev->dev,
  260. "Setting SPDIF clock rate for %d Hz failed!\n", mclk);
  261. return ret;
  262. }
  263. regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
  264. SUN4I_SPDIF_FCTL_TXIM, SUN4I_SPDIF_FCTL_TXIM);
  265. switch (rate) {
  266. case 22050:
  267. case 24000:
  268. mclk_div = 8;
  269. break;
  270. case 32000:
  271. mclk_div = 6;
  272. break;
  273. case 44100:
  274. case 48000:
  275. mclk_div = 4;
  276. break;
  277. case 88200:
  278. case 96000:
  279. mclk_div = 2;
  280. break;
  281. case 176400:
  282. case 192000:
  283. mclk_div = 1;
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. reg_val = 0;
  289. reg_val |= SUN4I_SPDIF_TXCFG_ASS;
  290. reg_val |= fmt; /* set non audio and bit depth */
  291. reg_val |= SUN4I_SPDIF_TXCFG_CHSTMODE;
  292. reg_val |= SUN4I_SPDIF_TXCFG_TXRATIO(mclk_div - 1);
  293. regmap_write(host->regmap, SUN4I_SPDIF_TXCFG, reg_val);
  294. return 0;
  295. }
  296. static int sun4i_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
  297. struct snd_soc_dai *dai)
  298. {
  299. int ret = 0;
  300. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
  301. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  302. return -EINVAL;
  303. switch (cmd) {
  304. case SNDRV_PCM_TRIGGER_START:
  305. case SNDRV_PCM_TRIGGER_RESUME:
  306. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  307. sun4i_snd_txctrl_on(substream, host);
  308. break;
  309. case SNDRV_PCM_TRIGGER_STOP:
  310. case SNDRV_PCM_TRIGGER_SUSPEND:
  311. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  312. sun4i_snd_txctrl_off(substream, host);
  313. break;
  314. default:
  315. ret = -EINVAL;
  316. break;
  317. }
  318. return ret;
  319. }
  320. static int sun4i_spdif_soc_dai_probe(struct snd_soc_dai *dai)
  321. {
  322. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
  323. snd_soc_dai_init_dma_data(dai, &host->dma_params_tx, NULL);
  324. return 0;
  325. }
  326. static const struct snd_soc_dai_ops sun4i_spdif_dai_ops = {
  327. .startup = sun4i_spdif_startup,
  328. .trigger = sun4i_spdif_trigger,
  329. .hw_params = sun4i_spdif_hw_params,
  330. };
  331. static const struct regmap_config sun4i_spdif_regmap_config = {
  332. .reg_bits = 32,
  333. .reg_stride = 4,
  334. .val_bits = 32,
  335. .max_register = SUN4I_SPDIF_RXCHSTA1,
  336. };
  337. #define SUN4I_RATES SNDRV_PCM_RATE_8000_192000
  338. #define SUN4I_FORMATS (SNDRV_PCM_FORMAT_S16_LE | \
  339. SNDRV_PCM_FORMAT_S20_3LE | \
  340. SNDRV_PCM_FORMAT_S24_LE)
  341. static struct snd_soc_dai_driver sun4i_spdif_dai = {
  342. .playback = {
  343. .channels_min = 1,
  344. .channels_max = 2,
  345. .rates = SUN4I_RATES,
  346. .formats = SUN4I_FORMATS,
  347. },
  348. .probe = sun4i_spdif_soc_dai_probe,
  349. .ops = &sun4i_spdif_dai_ops,
  350. .name = "spdif",
  351. };
  352. static const struct of_device_id sun4i_spdif_of_match[] = {
  353. { .compatible = "allwinner,sun4i-a10-spdif", },
  354. { .compatible = "allwinner,sun6i-a31-spdif", },
  355. { /* sentinel */ }
  356. };
  357. MODULE_DEVICE_TABLE(of, sun4i_spdif_of_match);
  358. static const struct snd_soc_component_driver sun4i_spdif_component = {
  359. .name = "sun4i-spdif",
  360. };
  361. static int sun4i_spdif_runtime_suspend(struct device *dev)
  362. {
  363. struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
  364. clk_disable_unprepare(host->spdif_clk);
  365. clk_disable_unprepare(host->apb_clk);
  366. return 0;
  367. }
  368. static int sun4i_spdif_runtime_resume(struct device *dev)
  369. {
  370. struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
  371. clk_prepare_enable(host->spdif_clk);
  372. clk_prepare_enable(host->apb_clk);
  373. return 0;
  374. }
  375. static int sun4i_spdif_probe(struct platform_device *pdev)
  376. {
  377. struct sun4i_spdif_dev *host;
  378. struct resource *res;
  379. int ret;
  380. void __iomem *base;
  381. dev_dbg(&pdev->dev, "Entered %s\n", __func__);
  382. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  383. if (!host)
  384. return -ENOMEM;
  385. host->pdev = pdev;
  386. /* Initialize this copy of the CPU DAI driver structure */
  387. memcpy(&host->cpu_dai_drv, &sun4i_spdif_dai, sizeof(sun4i_spdif_dai));
  388. host->cpu_dai_drv.name = dev_name(&pdev->dev);
  389. /* Get the addresses */
  390. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  391. base = devm_ioremap_resource(&pdev->dev, res);
  392. if (IS_ERR(base))
  393. return PTR_ERR(base);
  394. host->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  395. &sun4i_spdif_regmap_config);
  396. /* Clocks */
  397. host->apb_clk = devm_clk_get(&pdev->dev, "apb");
  398. if (IS_ERR(host->apb_clk)) {
  399. dev_err(&pdev->dev, "failed to get a apb clock.\n");
  400. return PTR_ERR(host->apb_clk);
  401. }
  402. host->spdif_clk = devm_clk_get(&pdev->dev, "spdif");
  403. if (IS_ERR(host->spdif_clk)) {
  404. dev_err(&pdev->dev, "failed to get a spdif clock.\n");
  405. ret = PTR_ERR(host->spdif_clk);
  406. goto err_disable_apb_clk;
  407. }
  408. host->dma_params_tx.addr = res->start + SUN4I_SPDIF_TXFIFO;
  409. host->dma_params_tx.maxburst = 8;
  410. host->dma_params_tx.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  411. platform_set_drvdata(pdev, host);
  412. if (of_device_is_compatible(pdev->dev.of_node,
  413. "allwinner,sun6i-a31-spdif")) {
  414. host->rst = devm_reset_control_get_optional(&pdev->dev, NULL);
  415. if (IS_ERR(host->rst) && PTR_ERR(host->rst) == -EPROBE_DEFER) {
  416. ret = -EPROBE_DEFER;
  417. dev_err(&pdev->dev, "Failed to get reset: %d\n", ret);
  418. goto err_disable_apb_clk;
  419. }
  420. if (!IS_ERR(host->rst))
  421. reset_control_deassert(host->rst);
  422. }
  423. ret = devm_snd_soc_register_component(&pdev->dev,
  424. &sun4i_spdif_component, &sun4i_spdif_dai, 1);
  425. if (ret)
  426. goto err_disable_apb_clk;
  427. pm_runtime_enable(&pdev->dev);
  428. if (!pm_runtime_enabled(&pdev->dev)) {
  429. ret = sun4i_spdif_runtime_resume(&pdev->dev);
  430. if (ret)
  431. goto err_unregister;
  432. }
  433. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  434. if (ret)
  435. goto err_suspend;
  436. return 0;
  437. err_suspend:
  438. if (!pm_runtime_status_suspended(&pdev->dev))
  439. sun4i_spdif_runtime_suspend(&pdev->dev);
  440. err_unregister:
  441. pm_runtime_disable(&pdev->dev);
  442. snd_soc_unregister_component(&pdev->dev);
  443. err_disable_apb_clk:
  444. clk_disable_unprepare(host->apb_clk);
  445. return ret;
  446. }
  447. static int sun4i_spdif_remove(struct platform_device *pdev)
  448. {
  449. pm_runtime_disable(&pdev->dev);
  450. if (!pm_runtime_status_suspended(&pdev->dev))
  451. sun4i_spdif_runtime_suspend(&pdev->dev);
  452. snd_soc_unregister_platform(&pdev->dev);
  453. snd_soc_unregister_component(&pdev->dev);
  454. return 0;
  455. }
  456. static const struct dev_pm_ops sun4i_spdif_pm = {
  457. SET_RUNTIME_PM_OPS(sun4i_spdif_runtime_suspend,
  458. sun4i_spdif_runtime_resume, NULL)
  459. };
  460. static struct platform_driver sun4i_spdif_driver = {
  461. .driver = {
  462. .name = "sun4i-spdif",
  463. .of_match_table = of_match_ptr(sun4i_spdif_of_match),
  464. .pm = &sun4i_spdif_pm,
  465. },
  466. .probe = sun4i_spdif_probe,
  467. .remove = sun4i_spdif_remove,
  468. };
  469. module_platform_driver(sun4i_spdif_driver);
  470. MODULE_AUTHOR("Marcus Cooper <codekipper@gmail.com>");
  471. MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
  472. MODULE_DESCRIPTION("Allwinner sun4i SPDIF SoC Interface");
  473. MODULE_LICENSE("GPL");
  474. MODULE_ALIAS("platform:sun4i-spdif");