stm32_sai_sub.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
  3. *
  4. * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
  5. * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
  6. *
  7. * License terms: GPL V2.0.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/regmap.h>
  24. #include <sound/asoundef.h>
  25. #include <sound/core.h>
  26. #include <sound/dmaengine_pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include "stm32_sai.h"
  29. #define SAI_FREE_PROTOCOL 0x0
  30. #define SAI_SPDIF_PROTOCOL 0x1
  31. #define SAI_SLOT_SIZE_AUTO 0x0
  32. #define SAI_SLOT_SIZE_16 0x1
  33. #define SAI_SLOT_SIZE_32 0x2
  34. #define SAI_DATASIZE_8 0x2
  35. #define SAI_DATASIZE_10 0x3
  36. #define SAI_DATASIZE_16 0x4
  37. #define SAI_DATASIZE_20 0x5
  38. #define SAI_DATASIZE_24 0x6
  39. #define SAI_DATASIZE_32 0x7
  40. #define STM_SAI_FIFO_SIZE 8
  41. #define STM_SAI_DAI_NAME_SIZE 15
  42. #define STM_SAI_IS_PLAYBACK(ip) ((ip)->dir == SNDRV_PCM_STREAM_PLAYBACK)
  43. #define STM_SAI_IS_CAPTURE(ip) ((ip)->dir == SNDRV_PCM_STREAM_CAPTURE)
  44. #define STM_SAI_A_ID 0x0
  45. #define STM_SAI_B_ID 0x1
  46. #define STM_SAI_IS_SUB_A(x) ((x)->id == STM_SAI_A_ID)
  47. #define STM_SAI_IS_SUB_B(x) ((x)->id == STM_SAI_B_ID)
  48. #define STM_SAI_BLOCK_NAME(x) (((x)->id == STM_SAI_A_ID) ? "A" : "B")
  49. #define SAI_SYNC_NONE 0x0
  50. #define SAI_SYNC_INTERNAL 0x1
  51. #define SAI_SYNC_EXTERNAL 0x2
  52. #define STM_SAI_PROTOCOL_IS_SPDIF(ip) ((ip)->spdif)
  53. #define STM_SAI_HAS_SPDIF(x) ((x)->pdata->conf->has_spdif)
  54. #define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata))
  55. #define SAI_IEC60958_BLOCK_FRAMES 192
  56. #define SAI_IEC60958_STATUS_BYTES 24
  57. /**
  58. * struct stm32_sai_sub_data - private data of SAI sub block (block A or B)
  59. * @pdev: device data pointer
  60. * @regmap: SAI register map pointer
  61. * @regmap_config: SAI sub block register map configuration pointer
  62. * @dma_params: dma configuration data for rx or tx channel
  63. * @cpu_dai_drv: DAI driver data pointer
  64. * @cpu_dai: DAI runtime data pointer
  65. * @substream: PCM substream data pointer
  66. * @pdata: SAI block parent data pointer
  67. * @np_sync_provider: synchronization provider node
  68. * @sai_ck: kernel clock feeding the SAI clock generator
  69. * @phys_addr: SAI registers physical base address
  70. * @mclk_rate: SAI block master clock frequency (Hz). set at init
  71. * @id: SAI sub block id corresponding to sub-block A or B
  72. * @dir: SAI block direction (playback or capture). set at init
  73. * @master: SAI block mode flag. (true=master, false=slave) set at init
  74. * @spdif: SAI S/PDIF iec60958 mode flag. set at init
  75. * @fmt: SAI block format. relevant only for custom protocols. set at init
  76. * @sync: SAI block synchronization mode. (none, internal or external)
  77. * @synco: SAI block ext sync source (provider setting). (none, sub-block A/B)
  78. * @synci: SAI block ext sync source (client setting). (SAI sync provider index)
  79. * @fs_length: frame synchronization length. depends on protocol settings
  80. * @slots: rx or tx slot number
  81. * @slot_width: rx or tx slot width in bits
  82. * @slot_mask: rx or tx active slots mask. set at init or at runtime
  83. * @data_size: PCM data width. corresponds to PCM substream width.
  84. * @spdif_frm_cnt: S/PDIF playback frame counter
  85. * @snd_aes_iec958: iec958 data
  86. * @ctrl_lock: control lock
  87. */
  88. struct stm32_sai_sub_data {
  89. struct platform_device *pdev;
  90. struct regmap *regmap;
  91. const struct regmap_config *regmap_config;
  92. struct snd_dmaengine_dai_dma_data dma_params;
  93. struct snd_soc_dai_driver *cpu_dai_drv;
  94. struct snd_soc_dai *cpu_dai;
  95. struct snd_pcm_substream *substream;
  96. struct stm32_sai_data *pdata;
  97. struct device_node *np_sync_provider;
  98. struct clk *sai_ck;
  99. dma_addr_t phys_addr;
  100. unsigned int mclk_rate;
  101. unsigned int id;
  102. int dir;
  103. bool master;
  104. bool spdif;
  105. int fmt;
  106. int sync;
  107. int synco;
  108. int synci;
  109. int fs_length;
  110. int slots;
  111. int slot_width;
  112. int slot_mask;
  113. int data_size;
  114. unsigned int spdif_frm_cnt;
  115. struct snd_aes_iec958 iec958;
  116. struct mutex ctrl_lock; /* protect resources accessed by controls */
  117. };
  118. enum stm32_sai_fifo_th {
  119. STM_SAI_FIFO_TH_EMPTY,
  120. STM_SAI_FIFO_TH_QUARTER,
  121. STM_SAI_FIFO_TH_HALF,
  122. STM_SAI_FIFO_TH_3_QUARTER,
  123. STM_SAI_FIFO_TH_FULL,
  124. };
  125. static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg)
  126. {
  127. switch (reg) {
  128. case STM_SAI_CR1_REGX:
  129. case STM_SAI_CR2_REGX:
  130. case STM_SAI_FRCR_REGX:
  131. case STM_SAI_SLOTR_REGX:
  132. case STM_SAI_IMR_REGX:
  133. case STM_SAI_SR_REGX:
  134. case STM_SAI_CLRFR_REGX:
  135. case STM_SAI_DR_REGX:
  136. case STM_SAI_PDMCR_REGX:
  137. case STM_SAI_PDMLY_REGX:
  138. return true;
  139. default:
  140. return false;
  141. }
  142. }
  143. static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg)
  144. {
  145. switch (reg) {
  146. case STM_SAI_DR_REGX:
  147. return true;
  148. default:
  149. return false;
  150. }
  151. }
  152. static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg)
  153. {
  154. switch (reg) {
  155. case STM_SAI_CR1_REGX:
  156. case STM_SAI_CR2_REGX:
  157. case STM_SAI_FRCR_REGX:
  158. case STM_SAI_SLOTR_REGX:
  159. case STM_SAI_IMR_REGX:
  160. case STM_SAI_SR_REGX:
  161. case STM_SAI_CLRFR_REGX:
  162. case STM_SAI_DR_REGX:
  163. case STM_SAI_PDMCR_REGX:
  164. case STM_SAI_PDMLY_REGX:
  165. return true;
  166. default:
  167. return false;
  168. }
  169. }
  170. static const struct regmap_config stm32_sai_sub_regmap_config_f4 = {
  171. .reg_bits = 32,
  172. .reg_stride = 4,
  173. .val_bits = 32,
  174. .max_register = STM_SAI_DR_REGX,
  175. .readable_reg = stm32_sai_sub_readable_reg,
  176. .volatile_reg = stm32_sai_sub_volatile_reg,
  177. .writeable_reg = stm32_sai_sub_writeable_reg,
  178. .fast_io = true,
  179. };
  180. static const struct regmap_config stm32_sai_sub_regmap_config_h7 = {
  181. .reg_bits = 32,
  182. .reg_stride = 4,
  183. .val_bits = 32,
  184. .max_register = STM_SAI_PDMLY_REGX,
  185. .readable_reg = stm32_sai_sub_readable_reg,
  186. .volatile_reg = stm32_sai_sub_volatile_reg,
  187. .writeable_reg = stm32_sai_sub_writeable_reg,
  188. .fast_io = true,
  189. };
  190. static int snd_pcm_iec958_info(struct snd_kcontrol *kcontrol,
  191. struct snd_ctl_elem_info *uinfo)
  192. {
  193. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  194. uinfo->count = 1;
  195. return 0;
  196. }
  197. static int snd_pcm_iec958_get(struct snd_kcontrol *kcontrol,
  198. struct snd_ctl_elem_value *uctl)
  199. {
  200. struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
  201. mutex_lock(&sai->ctrl_lock);
  202. memcpy(uctl->value.iec958.status, sai->iec958.status, 4);
  203. mutex_unlock(&sai->ctrl_lock);
  204. return 0;
  205. }
  206. static int snd_pcm_iec958_put(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_value *uctl)
  208. {
  209. struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
  210. mutex_lock(&sai->ctrl_lock);
  211. memcpy(sai->iec958.status, uctl->value.iec958.status, 4);
  212. mutex_unlock(&sai->ctrl_lock);
  213. return 0;
  214. }
  215. static const struct snd_kcontrol_new iec958_ctls = {
  216. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  217. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  218. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  219. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  220. .info = snd_pcm_iec958_info,
  221. .get = snd_pcm_iec958_get,
  222. .put = snd_pcm_iec958_put,
  223. };
  224. static irqreturn_t stm32_sai_isr(int irq, void *devid)
  225. {
  226. struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid;
  227. struct platform_device *pdev = sai->pdev;
  228. unsigned int sr, imr, flags;
  229. snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING;
  230. regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr);
  231. regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr);
  232. flags = sr & imr;
  233. if (!flags)
  234. return IRQ_NONE;
  235. regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK,
  236. SAI_XCLRFR_MASK);
  237. if (!sai->substream) {
  238. dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr);
  239. return IRQ_NONE;
  240. }
  241. if (flags & SAI_XIMR_OVRUDRIE) {
  242. dev_err(&pdev->dev, "IRQ %s\n",
  243. STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun");
  244. status = SNDRV_PCM_STATE_XRUN;
  245. }
  246. if (flags & SAI_XIMR_MUTEDETIE)
  247. dev_dbg(&pdev->dev, "IRQ mute detected\n");
  248. if (flags & SAI_XIMR_WCKCFGIE) {
  249. dev_err(&pdev->dev, "IRQ wrong clock configuration\n");
  250. status = SNDRV_PCM_STATE_DISCONNECTED;
  251. }
  252. if (flags & SAI_XIMR_CNRDYIE)
  253. dev_err(&pdev->dev, "IRQ Codec not ready\n");
  254. if (flags & SAI_XIMR_AFSDETIE) {
  255. dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n");
  256. status = SNDRV_PCM_STATE_XRUN;
  257. }
  258. if (flags & SAI_XIMR_LFSDETIE) {
  259. dev_err(&pdev->dev, "IRQ Late frame synchro\n");
  260. status = SNDRV_PCM_STATE_XRUN;
  261. }
  262. if (status != SNDRV_PCM_STATE_RUNNING)
  263. snd_pcm_stop_xrun(sai->substream);
  264. return IRQ_HANDLED;
  265. }
  266. static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai,
  267. int clk_id, unsigned int freq, int dir)
  268. {
  269. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  270. int ret;
  271. if ((dir == SND_SOC_CLOCK_OUT) && sai->master) {
  272. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  273. SAI_XCR1_NODIV,
  274. (unsigned int)~SAI_XCR1_NODIV);
  275. if (ret < 0)
  276. return ret;
  277. sai->mclk_rate = freq;
  278. dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq);
  279. }
  280. return 0;
  281. }
  282. static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
  283. u32 rx_mask, int slots, int slot_width)
  284. {
  285. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  286. int slotr, slotr_mask, slot_size;
  287. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  288. dev_warn(cpu_dai->dev, "Slot setting relevant only for TDM\n");
  289. return 0;
  290. }
  291. dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n",
  292. tx_mask, rx_mask, slots, slot_width);
  293. switch (slot_width) {
  294. case 16:
  295. slot_size = SAI_SLOT_SIZE_16;
  296. break;
  297. case 32:
  298. slot_size = SAI_SLOT_SIZE_32;
  299. break;
  300. default:
  301. slot_size = SAI_SLOT_SIZE_AUTO;
  302. break;
  303. }
  304. slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) |
  305. SAI_XSLOTR_NBSLOT_SET(slots - 1);
  306. slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK;
  307. /* tx/rx mask set in machine init, if slot number defined in DT */
  308. if (STM_SAI_IS_PLAYBACK(sai)) {
  309. sai->slot_mask = tx_mask;
  310. slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask);
  311. }
  312. if (STM_SAI_IS_CAPTURE(sai)) {
  313. sai->slot_mask = rx_mask;
  314. slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask);
  315. }
  316. slotr_mask |= SAI_XSLOTR_SLOTEN_MASK;
  317. regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr);
  318. sai->slot_width = slot_width;
  319. sai->slots = slots;
  320. return 0;
  321. }
  322. static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  323. {
  324. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  325. int cr1, frcr = 0;
  326. int cr1_mask, frcr_mask = 0;
  327. int ret;
  328. dev_dbg(cpu_dai->dev, "fmt %x\n", fmt);
  329. /* Do not generate master by default */
  330. cr1 = SAI_XCR1_NODIV;
  331. cr1_mask = SAI_XCR1_NODIV;
  332. cr1_mask |= SAI_XCR1_PRTCFG_MASK;
  333. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  334. cr1 |= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL);
  335. goto conf_update;
  336. }
  337. cr1 |= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL);
  338. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  339. /* SCK active high for all protocols */
  340. case SND_SOC_DAIFMT_I2S:
  341. cr1 |= SAI_XCR1_CKSTR;
  342. frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF;
  343. break;
  344. /* Left justified */
  345. case SND_SOC_DAIFMT_MSB:
  346. frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
  347. break;
  348. /* Right justified */
  349. case SND_SOC_DAIFMT_LSB:
  350. frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
  351. break;
  352. case SND_SOC_DAIFMT_DSP_A:
  353. frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF;
  354. break;
  355. case SND_SOC_DAIFMT_DSP_B:
  356. frcr |= SAI_XFRCR_FSPOL;
  357. break;
  358. default:
  359. dev_err(cpu_dai->dev, "Unsupported protocol %#x\n",
  360. fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  361. return -EINVAL;
  362. }
  363. cr1_mask |= SAI_XCR1_CKSTR;
  364. frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF |
  365. SAI_XFRCR_FSDEF;
  366. /* DAI clock strobing. Invert setting previously set */
  367. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  368. case SND_SOC_DAIFMT_NB_NF:
  369. break;
  370. case SND_SOC_DAIFMT_IB_NF:
  371. cr1 ^= SAI_XCR1_CKSTR;
  372. break;
  373. case SND_SOC_DAIFMT_NB_IF:
  374. frcr ^= SAI_XFRCR_FSPOL;
  375. break;
  376. case SND_SOC_DAIFMT_IB_IF:
  377. /* Invert fs & sck */
  378. cr1 ^= SAI_XCR1_CKSTR;
  379. frcr ^= SAI_XFRCR_FSPOL;
  380. break;
  381. default:
  382. dev_err(cpu_dai->dev, "Unsupported strobing %#x\n",
  383. fmt & SND_SOC_DAIFMT_INV_MASK);
  384. return -EINVAL;
  385. }
  386. cr1_mask |= SAI_XCR1_CKSTR;
  387. frcr_mask |= SAI_XFRCR_FSPOL;
  388. regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
  389. /* DAI clock master masks */
  390. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  391. case SND_SOC_DAIFMT_CBM_CFM:
  392. /* codec is master */
  393. cr1 |= SAI_XCR1_SLAVE;
  394. sai->master = false;
  395. break;
  396. case SND_SOC_DAIFMT_CBS_CFS:
  397. sai->master = true;
  398. break;
  399. default:
  400. dev_err(cpu_dai->dev, "Unsupported mode %#x\n",
  401. fmt & SND_SOC_DAIFMT_MASTER_MASK);
  402. return -EINVAL;
  403. }
  404. /* Set slave mode if sub-block is synchronized with another SAI */
  405. if (sai->sync) {
  406. dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n");
  407. cr1 |= SAI_XCR1_SLAVE;
  408. sai->master = false;
  409. }
  410. cr1_mask |= SAI_XCR1_SLAVE;
  411. conf_update:
  412. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
  413. if (ret < 0) {
  414. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  415. return ret;
  416. }
  417. sai->fmt = fmt;
  418. return 0;
  419. }
  420. static int stm32_sai_startup(struct snd_pcm_substream *substream,
  421. struct snd_soc_dai *cpu_dai)
  422. {
  423. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  424. int imr, cr2, ret;
  425. sai->substream = substream;
  426. ret = clk_prepare_enable(sai->sai_ck);
  427. if (ret < 0) {
  428. dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret);
  429. return ret;
  430. }
  431. /* Enable ITs */
  432. regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX,
  433. SAI_XCLRFR_MASK, SAI_XCLRFR_MASK);
  434. imr = SAI_XIMR_OVRUDRIE;
  435. if (STM_SAI_IS_CAPTURE(sai)) {
  436. regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2);
  437. if (cr2 & SAI_XCR2_MUTECNT_MASK)
  438. imr |= SAI_XIMR_MUTEDETIE;
  439. }
  440. if (sai->master)
  441. imr |= SAI_XIMR_WCKCFGIE;
  442. else
  443. imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE;
  444. regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
  445. SAI_XIMR_MASK, imr);
  446. return 0;
  447. }
  448. static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai,
  449. struct snd_pcm_substream *substream,
  450. struct snd_pcm_hw_params *params)
  451. {
  452. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  453. int cr1, cr1_mask, ret;
  454. /*
  455. * DMA bursts increment is set to 4 words.
  456. * SAI fifo threshold is set to half fifo, to keep enough space
  457. * for DMA incoming bursts.
  458. */
  459. regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX,
  460. SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK,
  461. SAI_XCR2_FFLUSH |
  462. SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF));
  463. /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
  464. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  465. sai->spdif_frm_cnt = 0;
  466. return 0;
  467. }
  468. /* Mode, data format and channel config */
  469. cr1_mask = SAI_XCR1_DS_MASK;
  470. switch (params_format(params)) {
  471. case SNDRV_PCM_FORMAT_S8:
  472. cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8);
  473. break;
  474. case SNDRV_PCM_FORMAT_S16_LE:
  475. cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16);
  476. break;
  477. case SNDRV_PCM_FORMAT_S32_LE:
  478. cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32);
  479. break;
  480. default:
  481. dev_err(cpu_dai->dev, "Data format not supported");
  482. return -EINVAL;
  483. }
  484. cr1_mask |= SAI_XCR1_MONO;
  485. if ((sai->slots == 2) && (params_channels(params) == 1))
  486. cr1 |= SAI_XCR1_MONO;
  487. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
  488. if (ret < 0) {
  489. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai)
  495. {
  496. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  497. int slotr, slot_sz;
  498. regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr);
  499. /*
  500. * If SLOTSZ is set to auto in SLOTR, align slot width on data size
  501. * By default slot width = data size, if not forced from DT
  502. */
  503. slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK;
  504. if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO))
  505. sai->slot_width = sai->data_size;
  506. if (sai->slot_width < sai->data_size) {
  507. dev_err(cpu_dai->dev,
  508. "Data size %d larger than slot width\n",
  509. sai->data_size);
  510. return -EINVAL;
  511. }
  512. /* Slot number is set to 2, if not specified in DT */
  513. if (!sai->slots)
  514. sai->slots = 2;
  515. /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
  516. regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
  517. SAI_XSLOTR_NBSLOT_MASK,
  518. SAI_XSLOTR_NBSLOT_SET((sai->slots - 1)));
  519. /* Set default slots mask if not already set from DT */
  520. if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) {
  521. sai->slot_mask = (1 << sai->slots) - 1;
  522. regmap_update_bits(sai->regmap,
  523. STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK,
  524. SAI_XSLOTR_SLOTEN_SET(sai->slot_mask));
  525. }
  526. dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n",
  527. sai->slots, sai->slot_width);
  528. return 0;
  529. }
  530. static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai)
  531. {
  532. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  533. int fs_active, offset, format;
  534. int frcr, frcr_mask;
  535. format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  536. sai->fs_length = sai->slot_width * sai->slots;
  537. fs_active = sai->fs_length / 2;
  538. if ((format == SND_SOC_DAIFMT_DSP_A) ||
  539. (format == SND_SOC_DAIFMT_DSP_B))
  540. fs_active = 1;
  541. frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1));
  542. frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1));
  543. frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK;
  544. dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n",
  545. sai->fs_length, fs_active);
  546. regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
  547. if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) {
  548. offset = sai->slot_width - sai->data_size;
  549. regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
  550. SAI_XSLOTR_FBOFF_MASK,
  551. SAI_XSLOTR_FBOFF_SET(offset));
  552. }
  553. }
  554. static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai)
  555. {
  556. unsigned char *cs = sai->iec958.status;
  557. cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
  558. cs[1] = IEC958_AES1_CON_GENERAL;
  559. cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
  560. cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
  561. }
  562. static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,
  563. struct snd_pcm_runtime *runtime)
  564. {
  565. if (!runtime)
  566. return;
  567. /* Force the sample rate according to runtime rate */
  568. mutex_lock(&sai->ctrl_lock);
  569. switch (runtime->rate) {
  570. case 22050:
  571. sai->iec958.status[3] = IEC958_AES3_CON_FS_22050;
  572. break;
  573. case 44100:
  574. sai->iec958.status[3] = IEC958_AES3_CON_FS_44100;
  575. break;
  576. case 88200:
  577. sai->iec958.status[3] = IEC958_AES3_CON_FS_88200;
  578. break;
  579. case 176400:
  580. sai->iec958.status[3] = IEC958_AES3_CON_FS_176400;
  581. break;
  582. case 24000:
  583. sai->iec958.status[3] = IEC958_AES3_CON_FS_24000;
  584. break;
  585. case 48000:
  586. sai->iec958.status[3] = IEC958_AES3_CON_FS_48000;
  587. break;
  588. case 96000:
  589. sai->iec958.status[3] = IEC958_AES3_CON_FS_96000;
  590. break;
  591. case 192000:
  592. sai->iec958.status[3] = IEC958_AES3_CON_FS_192000;
  593. break;
  594. case 32000:
  595. sai->iec958.status[3] = IEC958_AES3_CON_FS_32000;
  596. break;
  597. default:
  598. sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID;
  599. break;
  600. }
  601. mutex_unlock(&sai->ctrl_lock);
  602. }
  603. static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
  604. struct snd_pcm_hw_params *params)
  605. {
  606. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  607. int cr1, mask, div = 0;
  608. int sai_clk_rate, mclk_ratio, den, ret;
  609. int version = sai->pdata->conf->version;
  610. unsigned int rate = params_rate(params);
  611. if (!sai->mclk_rate) {
  612. dev_err(cpu_dai->dev, "Mclk rate is null\n");
  613. return -EINVAL;
  614. }
  615. if (!(rate % 11025))
  616. clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k);
  617. else
  618. clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k);
  619. sai_clk_rate = clk_get_rate(sai->sai_ck);
  620. if (STM_SAI_IS_F4(sai->pdata)) {
  621. /*
  622. * mclk_rate = 256 * fs
  623. * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
  624. * MCKDIV = sai_ck / (2 * mclk_rate) otherwise
  625. */
  626. if (2 * sai_clk_rate >= 3 * sai->mclk_rate)
  627. div = DIV_ROUND_CLOSEST(sai_clk_rate,
  628. 2 * sai->mclk_rate);
  629. } else {
  630. /*
  631. * TDM mode :
  632. * mclk on
  633. * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0)
  634. * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1)
  635. * mclk off
  636. * MCKDIV = sai_ck / (frl x ws) (NOMCK=1)
  637. * Note: NOMCK/NODIV correspond to same bit.
  638. */
  639. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  640. div = DIV_ROUND_CLOSEST(sai_clk_rate,
  641. (params_rate(params) * 128));
  642. } else {
  643. if (sai->mclk_rate) {
  644. mclk_ratio = sai->mclk_rate / rate;
  645. if (mclk_ratio == 512) {
  646. mask = SAI_XCR1_OSR;
  647. cr1 = SAI_XCR1_OSR;
  648. } else if (mclk_ratio != 256) {
  649. dev_err(cpu_dai->dev,
  650. "Wrong mclk ratio %d\n",
  651. mclk_ratio);
  652. return -EINVAL;
  653. }
  654. div = DIV_ROUND_CLOSEST(sai_clk_rate,
  655. sai->mclk_rate);
  656. } else {
  657. /* mclk-fs not set, master clock not active */
  658. den = sai->fs_length * params_rate(params);
  659. div = DIV_ROUND_CLOSEST(sai_clk_rate, den);
  660. }
  661. }
  662. }
  663. if (div > SAI_XCR1_MCKDIV_MAX(version)) {
  664. dev_err(cpu_dai->dev, "Divider %d out of range\n", div);
  665. return -EINVAL;
  666. }
  667. dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div);
  668. mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version));
  669. cr1 = SAI_XCR1_MCKDIV_SET(div);
  670. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1);
  671. if (ret < 0) {
  672. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  673. return ret;
  674. }
  675. return 0;
  676. }
  677. static int stm32_sai_hw_params(struct snd_pcm_substream *substream,
  678. struct snd_pcm_hw_params *params,
  679. struct snd_soc_dai *cpu_dai)
  680. {
  681. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  682. int ret;
  683. sai->data_size = params_width(params);
  684. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  685. /* Rate not already set in runtime structure */
  686. substream->runtime->rate = params_rate(params);
  687. stm32_sai_set_iec958_status(sai, substream->runtime);
  688. } else {
  689. ret = stm32_sai_set_slots(cpu_dai);
  690. if (ret < 0)
  691. return ret;
  692. stm32_sai_set_frame(cpu_dai);
  693. }
  694. ret = stm32_sai_set_config(cpu_dai, substream, params);
  695. if (ret)
  696. return ret;
  697. if (sai->master)
  698. ret = stm32_sai_configure_clock(cpu_dai, params);
  699. return ret;
  700. }
  701. static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd,
  702. struct snd_soc_dai *cpu_dai)
  703. {
  704. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  705. int ret;
  706. switch (cmd) {
  707. case SNDRV_PCM_TRIGGER_START:
  708. case SNDRV_PCM_TRIGGER_RESUME:
  709. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  710. dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n");
  711. regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  712. SAI_XCR1_DMAEN, SAI_XCR1_DMAEN);
  713. /* Enable SAI */
  714. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  715. SAI_XCR1_SAIEN, SAI_XCR1_SAIEN);
  716. if (ret < 0)
  717. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  718. break;
  719. case SNDRV_PCM_TRIGGER_SUSPEND:
  720. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  721. case SNDRV_PCM_TRIGGER_STOP:
  722. dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n");
  723. regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
  724. SAI_XIMR_MASK, 0);
  725. regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  726. SAI_XCR1_SAIEN,
  727. (unsigned int)~SAI_XCR1_SAIEN);
  728. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  729. SAI_XCR1_DMAEN,
  730. (unsigned int)~SAI_XCR1_DMAEN);
  731. if (ret < 0)
  732. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  733. if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
  734. sai->spdif_frm_cnt = 0;
  735. break;
  736. default:
  737. return -EINVAL;
  738. }
  739. return ret;
  740. }
  741. static void stm32_sai_shutdown(struct snd_pcm_substream *substream,
  742. struct snd_soc_dai *cpu_dai)
  743. {
  744. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  745. regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0);
  746. regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV,
  747. SAI_XCR1_NODIV);
  748. clk_disable_unprepare(sai->sai_ck);
  749. sai->substream = NULL;
  750. }
  751. static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
  752. struct snd_soc_dai *cpu_dai)
  753. {
  754. struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
  755. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  756. dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__);
  757. return snd_ctl_add(rtd->pcm->card,
  758. snd_ctl_new1(&iec958_ctls, sai));
  759. }
  760. return 0;
  761. }
  762. static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai)
  763. {
  764. struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
  765. int cr1 = 0, cr1_mask;
  766. sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX);
  767. /*
  768. * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
  769. * as it allows bytes, half-word and words transfers. (See DMA fifos
  770. * constraints).
  771. */
  772. sai->dma_params.maxburst = 4;
  773. /* Buswidth will be set by framework at runtime */
  774. sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  775. if (STM_SAI_IS_PLAYBACK(sai))
  776. snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL);
  777. else
  778. snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params);
  779. /* Next settings are not relevant for spdif mode */
  780. if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
  781. return 0;
  782. cr1_mask = SAI_XCR1_RX_TX;
  783. if (STM_SAI_IS_CAPTURE(sai))
  784. cr1 |= SAI_XCR1_RX_TX;
  785. /* Configure synchronization */
  786. if (sai->sync == SAI_SYNC_EXTERNAL) {
  787. /* Configure synchro client and provider */
  788. sai->pdata->set_sync(sai->pdata, sai->np_sync_provider,
  789. sai->synco, sai->synci);
  790. }
  791. cr1_mask |= SAI_XCR1_SYNCEN_MASK;
  792. cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync);
  793. return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
  794. }
  795. static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = {
  796. .set_sysclk = stm32_sai_set_sysclk,
  797. .set_fmt = stm32_sai_set_dai_fmt,
  798. .set_tdm_slot = stm32_sai_set_dai_tdm_slot,
  799. .startup = stm32_sai_startup,
  800. .hw_params = stm32_sai_hw_params,
  801. .trigger = stm32_sai_trigger,
  802. .shutdown = stm32_sai_shutdown,
  803. };
  804. static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream,
  805. int channel, unsigned long hwoff,
  806. void *buf, unsigned long bytes)
  807. {
  808. struct snd_pcm_runtime *runtime = substream->runtime;
  809. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  810. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  811. struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
  812. int *ptr = (int *)(runtime->dma_area + hwoff +
  813. channel * (runtime->dma_bytes / runtime->channels));
  814. ssize_t cnt = bytes_to_samples(runtime, bytes);
  815. unsigned int frm_cnt = sai->spdif_frm_cnt;
  816. unsigned int byte;
  817. unsigned int mask;
  818. do {
  819. *ptr = ((*ptr >> 8) & 0x00ffffff);
  820. /* Set channel status bit */
  821. byte = frm_cnt >> 3;
  822. mask = 1 << (frm_cnt - (byte << 3));
  823. if (sai->iec958.status[byte] & mask)
  824. *ptr |= 0x04000000;
  825. ptr++;
  826. if (!(cnt % 2))
  827. frm_cnt++;
  828. if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES)
  829. frm_cnt = 0;
  830. } while (--cnt);
  831. sai->spdif_frm_cnt = frm_cnt;
  832. return 0;
  833. }
  834. static const struct snd_pcm_hardware stm32_sai_pcm_hw = {
  835. .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP,
  836. .buffer_bytes_max = 8 * PAGE_SIZE,
  837. .period_bytes_min = 1024, /* 5ms at 48kHz */
  838. .period_bytes_max = PAGE_SIZE,
  839. .periods_min = 2,
  840. .periods_max = 8,
  841. };
  842. static struct snd_soc_dai_driver stm32_sai_playback_dai[] = {
  843. {
  844. .probe = stm32_sai_dai_probe,
  845. .pcm_new = stm32_sai_pcm_new,
  846. .id = 1, /* avoid call to fmt_single_name() */
  847. .playback = {
  848. .channels_min = 1,
  849. .channels_max = 2,
  850. .rate_min = 8000,
  851. .rate_max = 192000,
  852. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  853. /* DMA does not support 24 bits transfers */
  854. .formats =
  855. SNDRV_PCM_FMTBIT_S8 |
  856. SNDRV_PCM_FMTBIT_S16_LE |
  857. SNDRV_PCM_FMTBIT_S32_LE,
  858. },
  859. .ops = &stm32_sai_pcm_dai_ops,
  860. }
  861. };
  862. static struct snd_soc_dai_driver stm32_sai_capture_dai[] = {
  863. {
  864. .probe = stm32_sai_dai_probe,
  865. .id = 1, /* avoid call to fmt_single_name() */
  866. .capture = {
  867. .channels_min = 1,
  868. .channels_max = 2,
  869. .rate_min = 8000,
  870. .rate_max = 192000,
  871. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  872. /* DMA does not support 24 bits transfers */
  873. .formats =
  874. SNDRV_PCM_FMTBIT_S8 |
  875. SNDRV_PCM_FMTBIT_S16_LE |
  876. SNDRV_PCM_FMTBIT_S32_LE,
  877. },
  878. .ops = &stm32_sai_pcm_dai_ops,
  879. }
  880. };
  881. static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = {
  882. .pcm_hardware = &stm32_sai_pcm_hw,
  883. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  884. };
  885. static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = {
  886. .pcm_hardware = &stm32_sai_pcm_hw,
  887. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  888. .process = stm32_sai_pcm_process_spdif,
  889. };
  890. static const struct snd_soc_component_driver stm32_component = {
  891. .name = "stm32-sai",
  892. };
  893. static const struct of_device_id stm32_sai_sub_ids[] = {
  894. { .compatible = "st,stm32-sai-sub-a",
  895. .data = (void *)STM_SAI_A_ID},
  896. { .compatible = "st,stm32-sai-sub-b",
  897. .data = (void *)STM_SAI_B_ID},
  898. {}
  899. };
  900. MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids);
  901. static int stm32_sai_sub_parse_of(struct platform_device *pdev,
  902. struct stm32_sai_sub_data *sai)
  903. {
  904. struct device_node *np = pdev->dev.of_node;
  905. struct resource *res;
  906. void __iomem *base;
  907. struct of_phandle_args args;
  908. int ret;
  909. if (!np)
  910. return -ENODEV;
  911. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  912. base = devm_ioremap_resource(&pdev->dev, res);
  913. if (IS_ERR(base))
  914. return PTR_ERR(base);
  915. sai->phys_addr = res->start;
  916. sai->regmap_config = &stm32_sai_sub_regmap_config_f4;
  917. /* Note: PDM registers not available for H7 sub-block B */
  918. if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai))
  919. sai->regmap_config = &stm32_sai_sub_regmap_config_h7;
  920. sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck",
  921. base, sai->regmap_config);
  922. if (IS_ERR(sai->regmap)) {
  923. dev_err(&pdev->dev, "Failed to initialize MMIO\n");
  924. return PTR_ERR(sai->regmap);
  925. }
  926. /* Get direction property */
  927. if (of_property_match_string(np, "dma-names", "tx") >= 0) {
  928. sai->dir = SNDRV_PCM_STREAM_PLAYBACK;
  929. } else if (of_property_match_string(np, "dma-names", "rx") >= 0) {
  930. sai->dir = SNDRV_PCM_STREAM_CAPTURE;
  931. } else {
  932. dev_err(&pdev->dev, "Unsupported direction\n");
  933. return -EINVAL;
  934. }
  935. /* Get spdif iec60958 property */
  936. sai->spdif = false;
  937. if (of_get_property(np, "st,iec60958", NULL)) {
  938. if (!STM_SAI_HAS_SPDIF(sai) ||
  939. sai->dir == SNDRV_PCM_STREAM_CAPTURE) {
  940. dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n");
  941. return -EINVAL;
  942. }
  943. stm32_sai_init_iec958_status(sai);
  944. sai->spdif = true;
  945. sai->master = true;
  946. }
  947. /* Get synchronization property */
  948. args.np = NULL;
  949. ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args);
  950. if (ret < 0 && ret != -ENOENT) {
  951. dev_err(&pdev->dev, "Failed to get st,sync property\n");
  952. return ret;
  953. }
  954. sai->sync = SAI_SYNC_NONE;
  955. if (args.np) {
  956. if (args.np == np) {
  957. dev_err(&pdev->dev, "%s sync own reference\n",
  958. np->name);
  959. of_node_put(args.np);
  960. return -EINVAL;
  961. }
  962. sai->np_sync_provider = of_get_parent(args.np);
  963. if (!sai->np_sync_provider) {
  964. dev_err(&pdev->dev, "%s parent node not found\n",
  965. np->name);
  966. of_node_put(args.np);
  967. return -ENODEV;
  968. }
  969. sai->sync = SAI_SYNC_INTERNAL;
  970. if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) {
  971. if (!STM_SAI_HAS_EXT_SYNC(sai)) {
  972. dev_err(&pdev->dev,
  973. "External synchro not supported\n");
  974. of_node_put(args.np);
  975. return -EINVAL;
  976. }
  977. sai->sync = SAI_SYNC_EXTERNAL;
  978. sai->synci = args.args[0];
  979. if (sai->synci < 1 ||
  980. (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) {
  981. dev_err(&pdev->dev, "Wrong SAI index\n");
  982. of_node_put(args.np);
  983. return -EINVAL;
  984. }
  985. if (of_property_match_string(args.np, "compatible",
  986. "st,stm32-sai-sub-a") >= 0)
  987. sai->synco = STM_SAI_SYNC_OUT_A;
  988. if (of_property_match_string(args.np, "compatible",
  989. "st,stm32-sai-sub-b") >= 0)
  990. sai->synco = STM_SAI_SYNC_OUT_B;
  991. if (!sai->synco) {
  992. dev_err(&pdev->dev, "Unknown SAI sub-block\n");
  993. of_node_put(args.np);
  994. return -EINVAL;
  995. }
  996. }
  997. dev_dbg(&pdev->dev, "%s synchronized with %s\n",
  998. pdev->name, args.np->full_name);
  999. }
  1000. of_node_put(args.np);
  1001. sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck");
  1002. if (IS_ERR(sai->sai_ck)) {
  1003. dev_err(&pdev->dev, "Missing kernel clock sai_ck\n");
  1004. return PTR_ERR(sai->sai_ck);
  1005. }
  1006. return 0;
  1007. }
  1008. static int stm32_sai_sub_dais_init(struct platform_device *pdev,
  1009. struct stm32_sai_sub_data *sai)
  1010. {
  1011. sai->cpu_dai_drv = devm_kzalloc(&pdev->dev,
  1012. sizeof(struct snd_soc_dai_driver),
  1013. GFP_KERNEL);
  1014. if (!sai->cpu_dai_drv)
  1015. return -ENOMEM;
  1016. sai->cpu_dai_drv->name = dev_name(&pdev->dev);
  1017. if (STM_SAI_IS_PLAYBACK(sai)) {
  1018. memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai,
  1019. sizeof(stm32_sai_playback_dai));
  1020. sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name;
  1021. } else {
  1022. memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai,
  1023. sizeof(stm32_sai_capture_dai));
  1024. sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name;
  1025. }
  1026. return 0;
  1027. }
  1028. static int stm32_sai_sub_probe(struct platform_device *pdev)
  1029. {
  1030. struct stm32_sai_sub_data *sai;
  1031. const struct of_device_id *of_id;
  1032. const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config;
  1033. int ret;
  1034. sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
  1035. if (!sai)
  1036. return -ENOMEM;
  1037. of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev);
  1038. if (!of_id)
  1039. return -EINVAL;
  1040. sai->id = (uintptr_t)of_id->data;
  1041. sai->pdev = pdev;
  1042. mutex_init(&sai->ctrl_lock);
  1043. platform_set_drvdata(pdev, sai);
  1044. sai->pdata = dev_get_drvdata(pdev->dev.parent);
  1045. if (!sai->pdata) {
  1046. dev_err(&pdev->dev, "Parent device data not available\n");
  1047. return -EINVAL;
  1048. }
  1049. ret = stm32_sai_sub_parse_of(pdev, sai);
  1050. if (ret)
  1051. return ret;
  1052. ret = stm32_sai_sub_dais_init(pdev, sai);
  1053. if (ret)
  1054. return ret;
  1055. ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr,
  1056. IRQF_SHARED, dev_name(&pdev->dev), sai);
  1057. if (ret) {
  1058. dev_err(&pdev->dev, "IRQ request returned %d\n", ret);
  1059. return ret;
  1060. }
  1061. ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component,
  1062. sai->cpu_dai_drv, 1);
  1063. if (ret)
  1064. return ret;
  1065. if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
  1066. conf = &stm32_sai_pcm_config_spdif;
  1067. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, conf, 0);
  1068. if (ret) {
  1069. dev_err(&pdev->dev, "Could not register pcm dma\n");
  1070. return ret;
  1071. }
  1072. return 0;
  1073. }
  1074. static struct platform_driver stm32_sai_sub_driver = {
  1075. .driver = {
  1076. .name = "st,stm32-sai-sub",
  1077. .of_match_table = stm32_sai_sub_ids,
  1078. },
  1079. .probe = stm32_sai_sub_probe,
  1080. };
  1081. module_platform_driver(stm32_sai_sub_driver);
  1082. MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
  1083. MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
  1084. MODULE_ALIAS("platform:st,stm32-sai-sub");
  1085. MODULE_LICENSE("GPL v2");