sun4i-spdif.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA SoC SPDIF Audio Layer
  4. *
  5. * Copyright 2015 Andrea Venturi <be17068@iperbole.bo.it>
  6. * Copyright 2015 Marcus Cooper <codekipper@gmail.com>
  7. *
  8. * Based on the Allwinner SDK driver, released under the GPL.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/regmap.h>
  16. #include <linux/of.h>
  17. #include <linux/ioport.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/reset.h>
  22. #include <linux/spinlock.h>
  23. #include <sound/asoundef.h>
  24. #include <sound/dmaengine_pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #define SUN4I_SPDIF_CTL (0x00)
  28. #define SUN4I_SPDIF_CTL_MCLKDIV(v) ((v) << 4) /* v even */
  29. #define SUN4I_SPDIF_CTL_MCLKOUTEN BIT(2)
  30. #define SUN4I_SPDIF_CTL_GEN BIT(1)
  31. #define SUN4I_SPDIF_CTL_RESET BIT(0)
  32. #define SUN4I_SPDIF_TXCFG (0x04)
  33. #define SUN4I_SPDIF_TXCFG_SINGLEMOD BIT(31)
  34. #define SUN4I_SPDIF_TXCFG_ASS BIT(17)
  35. #define SUN4I_SPDIF_TXCFG_NONAUDIO BIT(16)
  36. #define SUN4I_SPDIF_TXCFG_TXRATIO(v) ((v) << 4)
  37. #define SUN4I_SPDIF_TXCFG_TXRATIO_MASK GENMASK(8, 4)
  38. #define SUN4I_SPDIF_TXCFG_FMTRVD GENMASK(3, 2)
  39. #define SUN4I_SPDIF_TXCFG_FMT16BIT (0 << 2)
  40. #define SUN4I_SPDIF_TXCFG_FMT20BIT (1 << 2)
  41. #define SUN4I_SPDIF_TXCFG_FMT24BIT (2 << 2)
  42. #define SUN4I_SPDIF_TXCFG_CHSTMODE BIT(1)
  43. #define SUN4I_SPDIF_TXCFG_TXEN BIT(0)
  44. #define SUN4I_SPDIF_RXCFG (0x08)
  45. #define SUN4I_SPDIF_RXCFG_LOCKFLAG BIT(4)
  46. #define SUN4I_SPDIF_RXCFG_CHSTSRC BIT(3)
  47. #define SUN4I_SPDIF_RXCFG_CHSTCP BIT(1)
  48. #define SUN4I_SPDIF_RXCFG_RXEN BIT(0)
  49. #define SUN4I_SPDIF_TXFIFO (0x0C)
  50. #define SUN4I_SPDIF_RXFIFO (0x10)
  51. #define SUN4I_SPDIF_FCTL (0x14)
  52. #define SUN4I_SPDIF_FCTL_FIFOSRC BIT(31)
  53. #define SUN4I_SPDIF_FCTL_FTX BIT(17)
  54. #define SUN4I_SPDIF_FCTL_FRX BIT(16)
  55. #define SUN4I_SPDIF_FCTL_TXTL(v) ((v) << 8)
  56. #define SUN4I_SPDIF_FCTL_TXTL_MASK GENMASK(12, 8)
  57. #define SUN4I_SPDIF_FCTL_RXTL(v) ((v) << 3)
  58. #define SUN4I_SPDIF_FCTL_RXTL_MASK GENMASK(7, 3)
  59. #define SUN4I_SPDIF_FCTL_TXIM BIT(2)
  60. #define SUN4I_SPDIF_FCTL_RXOM(v) ((v) << 0)
  61. #define SUN4I_SPDIF_FCTL_RXOM_MASK GENMASK(1, 0)
  62. #define SUN50I_H6_SPDIF_FCTL (0x14)
  63. #define SUN50I_H6_SPDIF_FCTL_HUB_EN BIT(31)
  64. #define SUN50I_H6_SPDIF_FCTL_FTX BIT(30)
  65. #define SUN50I_H6_SPDIF_FCTL_FRX BIT(29)
  66. #define SUN50I_H6_SPDIF_FCTL_TXTL(v) ((v) << 12)
  67. #define SUN50I_H6_SPDIF_FCTL_TXTL_MASK GENMASK(19, 12)
  68. #define SUN50I_H6_SPDIF_FCTL_RXTL(v) ((v) << 4)
  69. #define SUN50I_H6_SPDIF_FCTL_RXTL_MASK GENMASK(10, 4)
  70. #define SUN50I_H6_SPDIF_FCTL_TXIM BIT(2)
  71. #define SUN50I_H6_SPDIF_FCTL_RXOM(v) ((v) << 0)
  72. #define SUN50I_H6_SPDIF_FCTL_RXOM_MASK GENMASK(1, 0)
  73. #define SUN4I_SPDIF_FSTA (0x18)
  74. #define SUN4I_SPDIF_FSTA_TXE BIT(14)
  75. #define SUN4I_SPDIF_FSTA_TXECNTSHT (8)
  76. #define SUN4I_SPDIF_FSTA_RXA BIT(6)
  77. #define SUN4I_SPDIF_FSTA_RXACNTSHT (0)
  78. #define SUN4I_SPDIF_INT (0x1C)
  79. #define SUN4I_SPDIF_INT_RXLOCKEN BIT(18)
  80. #define SUN4I_SPDIF_INT_RXUNLOCKEN BIT(17)
  81. #define SUN4I_SPDIF_INT_RXPARERREN BIT(16)
  82. #define SUN4I_SPDIF_INT_TXDRQEN BIT(7)
  83. #define SUN4I_SPDIF_INT_TXUIEN BIT(6)
  84. #define SUN4I_SPDIF_INT_TXOIEN BIT(5)
  85. #define SUN4I_SPDIF_INT_TXEIEN BIT(4)
  86. #define SUN4I_SPDIF_INT_RXDRQEN BIT(2)
  87. #define SUN4I_SPDIF_INT_RXOIEN BIT(1)
  88. #define SUN4I_SPDIF_INT_RXAIEN BIT(0)
  89. #define SUN4I_SPDIF_ISTA (0x20)
  90. #define SUN4I_SPDIF_ISTA_RXLOCKSTA BIT(18)
  91. #define SUN4I_SPDIF_ISTA_RXUNLOCKSTA BIT(17)
  92. #define SUN4I_SPDIF_ISTA_RXPARERRSTA BIT(16)
  93. #define SUN4I_SPDIF_ISTA_TXUSTA BIT(6)
  94. #define SUN4I_SPDIF_ISTA_TXOSTA BIT(5)
  95. #define SUN4I_SPDIF_ISTA_TXESTA BIT(4)
  96. #define SUN4I_SPDIF_ISTA_RXOSTA BIT(1)
  97. #define SUN4I_SPDIF_ISTA_RXASTA BIT(0)
  98. #define SUN8I_SPDIF_TXFIFO (0x20)
  99. #define SUN4I_SPDIF_TXCNT (0x24)
  100. #define SUN4I_SPDIF_RXCNT (0x28)
  101. #define SUN4I_SPDIF_TXCHSTA0 (0x2C)
  102. #define SUN4I_SPDIF_TXCHSTA0_CLK(v) ((v) << 28)
  103. #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ(v) ((v) << 24)
  104. #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ_MASK GENMASK(27, 24)
  105. #define SUN4I_SPDIF_TXCHSTA0_CHNUM(v) ((v) << 20)
  106. #define SUN4I_SPDIF_TXCHSTA0_CHNUM_MASK GENMASK(23, 20)
  107. #define SUN4I_SPDIF_TXCHSTA0_SRCNUM(v) ((v) << 16)
  108. #define SUN4I_SPDIF_TXCHSTA0_CATACOD(v) ((v) << 8)
  109. #define SUN4I_SPDIF_TXCHSTA0_MODE(v) ((v) << 6)
  110. #define SUN4I_SPDIF_TXCHSTA0_EMPHASIS(v) ((v) << 3)
  111. #define SUN4I_SPDIF_TXCHSTA0_CP BIT(2)
  112. #define SUN4I_SPDIF_TXCHSTA0_AUDIO BIT(1)
  113. #define SUN4I_SPDIF_TXCHSTA0_PRO BIT(0)
  114. #define SUN4I_SPDIF_TXCHSTA1 (0x30)
  115. #define SUN4I_SPDIF_TXCHSTA1_CGMSA(v) ((v) << 8)
  116. #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ(v) ((v) << 4)
  117. #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ_MASK GENMASK(7, 4)
  118. #define SUN4I_SPDIF_TXCHSTA1_SAMWORDLEN(v) ((v) << 1)
  119. #define SUN4I_SPDIF_TXCHSTA1_MAXWORDLEN BIT(0)
  120. #define SUN4I_SPDIF_RXCHSTA0 (0x34)
  121. #define SUN4I_SPDIF_RXCHSTA0_CLK(v) ((v) << 28)
  122. #define SUN4I_SPDIF_RXCHSTA0_SAMFREQ(v) ((v) << 24)
  123. #define SUN4I_SPDIF_RXCHSTA0_CHNUM(v) ((v) << 20)
  124. #define SUN4I_SPDIF_RXCHSTA0_SRCNUM(v) ((v) << 16)
  125. #define SUN4I_SPDIF_RXCHSTA0_CATACOD(v) ((v) << 8)
  126. #define SUN4I_SPDIF_RXCHSTA0_MODE(v) ((v) << 6)
  127. #define SUN4I_SPDIF_RXCHSTA0_EMPHASIS(v) ((v) << 3)
  128. #define SUN4I_SPDIF_RXCHSTA0_CP BIT(2)
  129. #define SUN4I_SPDIF_RXCHSTA0_AUDIO BIT(1)
  130. #define SUN4I_SPDIF_RXCHSTA0_PRO BIT(0)
  131. #define SUN4I_SPDIF_RXCHSTA1 (0x38)
  132. #define SUN4I_SPDIF_RXCHSTA1_CGMSA(v) ((v) << 8)
  133. #define SUN4I_SPDIF_RXCHSTA1_ORISAMFREQ(v) ((v) << 4)
  134. #define SUN4I_SPDIF_RXCHSTA1_SAMWORDLEN(v) ((v) << 1)
  135. #define SUN4I_SPDIF_RXCHSTA1_MAXWORDLEN BIT(0)
  136. /* Defines for Sampling Frequency */
  137. #define SUN4I_SPDIF_SAMFREQ_44_1KHZ 0x0
  138. #define SUN4I_SPDIF_SAMFREQ_NOT_INDICATED 0x1
  139. #define SUN4I_SPDIF_SAMFREQ_48KHZ 0x2
  140. #define SUN4I_SPDIF_SAMFREQ_32KHZ 0x3
  141. #define SUN4I_SPDIF_SAMFREQ_22_05KHZ 0x4
  142. #define SUN4I_SPDIF_SAMFREQ_24KHZ 0x6
  143. #define SUN4I_SPDIF_SAMFREQ_88_2KHZ 0x8
  144. #define SUN4I_SPDIF_SAMFREQ_76_8KHZ 0x9
  145. #define SUN4I_SPDIF_SAMFREQ_96KHZ 0xa
  146. #define SUN4I_SPDIF_SAMFREQ_176_4KHZ 0xc
  147. #define SUN4I_SPDIF_SAMFREQ_192KHZ 0xe
  148. /**
  149. * struct sun4i_spdif_quirks - Differences between SoC variants.
  150. *
  151. * @reg_dac_txdata: TX FIFO offset for DMA config.
  152. * @has_reset: SoC needs reset deasserted.
  153. * @val_fctl_ftx: TX FIFO flush bitmask.
  154. */
  155. struct sun4i_spdif_quirks {
  156. unsigned int reg_dac_txdata;
  157. bool has_reset;
  158. unsigned int val_fctl_ftx;
  159. unsigned int mclk_multiplier;
  160. };
  161. struct sun4i_spdif_dev {
  162. struct platform_device *pdev;
  163. struct clk *spdif_clk;
  164. struct clk *apb_clk;
  165. struct reset_control *rst;
  166. struct snd_soc_dai_driver cpu_dai_drv;
  167. struct regmap *regmap;
  168. struct snd_dmaengine_dai_dma_data dma_params_tx;
  169. const struct sun4i_spdif_quirks *quirks;
  170. spinlock_t lock;
  171. };
  172. static void sun4i_spdif_configure(struct sun4i_spdif_dev *host)
  173. {
  174. const struct sun4i_spdif_quirks *quirks = host->quirks;
  175. /* soft reset SPDIF */
  176. regmap_write(host->regmap, SUN4I_SPDIF_CTL, SUN4I_SPDIF_CTL_RESET);
  177. /* flush TX FIFO */
  178. regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
  179. quirks->val_fctl_ftx, quirks->val_fctl_ftx);
  180. /* clear TX counter */
  181. regmap_write(host->regmap, SUN4I_SPDIF_TXCNT, 0);
  182. }
  183. static void sun4i_snd_txctrl_on(struct snd_pcm_substream *substream,
  184. struct sun4i_spdif_dev *host)
  185. {
  186. if (substream->runtime->channels == 1)
  187. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  188. SUN4I_SPDIF_TXCFG_SINGLEMOD,
  189. SUN4I_SPDIF_TXCFG_SINGLEMOD);
  190. /* SPDIF TX ENABLE */
  191. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  192. SUN4I_SPDIF_TXCFG_TXEN, SUN4I_SPDIF_TXCFG_TXEN);
  193. /* DRQ ENABLE */
  194. regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
  195. SUN4I_SPDIF_INT_TXDRQEN, SUN4I_SPDIF_INT_TXDRQEN);
  196. /* Global enable */
  197. regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
  198. SUN4I_SPDIF_CTL_GEN, SUN4I_SPDIF_CTL_GEN);
  199. }
  200. static void sun4i_snd_txctrl_off(struct snd_pcm_substream *substream,
  201. struct sun4i_spdif_dev *host)
  202. {
  203. /* SPDIF TX DISABLE */
  204. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  205. SUN4I_SPDIF_TXCFG_TXEN, 0);
  206. /* DRQ DISABLE */
  207. regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
  208. SUN4I_SPDIF_INT_TXDRQEN, 0);
  209. /* Global disable */
  210. regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
  211. SUN4I_SPDIF_CTL_GEN, 0);
  212. }
  213. static int sun4i_spdif_startup(struct snd_pcm_substream *substream,
  214. struct snd_soc_dai *cpu_dai)
  215. {
  216. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  217. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  218. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  219. return -EINVAL;
  220. sun4i_spdif_configure(host);
  221. return 0;
  222. }
  223. static int sun4i_spdif_hw_params(struct snd_pcm_substream *substream,
  224. struct snd_pcm_hw_params *params,
  225. struct snd_soc_dai *cpu_dai)
  226. {
  227. int ret = 0;
  228. int fmt;
  229. unsigned long rate = params_rate(params);
  230. u32 mclk_div = 0;
  231. unsigned int mclk = 0;
  232. u32 reg_val;
  233. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  234. struct platform_device *pdev = host->pdev;
  235. /* Add the PCM and raw data select interface */
  236. switch (params_channels(params)) {
  237. case 1: /* PCM mode */
  238. case 2:
  239. fmt = 0;
  240. break;
  241. case 4: /* raw data mode */
  242. fmt = SUN4I_SPDIF_TXCFG_NONAUDIO;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. switch (params_format(params)) {
  248. case SNDRV_PCM_FORMAT_S16_LE:
  249. fmt |= SUN4I_SPDIF_TXCFG_FMT16BIT;
  250. break;
  251. case SNDRV_PCM_FORMAT_S20_3LE:
  252. fmt |= SUN4I_SPDIF_TXCFG_FMT20BIT;
  253. break;
  254. case SNDRV_PCM_FORMAT_S24_LE:
  255. fmt |= SUN4I_SPDIF_TXCFG_FMT24BIT;
  256. break;
  257. default:
  258. return -EINVAL;
  259. }
  260. switch (rate) {
  261. case 22050:
  262. case 44100:
  263. case 88200:
  264. case 176400:
  265. mclk = 22579200;
  266. break;
  267. case 24000:
  268. case 32000:
  269. case 48000:
  270. case 96000:
  271. case 192000:
  272. mclk = 24576000;
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. mclk *= host->quirks->mclk_multiplier;
  278. ret = clk_set_rate(host->spdif_clk, mclk);
  279. if (ret < 0) {
  280. dev_err(&pdev->dev,
  281. "Setting SPDIF clock rate for %d Hz failed!\n", mclk);
  282. return ret;
  283. }
  284. regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
  285. SUN4I_SPDIF_FCTL_TXIM, SUN4I_SPDIF_FCTL_TXIM);
  286. switch (rate) {
  287. case 22050:
  288. case 24000:
  289. mclk_div = 8;
  290. break;
  291. case 32000:
  292. mclk_div = 6;
  293. break;
  294. case 44100:
  295. case 48000:
  296. mclk_div = 4;
  297. break;
  298. case 88200:
  299. case 96000:
  300. mclk_div = 2;
  301. break;
  302. case 176400:
  303. case 192000:
  304. mclk_div = 1;
  305. break;
  306. default:
  307. return -EINVAL;
  308. }
  309. mclk_div *= host->quirks->mclk_multiplier;
  310. reg_val = 0;
  311. reg_val |= SUN4I_SPDIF_TXCFG_ASS;
  312. reg_val |= fmt; /* set non audio and bit depth */
  313. reg_val |= SUN4I_SPDIF_TXCFG_CHSTMODE;
  314. reg_val |= SUN4I_SPDIF_TXCFG_TXRATIO(mclk_div - 1);
  315. regmap_write(host->regmap, SUN4I_SPDIF_TXCFG, reg_val);
  316. return 0;
  317. }
  318. static int sun4i_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
  319. struct snd_soc_dai *dai)
  320. {
  321. int ret = 0;
  322. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
  323. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  324. return -EINVAL;
  325. switch (cmd) {
  326. case SNDRV_PCM_TRIGGER_START:
  327. case SNDRV_PCM_TRIGGER_RESUME:
  328. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  329. sun4i_snd_txctrl_on(substream, host);
  330. break;
  331. case SNDRV_PCM_TRIGGER_STOP:
  332. case SNDRV_PCM_TRIGGER_SUSPEND:
  333. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  334. sun4i_snd_txctrl_off(substream, host);
  335. break;
  336. default:
  337. ret = -EINVAL;
  338. break;
  339. }
  340. return ret;
  341. }
  342. static int sun4i_spdif_info(struct snd_kcontrol *kcontrol,
  343. struct snd_ctl_elem_info *uinfo)
  344. {
  345. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  346. uinfo->count = 1;
  347. return 0;
  348. }
  349. static int sun4i_spdif_get_status_mask(struct snd_kcontrol *kcontrol,
  350. struct snd_ctl_elem_value *ucontrol)
  351. {
  352. u8 *status = ucontrol->value.iec958.status;
  353. status[0] = 0xff;
  354. status[1] = 0xff;
  355. status[2] = 0xff;
  356. status[3] = 0xff;
  357. status[4] = 0xff;
  358. status[5] = 0x03;
  359. return 0;
  360. }
  361. static int sun4i_spdif_get_status(struct snd_kcontrol *kcontrol,
  362. struct snd_ctl_elem_value *ucontrol)
  363. {
  364. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  365. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  366. u8 *status = ucontrol->value.iec958.status;
  367. unsigned long flags;
  368. unsigned int reg;
  369. spin_lock_irqsave(&host->lock, flags);
  370. regmap_read(host->regmap, SUN4I_SPDIF_TXCHSTA0, &reg);
  371. status[0] = reg & 0xff;
  372. status[1] = (reg >> 8) & 0xff;
  373. status[2] = (reg >> 16) & 0xff;
  374. status[3] = (reg >> 24) & 0xff;
  375. regmap_read(host->regmap, SUN4I_SPDIF_TXCHSTA1, &reg);
  376. status[4] = reg & 0xff;
  377. status[5] = (reg >> 8) & 0x3;
  378. spin_unlock_irqrestore(&host->lock, flags);
  379. return 0;
  380. }
  381. static int sun4i_spdif_set_status(struct snd_kcontrol *kcontrol,
  382. struct snd_ctl_elem_value *ucontrol)
  383. {
  384. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  385. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  386. u8 *status = ucontrol->value.iec958.status;
  387. unsigned long flags;
  388. unsigned int reg;
  389. bool chg0, chg1;
  390. spin_lock_irqsave(&host->lock, flags);
  391. reg = (u32)status[3] << 24;
  392. reg |= (u32)status[2] << 16;
  393. reg |= (u32)status[1] << 8;
  394. reg |= (u32)status[0];
  395. regmap_update_bits_check(host->regmap, SUN4I_SPDIF_TXCHSTA0,
  396. GENMASK(31,0), reg, &chg0);
  397. reg = (u32)status[5] << 8;
  398. reg |= (u32)status[4];
  399. regmap_update_bits_check(host->regmap, SUN4I_SPDIF_TXCHSTA1,
  400. GENMASK(9,0), reg, &chg1);
  401. reg = SUN4I_SPDIF_TXCFG_CHSTMODE;
  402. if (status[0] & IEC958_AES0_NONAUDIO)
  403. reg |= SUN4I_SPDIF_TXCFG_NONAUDIO;
  404. regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
  405. SUN4I_SPDIF_TXCFG_CHSTMODE |
  406. SUN4I_SPDIF_TXCFG_NONAUDIO, reg);
  407. spin_unlock_irqrestore(&host->lock, flags);
  408. return chg0 || chg1;
  409. }
  410. static struct snd_kcontrol_new sun4i_spdif_controls[] = {
  411. {
  412. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  413. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  414. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
  415. .info = sun4i_spdif_info,
  416. .get = sun4i_spdif_get_status_mask
  417. },
  418. {
  419. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  420. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  421. .info = sun4i_spdif_info,
  422. .get = sun4i_spdif_get_status,
  423. .put = sun4i_spdif_set_status
  424. }
  425. };
  426. static int sun4i_spdif_soc_dai_probe(struct snd_soc_dai *dai)
  427. {
  428. struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
  429. snd_soc_dai_init_dma_data(dai, &host->dma_params_tx, NULL);
  430. snd_soc_add_dai_controls(dai, sun4i_spdif_controls,
  431. ARRAY_SIZE(sun4i_spdif_controls));
  432. return 0;
  433. }
  434. static const struct snd_soc_dai_ops sun4i_spdif_dai_ops = {
  435. .probe = sun4i_spdif_soc_dai_probe,
  436. .startup = sun4i_spdif_startup,
  437. .trigger = sun4i_spdif_trigger,
  438. .hw_params = sun4i_spdif_hw_params,
  439. };
  440. static const struct regmap_config sun4i_spdif_regmap_config = {
  441. .reg_bits = 32,
  442. .reg_stride = 4,
  443. .val_bits = 32,
  444. .max_register = SUN4I_SPDIF_RXCHSTA1,
  445. };
  446. #define SUN4I_RATES SNDRV_PCM_RATE_8000_192000
  447. #define SUN4I_FORMATS (SNDRV_PCM_FORMAT_S16_LE | \
  448. SNDRV_PCM_FORMAT_S20_3LE | \
  449. SNDRV_PCM_FORMAT_S24_LE)
  450. static struct snd_soc_dai_driver sun4i_spdif_dai = {
  451. .playback = {
  452. .channels_min = 1,
  453. .channels_max = 2,
  454. .rates = SUN4I_RATES,
  455. .formats = SUN4I_FORMATS,
  456. },
  457. .ops = &sun4i_spdif_dai_ops,
  458. .name = "spdif",
  459. };
  460. static const struct sun4i_spdif_quirks sun4i_a10_spdif_quirks = {
  461. .reg_dac_txdata = SUN4I_SPDIF_TXFIFO,
  462. .val_fctl_ftx = SUN4I_SPDIF_FCTL_FTX,
  463. .mclk_multiplier = 1,
  464. };
  465. static const struct sun4i_spdif_quirks sun6i_a31_spdif_quirks = {
  466. .reg_dac_txdata = SUN4I_SPDIF_TXFIFO,
  467. .val_fctl_ftx = SUN4I_SPDIF_FCTL_FTX,
  468. .has_reset = true,
  469. .mclk_multiplier = 1,
  470. };
  471. static const struct sun4i_spdif_quirks sun8i_h3_spdif_quirks = {
  472. .reg_dac_txdata = SUN8I_SPDIF_TXFIFO,
  473. .val_fctl_ftx = SUN4I_SPDIF_FCTL_FTX,
  474. .has_reset = true,
  475. .mclk_multiplier = 4,
  476. };
  477. static const struct sun4i_spdif_quirks sun50i_h6_spdif_quirks = {
  478. .reg_dac_txdata = SUN8I_SPDIF_TXFIFO,
  479. .val_fctl_ftx = SUN50I_H6_SPDIF_FCTL_FTX,
  480. .has_reset = true,
  481. .mclk_multiplier = 1,
  482. };
  483. static const struct of_device_id sun4i_spdif_of_match[] = {
  484. {
  485. .compatible = "allwinner,sun4i-a10-spdif",
  486. .data = &sun4i_a10_spdif_quirks,
  487. },
  488. {
  489. .compatible = "allwinner,sun6i-a31-spdif",
  490. .data = &sun6i_a31_spdif_quirks,
  491. },
  492. {
  493. .compatible = "allwinner,sun8i-h3-spdif",
  494. .data = &sun8i_h3_spdif_quirks,
  495. },
  496. {
  497. .compatible = "allwinner,sun50i-h6-spdif",
  498. .data = &sun50i_h6_spdif_quirks,
  499. },
  500. {
  501. .compatible = "allwinner,sun50i-h616-spdif",
  502. /* Essentially the same as the H6, but without RX */
  503. .data = &sun50i_h6_spdif_quirks,
  504. },
  505. { /* sentinel */ }
  506. };
  507. MODULE_DEVICE_TABLE(of, sun4i_spdif_of_match);
  508. static const struct snd_soc_component_driver sun4i_spdif_component = {
  509. .name = "sun4i-spdif",
  510. .legacy_dai_naming = 1,
  511. };
  512. static int sun4i_spdif_runtime_suspend(struct device *dev)
  513. {
  514. struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
  515. clk_disable_unprepare(host->spdif_clk);
  516. clk_disable_unprepare(host->apb_clk);
  517. return 0;
  518. }
  519. static int sun4i_spdif_runtime_resume(struct device *dev)
  520. {
  521. struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
  522. int ret;
  523. ret = clk_prepare_enable(host->spdif_clk);
  524. if (ret)
  525. return ret;
  526. ret = clk_prepare_enable(host->apb_clk);
  527. if (ret)
  528. clk_disable_unprepare(host->spdif_clk);
  529. return ret;
  530. }
  531. static int sun4i_spdif_probe(struct platform_device *pdev)
  532. {
  533. struct sun4i_spdif_dev *host;
  534. struct resource *res;
  535. const struct sun4i_spdif_quirks *quirks;
  536. int ret;
  537. void __iomem *base;
  538. dev_dbg(&pdev->dev, "Entered %s\n", __func__);
  539. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  540. if (!host)
  541. return -ENOMEM;
  542. host->pdev = pdev;
  543. spin_lock_init(&host->lock);
  544. /* Initialize this copy of the CPU DAI driver structure */
  545. memcpy(&host->cpu_dai_drv, &sun4i_spdif_dai, sizeof(sun4i_spdif_dai));
  546. host->cpu_dai_drv.name = dev_name(&pdev->dev);
  547. /* Get the addresses */
  548. base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  549. if (IS_ERR(base))
  550. return PTR_ERR(base);
  551. quirks = of_device_get_match_data(&pdev->dev);
  552. if (quirks == NULL) {
  553. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  554. return -ENODEV;
  555. }
  556. host->quirks = quirks;
  557. host->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  558. &sun4i_spdif_regmap_config);
  559. /* Clocks */
  560. host->apb_clk = devm_clk_get(&pdev->dev, "apb");
  561. if (IS_ERR(host->apb_clk)) {
  562. dev_err(&pdev->dev, "failed to get a apb clock.\n");
  563. return PTR_ERR(host->apb_clk);
  564. }
  565. host->spdif_clk = devm_clk_get(&pdev->dev, "spdif");
  566. if (IS_ERR(host->spdif_clk)) {
  567. dev_err(&pdev->dev, "failed to get a spdif clock.\n");
  568. return PTR_ERR(host->spdif_clk);
  569. }
  570. host->dma_params_tx.addr = res->start + quirks->reg_dac_txdata;
  571. host->dma_params_tx.maxburst = 8;
  572. host->dma_params_tx.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  573. platform_set_drvdata(pdev, host);
  574. if (quirks->has_reset) {
  575. host->rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
  576. NULL);
  577. if (PTR_ERR(host->rst) == -EPROBE_DEFER) {
  578. ret = -EPROBE_DEFER;
  579. dev_err(&pdev->dev, "Failed to get reset: %d\n", ret);
  580. return ret;
  581. }
  582. if (!IS_ERR(host->rst))
  583. reset_control_deassert(host->rst);
  584. }
  585. ret = devm_snd_soc_register_component(&pdev->dev,
  586. &sun4i_spdif_component, &sun4i_spdif_dai, 1);
  587. if (ret)
  588. return ret;
  589. pm_runtime_enable(&pdev->dev);
  590. if (!pm_runtime_enabled(&pdev->dev)) {
  591. ret = sun4i_spdif_runtime_resume(&pdev->dev);
  592. if (ret)
  593. goto err_unregister;
  594. }
  595. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  596. if (ret)
  597. goto err_suspend;
  598. return 0;
  599. err_suspend:
  600. if (!pm_runtime_status_suspended(&pdev->dev))
  601. sun4i_spdif_runtime_suspend(&pdev->dev);
  602. err_unregister:
  603. pm_runtime_disable(&pdev->dev);
  604. return ret;
  605. }
  606. static void sun4i_spdif_remove(struct platform_device *pdev)
  607. {
  608. pm_runtime_disable(&pdev->dev);
  609. if (!pm_runtime_status_suspended(&pdev->dev))
  610. sun4i_spdif_runtime_suspend(&pdev->dev);
  611. }
  612. static const struct dev_pm_ops sun4i_spdif_pm = {
  613. SET_RUNTIME_PM_OPS(sun4i_spdif_runtime_suspend,
  614. sun4i_spdif_runtime_resume, NULL)
  615. };
  616. static struct platform_driver sun4i_spdif_driver = {
  617. .driver = {
  618. .name = "sun4i-spdif",
  619. .of_match_table = sun4i_spdif_of_match,
  620. .pm = &sun4i_spdif_pm,
  621. },
  622. .probe = sun4i_spdif_probe,
  623. .remove = sun4i_spdif_remove,
  624. };
  625. module_platform_driver(sun4i_spdif_driver);
  626. MODULE_AUTHOR("Marcus Cooper <codekipper@gmail.com>");
  627. MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
  628. MODULE_DESCRIPTION("Allwinner sun4i SPDIF SoC Interface");
  629. MODULE_LICENSE("GPL");
  630. MODULE_ALIAS("platform:sun4i-spdif");