sun4i-spdif.c 20 KB

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