sun4i-spdif.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 SUN8I_SPDIF_TXFIFO (0x20)
  96. #define SUN4I_SPDIF_TXCNT (0x24)
  97. #define SUN4I_SPDIF_RXCNT (0x28)
  98. #define SUN4I_SPDIF_TXCHSTA0 (0x2C)
  99. #define SUN4I_SPDIF_TXCHSTA0_CLK(v) ((v) << 28)
  100. #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ(v) ((v) << 24)
  101. #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ_MASK GENMASK(27, 24)
  102. #define SUN4I_SPDIF_TXCHSTA0_CHNUM(v) ((v) << 20)
  103. #define SUN4I_SPDIF_TXCHSTA0_CHNUM_MASK GENMASK(23, 20)
  104. #define SUN4I_SPDIF_TXCHSTA0_SRCNUM(v) ((v) << 16)
  105. #define SUN4I_SPDIF_TXCHSTA0_CATACOD(v) ((v) << 8)
  106. #define SUN4I_SPDIF_TXCHSTA0_MODE(v) ((v) << 6)
  107. #define SUN4I_SPDIF_TXCHSTA0_EMPHASIS(v) ((v) << 3)
  108. #define SUN4I_SPDIF_TXCHSTA0_CP BIT(2)
  109. #define SUN4I_SPDIF_TXCHSTA0_AUDIO BIT(1)
  110. #define SUN4I_SPDIF_TXCHSTA0_PRO BIT(0)
  111. #define SUN4I_SPDIF_TXCHSTA1 (0x30)
  112. #define SUN4I_SPDIF_TXCHSTA1_CGMSA(v) ((v) << 8)
  113. #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ(v) ((v) << 4)
  114. #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ_MASK GENMASK(7, 4)
  115. #define SUN4I_SPDIF_TXCHSTA1_SAMWORDLEN(v) ((v) << 1)
  116. #define SUN4I_SPDIF_TXCHSTA1_MAXWORDLEN BIT(0)
  117. #define SUN4I_SPDIF_RXCHSTA0 (0x34)
  118. #define SUN4I_SPDIF_RXCHSTA0_CLK(v) ((v) << 28)
  119. #define SUN4I_SPDIF_RXCHSTA0_SAMFREQ(v) ((v) << 24)
  120. #define SUN4I_SPDIF_RXCHSTA0_CHNUM(v) ((v) << 20)
  121. #define SUN4I_SPDIF_RXCHSTA0_SRCNUM(v) ((v) << 16)
  122. #define SUN4I_SPDIF_RXCHSTA0_CATACOD(v) ((v) << 8)
  123. #define SUN4I_SPDIF_RXCHSTA0_MODE(v) ((v) << 6)
  124. #define SUN4I_SPDIF_RXCHSTA0_EMPHASIS(v) ((v) << 3)
  125. #define SUN4I_SPDIF_RXCHSTA0_CP BIT(2)
  126. #define SUN4I_SPDIF_RXCHSTA0_AUDIO BIT(1)
  127. #define SUN4I_SPDIF_RXCHSTA0_PRO BIT(0)
  128. #define SUN4I_SPDIF_RXCHSTA1 (0x38)
  129. #define SUN4I_SPDIF_RXCHSTA1_CGMSA(v) ((v) << 8)
  130. #define SUN4I_SPDIF_RXCHSTA1_ORISAMFREQ(v) ((v) << 4)
  131. #define SUN4I_SPDIF_RXCHSTA1_SAMWORDLEN(v) ((v) << 1)
  132. #define SUN4I_SPDIF_RXCHSTA1_MAXWORDLEN BIT(0)
  133. /* Defines for Sampling Frequency */
  134. #define SUN4I_SPDIF_SAMFREQ_44_1KHZ 0x0
  135. #define SUN4I_SPDIF_SAMFREQ_NOT_INDICATED 0x1
  136. #define SUN4I_SPDIF_SAMFREQ_48KHZ 0x2
  137. #define SUN4I_SPDIF_SAMFREQ_32KHZ 0x3
  138. #define SUN4I_SPDIF_SAMFREQ_22_05KHZ 0x4
  139. #define SUN4I_SPDIF_SAMFREQ_24KHZ 0x6
  140. #define SUN4I_SPDIF_SAMFREQ_88_2KHZ 0x8
  141. #define SUN4I_SPDIF_SAMFREQ_76_8KHZ 0x9
  142. #define SUN4I_SPDIF_SAMFREQ_96KHZ 0xa
  143. #define SUN4I_SPDIF_SAMFREQ_176_4KHZ 0xc
  144. #define SUN4I_SPDIF_SAMFREQ_192KHZ 0xe
  145. struct sun4i_spdif_dev {
  146. struct platform_device *pdev;
  147. struct clk *spdif_clk;
  148. struct clk *apb_clk;
  149. struct reset_control *rst;
  150. struct snd_soc_dai_driver cpu_dai_drv;
  151. struct regmap *regmap;
  152. struct snd_dmaengine_dai_dma_data dma_params_tx;
  153. };
  154. static void sun4i_spdif_configure(struct sun4i_spdif_dev *host)
  155. {
  156. /* soft reset SPDIF */
  157. regmap_write(host->regmap, SUN4I_SPDIF_CTL, SUN4I_SPDIF_CTL_RESET);
  158. /* flush TX FIFO */
  159. regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
  160. SUN4I_SPDIF_FCTL_FTX, SUN4I_SPDIF_FCTL_FTX);
  161. /* clear TX counter */
  162. regmap_write(host->regmap, SUN4I_SPDIF_TXCNT, 0);
  163. }
  164. static void sun4i_snd_txctrl_on(struct snd_pcm_substream *substream,
  165. struct sun4i_spdif_dev *host)
  166. {
  167. if (substream->runtime->channels == 1)
  168. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  169. SUN4I_SPDIF_TXCFG_SINGLEMOD,
  170. SUN4I_SPDIF_TXCFG_SINGLEMOD);
  171. /* SPDIF TX ENABLE */
  172. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  173. SUN4I_SPDIF_TXCFG_TXEN, SUN4I_SPDIF_TXCFG_TXEN);
  174. /* DRQ ENABLE */
  175. regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
  176. SUN4I_SPDIF_INT_TXDRQEN, SUN4I_SPDIF_INT_TXDRQEN);
  177. /* Global enable */
  178. regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
  179. SUN4I_SPDIF_CTL_GEN, SUN4I_SPDIF_CTL_GEN);
  180. }
  181. static void sun4i_snd_txctrl_off(struct snd_pcm_substream *substream,
  182. struct sun4i_spdif_dev *host)
  183. {
  184. /* SPDIF TX DISABLE */
  185. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  186. SUN4I_SPDIF_TXCFG_TXEN, 0);
  187. /* DRQ DISABLE */
  188. regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
  189. SUN4I_SPDIF_INT_TXDRQEN, 0);
  190. /* Global disable */
  191. regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
  192. SUN4I_SPDIF_CTL_GEN, 0);
  193. }
  194. static int sun4i_spdif_startup(struct snd_pcm_substream *substream,
  195. struct snd_soc_dai *cpu_dai)
  196. {
  197. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  198. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  199. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  200. return -EINVAL;
  201. sun4i_spdif_configure(host);
  202. return 0;
  203. }
  204. static int sun4i_spdif_hw_params(struct snd_pcm_substream *substream,
  205. struct snd_pcm_hw_params *params,
  206. struct snd_soc_dai *cpu_dai)
  207. {
  208. int ret = 0;
  209. int fmt;
  210. unsigned long rate = params_rate(params);
  211. u32 mclk_div = 0;
  212. unsigned int mclk = 0;
  213. u32 reg_val;
  214. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  215. struct platform_device *pdev = host->pdev;
  216. /* Add the PCM and raw data select interface */
  217. switch (params_channels(params)) {
  218. case 1: /* PCM mode */
  219. case 2:
  220. fmt = 0;
  221. break;
  222. case 4: /* raw data mode */
  223. fmt = SUN4I_SPDIF_TXCFG_NONAUDIO;
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. switch (params_format(params)) {
  229. case SNDRV_PCM_FORMAT_S16_LE:
  230. fmt |= SUN4I_SPDIF_TXCFG_FMT16BIT;
  231. break;
  232. case SNDRV_PCM_FORMAT_S20_3LE:
  233. fmt |= SUN4I_SPDIF_TXCFG_FMT20BIT;
  234. break;
  235. case SNDRV_PCM_FORMAT_S24_LE:
  236. fmt |= SUN4I_SPDIF_TXCFG_FMT24BIT;
  237. break;
  238. default:
  239. return -EINVAL;
  240. }
  241. switch (rate) {
  242. case 22050:
  243. case 44100:
  244. case 88200:
  245. case 176400:
  246. mclk = 22579200;
  247. break;
  248. case 24000:
  249. case 32000:
  250. case 48000:
  251. case 96000:
  252. case 192000:
  253. mclk = 24576000;
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. ret = clk_set_rate(host->spdif_clk, mclk);
  259. if (ret < 0) {
  260. dev_err(&pdev->dev,
  261. "Setting SPDIF clock rate for %d Hz failed!\n", mclk);
  262. return ret;
  263. }
  264. regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
  265. SUN4I_SPDIF_FCTL_TXIM, SUN4I_SPDIF_FCTL_TXIM);
  266. switch (rate) {
  267. case 22050:
  268. case 24000:
  269. mclk_div = 8;
  270. break;
  271. case 32000:
  272. mclk_div = 6;
  273. break;
  274. case 44100:
  275. case 48000:
  276. mclk_div = 4;
  277. break;
  278. case 88200:
  279. case 96000:
  280. mclk_div = 2;
  281. break;
  282. case 176400:
  283. case 192000:
  284. mclk_div = 1;
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. reg_val = 0;
  290. reg_val |= SUN4I_SPDIF_TXCFG_ASS;
  291. reg_val |= fmt; /* set non audio and bit depth */
  292. reg_val |= SUN4I_SPDIF_TXCFG_CHSTMODE;
  293. reg_val |= SUN4I_SPDIF_TXCFG_TXRATIO(mclk_div - 1);
  294. regmap_write(host->regmap, SUN4I_SPDIF_TXCFG, reg_val);
  295. return 0;
  296. }
  297. static int sun4i_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
  298. struct snd_soc_dai *dai)
  299. {
  300. int ret = 0;
  301. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
  302. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  303. return -EINVAL;
  304. switch (cmd) {
  305. case SNDRV_PCM_TRIGGER_START:
  306. case SNDRV_PCM_TRIGGER_RESUME:
  307. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  308. sun4i_snd_txctrl_on(substream, host);
  309. break;
  310. case SNDRV_PCM_TRIGGER_STOP:
  311. case SNDRV_PCM_TRIGGER_SUSPEND:
  312. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  313. sun4i_snd_txctrl_off(substream, host);
  314. break;
  315. default:
  316. ret = -EINVAL;
  317. break;
  318. }
  319. return ret;
  320. }
  321. static int sun4i_spdif_soc_dai_probe(struct snd_soc_dai *dai)
  322. {
  323. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
  324. snd_soc_dai_init_dma_data(dai, &host->dma_params_tx, NULL);
  325. return 0;
  326. }
  327. static const struct snd_soc_dai_ops sun4i_spdif_dai_ops = {
  328. .startup = sun4i_spdif_startup,
  329. .trigger = sun4i_spdif_trigger,
  330. .hw_params = sun4i_spdif_hw_params,
  331. };
  332. static const struct regmap_config sun4i_spdif_regmap_config = {
  333. .reg_bits = 32,
  334. .reg_stride = 4,
  335. .val_bits = 32,
  336. .max_register = SUN4I_SPDIF_RXCHSTA1,
  337. };
  338. #define SUN4I_RATES SNDRV_PCM_RATE_8000_192000
  339. #define SUN4I_FORMATS (SNDRV_PCM_FORMAT_S16_LE | \
  340. SNDRV_PCM_FORMAT_S20_3LE | \
  341. SNDRV_PCM_FORMAT_S24_LE)
  342. static struct snd_soc_dai_driver sun4i_spdif_dai = {
  343. .playback = {
  344. .channels_min = 1,
  345. .channels_max = 2,
  346. .rates = SUN4I_RATES,
  347. .formats = SUN4I_FORMATS,
  348. },
  349. .probe = sun4i_spdif_soc_dai_probe,
  350. .ops = &sun4i_spdif_dai_ops,
  351. .name = "spdif",
  352. };
  353. struct sun4i_spdif_quirks {
  354. unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
  355. bool has_reset;
  356. };
  357. static const struct sun4i_spdif_quirks sun4i_a10_spdif_quirks = {
  358. .reg_dac_txdata = SUN4I_SPDIF_TXFIFO,
  359. };
  360. static const struct sun4i_spdif_quirks sun6i_a31_spdif_quirks = {
  361. .reg_dac_txdata = SUN4I_SPDIF_TXFIFO,
  362. .has_reset = true,
  363. };
  364. static const struct sun4i_spdif_quirks sun8i_h3_spdif_quirks = {
  365. .reg_dac_txdata = SUN8I_SPDIF_TXFIFO,
  366. .has_reset = true,
  367. };
  368. static const struct of_device_id sun4i_spdif_of_match[] = {
  369. {
  370. .compatible = "allwinner,sun4i-a10-spdif",
  371. .data = &sun4i_a10_spdif_quirks,
  372. },
  373. {
  374. .compatible = "allwinner,sun6i-a31-spdif",
  375. .data = &sun6i_a31_spdif_quirks,
  376. },
  377. {
  378. .compatible = "allwinner,sun8i-h3-spdif",
  379. .data = &sun8i_h3_spdif_quirks,
  380. },
  381. { /* sentinel */ }
  382. };
  383. MODULE_DEVICE_TABLE(of, sun4i_spdif_of_match);
  384. static const struct snd_soc_component_driver sun4i_spdif_component = {
  385. .name = "sun4i-spdif",
  386. };
  387. static int sun4i_spdif_runtime_suspend(struct device *dev)
  388. {
  389. struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
  390. clk_disable_unprepare(host->spdif_clk);
  391. clk_disable_unprepare(host->apb_clk);
  392. return 0;
  393. }
  394. static int sun4i_spdif_runtime_resume(struct device *dev)
  395. {
  396. struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
  397. int ret;
  398. ret = clk_prepare_enable(host->spdif_clk);
  399. if (ret)
  400. return ret;
  401. ret = clk_prepare_enable(host->apb_clk);
  402. if (ret)
  403. clk_disable_unprepare(host->spdif_clk);
  404. return ret;
  405. }
  406. static int sun4i_spdif_probe(struct platform_device *pdev)
  407. {
  408. struct sun4i_spdif_dev *host;
  409. struct resource *res;
  410. const struct sun4i_spdif_quirks *quirks;
  411. int ret;
  412. void __iomem *base;
  413. dev_dbg(&pdev->dev, "Entered %s\n", __func__);
  414. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  415. if (!host)
  416. return -ENOMEM;
  417. host->pdev = pdev;
  418. /* Initialize this copy of the CPU DAI driver structure */
  419. memcpy(&host->cpu_dai_drv, &sun4i_spdif_dai, sizeof(sun4i_spdif_dai));
  420. host->cpu_dai_drv.name = dev_name(&pdev->dev);
  421. /* Get the addresses */
  422. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  423. base = devm_ioremap_resource(&pdev->dev, res);
  424. if (IS_ERR(base))
  425. return PTR_ERR(base);
  426. quirks = of_device_get_match_data(&pdev->dev);
  427. if (quirks == NULL) {
  428. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  429. return -ENODEV;
  430. }
  431. host->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  432. &sun4i_spdif_regmap_config);
  433. /* Clocks */
  434. host->apb_clk = devm_clk_get(&pdev->dev, "apb");
  435. if (IS_ERR(host->apb_clk)) {
  436. dev_err(&pdev->dev, "failed to get a apb clock.\n");
  437. return PTR_ERR(host->apb_clk);
  438. }
  439. host->spdif_clk = devm_clk_get(&pdev->dev, "spdif");
  440. if (IS_ERR(host->spdif_clk)) {
  441. dev_err(&pdev->dev, "failed to get a spdif clock.\n");
  442. return PTR_ERR(host->spdif_clk);
  443. }
  444. host->dma_params_tx.addr = res->start + quirks->reg_dac_txdata;
  445. host->dma_params_tx.maxburst = 8;
  446. host->dma_params_tx.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  447. platform_set_drvdata(pdev, host);
  448. if (quirks->has_reset) {
  449. host->rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
  450. NULL);
  451. if (IS_ERR(host->rst) && PTR_ERR(host->rst) == -EPROBE_DEFER) {
  452. ret = -EPROBE_DEFER;
  453. dev_err(&pdev->dev, "Failed to get reset: %d\n", ret);
  454. return ret;
  455. }
  456. if (!IS_ERR(host->rst))
  457. reset_control_deassert(host->rst);
  458. }
  459. ret = devm_snd_soc_register_component(&pdev->dev,
  460. &sun4i_spdif_component, &sun4i_spdif_dai, 1);
  461. if (ret)
  462. return ret;
  463. pm_runtime_enable(&pdev->dev);
  464. if (!pm_runtime_enabled(&pdev->dev)) {
  465. ret = sun4i_spdif_runtime_resume(&pdev->dev);
  466. if (ret)
  467. goto err_unregister;
  468. }
  469. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  470. if (ret)
  471. goto err_suspend;
  472. return 0;
  473. err_suspend:
  474. if (!pm_runtime_status_suspended(&pdev->dev))
  475. sun4i_spdif_runtime_suspend(&pdev->dev);
  476. err_unregister:
  477. pm_runtime_disable(&pdev->dev);
  478. return ret;
  479. }
  480. static int sun4i_spdif_remove(struct platform_device *pdev)
  481. {
  482. pm_runtime_disable(&pdev->dev);
  483. if (!pm_runtime_status_suspended(&pdev->dev))
  484. sun4i_spdif_runtime_suspend(&pdev->dev);
  485. return 0;
  486. }
  487. static const struct dev_pm_ops sun4i_spdif_pm = {
  488. SET_RUNTIME_PM_OPS(sun4i_spdif_runtime_suspend,
  489. sun4i_spdif_runtime_resume, NULL)
  490. };
  491. static struct platform_driver sun4i_spdif_driver = {
  492. .driver = {
  493. .name = "sun4i-spdif",
  494. .of_match_table = of_match_ptr(sun4i_spdif_of_match),
  495. .pm = &sun4i_spdif_pm,
  496. },
  497. .probe = sun4i_spdif_probe,
  498. .remove = sun4i_spdif_remove,
  499. };
  500. module_platform_driver(sun4i_spdif_driver);
  501. MODULE_AUTHOR("Marcus Cooper <codekipper@gmail.com>");
  502. MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
  503. MODULE_DESCRIPTION("Allwinner sun4i SPDIF SoC Interface");
  504. MODULE_LICENSE("GPL");
  505. MODULE_ALIAS("platform:sun4i-spdif");