stm32_sai_sub.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. * @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. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  427. snd_pcm_hw_constraint_mask64(substream->runtime,
  428. SNDRV_PCM_HW_PARAM_FORMAT,
  429. SNDRV_PCM_FMTBIT_S32_LE);
  430. snd_pcm_hw_constraint_single(substream->runtime,
  431. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  432. }
  433. ret = clk_prepare_enable(sai->sai_ck);
  434. if (ret < 0) {
  435. dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret);
  436. return ret;
  437. }
  438. /* Enable ITs */
  439. regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX,
  440. SAI_XCLRFR_MASK, SAI_XCLRFR_MASK);
  441. imr = SAI_XIMR_OVRUDRIE;
  442. if (STM_SAI_IS_CAPTURE(sai)) {
  443. regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2);
  444. if (cr2 & SAI_XCR2_MUTECNT_MASK)
  445. imr |= SAI_XIMR_MUTEDETIE;
  446. }
  447. if (sai->master)
  448. imr |= SAI_XIMR_WCKCFGIE;
  449. else
  450. imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE;
  451. regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
  452. SAI_XIMR_MASK, imr);
  453. return 0;
  454. }
  455. static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai,
  456. struct snd_pcm_substream *substream,
  457. struct snd_pcm_hw_params *params)
  458. {
  459. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  460. int cr1, cr1_mask, ret;
  461. /*
  462. * DMA bursts increment is set to 4 words.
  463. * SAI fifo threshold is set to half fifo, to keep enough space
  464. * for DMA incoming bursts.
  465. */
  466. regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX,
  467. SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK,
  468. SAI_XCR2_FFLUSH |
  469. SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF));
  470. /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
  471. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  472. sai->spdif_frm_cnt = 0;
  473. return 0;
  474. }
  475. /* Mode, data format and channel config */
  476. cr1_mask = SAI_XCR1_DS_MASK;
  477. switch (params_format(params)) {
  478. case SNDRV_PCM_FORMAT_S8:
  479. cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8);
  480. break;
  481. case SNDRV_PCM_FORMAT_S16_LE:
  482. cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16);
  483. break;
  484. case SNDRV_PCM_FORMAT_S32_LE:
  485. cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32);
  486. break;
  487. default:
  488. dev_err(cpu_dai->dev, "Data format not supported");
  489. return -EINVAL;
  490. }
  491. cr1_mask |= SAI_XCR1_MONO;
  492. if ((sai->slots == 2) && (params_channels(params) == 1))
  493. cr1 |= SAI_XCR1_MONO;
  494. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
  495. if (ret < 0) {
  496. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  497. return ret;
  498. }
  499. return 0;
  500. }
  501. static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai)
  502. {
  503. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  504. int slotr, slot_sz;
  505. regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr);
  506. /*
  507. * If SLOTSZ is set to auto in SLOTR, align slot width on data size
  508. * By default slot width = data size, if not forced from DT
  509. */
  510. slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK;
  511. if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO))
  512. sai->slot_width = sai->data_size;
  513. if (sai->slot_width < sai->data_size) {
  514. dev_err(cpu_dai->dev,
  515. "Data size %d larger than slot width\n",
  516. sai->data_size);
  517. return -EINVAL;
  518. }
  519. /* Slot number is set to 2, if not specified in DT */
  520. if (!sai->slots)
  521. sai->slots = 2;
  522. /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
  523. regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
  524. SAI_XSLOTR_NBSLOT_MASK,
  525. SAI_XSLOTR_NBSLOT_SET((sai->slots - 1)));
  526. /* Set default slots mask if not already set from DT */
  527. if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) {
  528. sai->slot_mask = (1 << sai->slots) - 1;
  529. regmap_update_bits(sai->regmap,
  530. STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK,
  531. SAI_XSLOTR_SLOTEN_SET(sai->slot_mask));
  532. }
  533. dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n",
  534. sai->slots, sai->slot_width);
  535. return 0;
  536. }
  537. static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai)
  538. {
  539. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  540. int fs_active, offset, format;
  541. int frcr, frcr_mask;
  542. format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  543. sai->fs_length = sai->slot_width * sai->slots;
  544. fs_active = sai->fs_length / 2;
  545. if ((format == SND_SOC_DAIFMT_DSP_A) ||
  546. (format == SND_SOC_DAIFMT_DSP_B))
  547. fs_active = 1;
  548. frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1));
  549. frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1));
  550. frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK;
  551. dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n",
  552. sai->fs_length, fs_active);
  553. regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
  554. if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) {
  555. offset = sai->slot_width - sai->data_size;
  556. regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
  557. SAI_XSLOTR_FBOFF_MASK,
  558. SAI_XSLOTR_FBOFF_SET(offset));
  559. }
  560. }
  561. static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai)
  562. {
  563. unsigned char *cs = sai->iec958.status;
  564. cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
  565. cs[1] = IEC958_AES1_CON_GENERAL;
  566. cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
  567. cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
  568. }
  569. static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,
  570. struct snd_pcm_runtime *runtime)
  571. {
  572. if (!runtime)
  573. return;
  574. /* Force the sample rate according to runtime rate */
  575. mutex_lock(&sai->ctrl_lock);
  576. switch (runtime->rate) {
  577. case 22050:
  578. sai->iec958.status[3] = IEC958_AES3_CON_FS_22050;
  579. break;
  580. case 44100:
  581. sai->iec958.status[3] = IEC958_AES3_CON_FS_44100;
  582. break;
  583. case 88200:
  584. sai->iec958.status[3] = IEC958_AES3_CON_FS_88200;
  585. break;
  586. case 176400:
  587. sai->iec958.status[3] = IEC958_AES3_CON_FS_176400;
  588. break;
  589. case 24000:
  590. sai->iec958.status[3] = IEC958_AES3_CON_FS_24000;
  591. break;
  592. case 48000:
  593. sai->iec958.status[3] = IEC958_AES3_CON_FS_48000;
  594. break;
  595. case 96000:
  596. sai->iec958.status[3] = IEC958_AES3_CON_FS_96000;
  597. break;
  598. case 192000:
  599. sai->iec958.status[3] = IEC958_AES3_CON_FS_192000;
  600. break;
  601. case 32000:
  602. sai->iec958.status[3] = IEC958_AES3_CON_FS_32000;
  603. break;
  604. default:
  605. sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID;
  606. break;
  607. }
  608. mutex_unlock(&sai->ctrl_lock);
  609. }
  610. static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
  611. struct snd_pcm_hw_params *params)
  612. {
  613. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  614. int cr1, mask, div = 0;
  615. int sai_clk_rate, mclk_ratio, den, ret;
  616. int version = sai->pdata->conf->version;
  617. unsigned int rate = params_rate(params);
  618. if (!sai->mclk_rate) {
  619. dev_err(cpu_dai->dev, "Mclk rate is null\n");
  620. return -EINVAL;
  621. }
  622. if (!(rate % 11025))
  623. clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k);
  624. else
  625. clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k);
  626. sai_clk_rate = clk_get_rate(sai->sai_ck);
  627. if (STM_SAI_IS_F4(sai->pdata)) {
  628. /*
  629. * mclk_rate = 256 * fs
  630. * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
  631. * MCKDIV = sai_ck / (2 * mclk_rate) otherwise
  632. */
  633. if (2 * sai_clk_rate >= 3 * sai->mclk_rate)
  634. div = DIV_ROUND_CLOSEST(sai_clk_rate,
  635. 2 * sai->mclk_rate);
  636. } else {
  637. /*
  638. * TDM mode :
  639. * mclk on
  640. * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0)
  641. * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1)
  642. * mclk off
  643. * MCKDIV = sai_ck / (frl x ws) (NOMCK=1)
  644. * Note: NOMCK/NODIV correspond to same bit.
  645. */
  646. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  647. div = DIV_ROUND_CLOSEST(sai_clk_rate,
  648. (params_rate(params) * 128));
  649. } else {
  650. if (sai->mclk_rate) {
  651. mclk_ratio = sai->mclk_rate / rate;
  652. if (mclk_ratio == 512) {
  653. mask = SAI_XCR1_OSR;
  654. cr1 = SAI_XCR1_OSR;
  655. } else if (mclk_ratio != 256) {
  656. dev_err(cpu_dai->dev,
  657. "Wrong mclk ratio %d\n",
  658. mclk_ratio);
  659. return -EINVAL;
  660. }
  661. div = DIV_ROUND_CLOSEST(sai_clk_rate,
  662. sai->mclk_rate);
  663. } else {
  664. /* mclk-fs not set, master clock not active */
  665. den = sai->fs_length * params_rate(params);
  666. div = DIV_ROUND_CLOSEST(sai_clk_rate, den);
  667. }
  668. }
  669. }
  670. if (div > SAI_XCR1_MCKDIV_MAX(version)) {
  671. dev_err(cpu_dai->dev, "Divider %d out of range\n", div);
  672. return -EINVAL;
  673. }
  674. dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div);
  675. mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version));
  676. cr1 = SAI_XCR1_MCKDIV_SET(div);
  677. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1);
  678. if (ret < 0) {
  679. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  680. return ret;
  681. }
  682. return 0;
  683. }
  684. static int stm32_sai_hw_params(struct snd_pcm_substream *substream,
  685. struct snd_pcm_hw_params *params,
  686. struct snd_soc_dai *cpu_dai)
  687. {
  688. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  689. int ret;
  690. sai->data_size = params_width(params);
  691. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  692. /* Rate not already set in runtime structure */
  693. substream->runtime->rate = params_rate(params);
  694. stm32_sai_set_iec958_status(sai, substream->runtime);
  695. } else {
  696. ret = stm32_sai_set_slots(cpu_dai);
  697. if (ret < 0)
  698. return ret;
  699. stm32_sai_set_frame(cpu_dai);
  700. }
  701. ret = stm32_sai_set_config(cpu_dai, substream, params);
  702. if (ret)
  703. return ret;
  704. if (sai->master)
  705. ret = stm32_sai_configure_clock(cpu_dai, params);
  706. return ret;
  707. }
  708. static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd,
  709. struct snd_soc_dai *cpu_dai)
  710. {
  711. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  712. int ret;
  713. switch (cmd) {
  714. case SNDRV_PCM_TRIGGER_START:
  715. case SNDRV_PCM_TRIGGER_RESUME:
  716. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  717. dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n");
  718. regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  719. SAI_XCR1_DMAEN, SAI_XCR1_DMAEN);
  720. /* Enable SAI */
  721. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  722. SAI_XCR1_SAIEN, SAI_XCR1_SAIEN);
  723. if (ret < 0)
  724. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  725. break;
  726. case SNDRV_PCM_TRIGGER_SUSPEND:
  727. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  728. case SNDRV_PCM_TRIGGER_STOP:
  729. dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n");
  730. regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
  731. SAI_XIMR_MASK, 0);
  732. regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  733. SAI_XCR1_SAIEN,
  734. (unsigned int)~SAI_XCR1_SAIEN);
  735. ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
  736. SAI_XCR1_DMAEN,
  737. (unsigned int)~SAI_XCR1_DMAEN);
  738. if (ret < 0)
  739. dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
  740. if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
  741. sai->spdif_frm_cnt = 0;
  742. break;
  743. default:
  744. return -EINVAL;
  745. }
  746. return ret;
  747. }
  748. static void stm32_sai_shutdown(struct snd_pcm_substream *substream,
  749. struct snd_soc_dai *cpu_dai)
  750. {
  751. struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
  752. regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0);
  753. regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV,
  754. SAI_XCR1_NODIV);
  755. clk_disable_unprepare(sai->sai_ck);
  756. sai->substream = NULL;
  757. }
  758. static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
  759. struct snd_soc_dai *cpu_dai)
  760. {
  761. struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
  762. struct snd_kcontrol_new knew = iec958_ctls;
  763. if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
  764. dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__);
  765. knew.device = rtd->pcm->device;
  766. return snd_ctl_add(rtd->pcm->card, snd_ctl_new1(&knew, sai));
  767. }
  768. return 0;
  769. }
  770. static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai)
  771. {
  772. struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
  773. int cr1 = 0, cr1_mask;
  774. sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX);
  775. /*
  776. * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
  777. * as it allows bytes, half-word and words transfers. (See DMA fifos
  778. * constraints).
  779. */
  780. sai->dma_params.maxburst = 4;
  781. /* Buswidth will be set by framework at runtime */
  782. sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  783. if (STM_SAI_IS_PLAYBACK(sai))
  784. snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL);
  785. else
  786. snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params);
  787. /* Next settings are not relevant for spdif mode */
  788. if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
  789. return 0;
  790. cr1_mask = SAI_XCR1_RX_TX;
  791. if (STM_SAI_IS_CAPTURE(sai))
  792. cr1 |= SAI_XCR1_RX_TX;
  793. /* Configure synchronization */
  794. if (sai->sync == SAI_SYNC_EXTERNAL) {
  795. /* Configure synchro client and provider */
  796. sai->pdata->set_sync(sai->pdata, sai->np_sync_provider,
  797. sai->synco, sai->synci);
  798. }
  799. cr1_mask |= SAI_XCR1_SYNCEN_MASK;
  800. cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync);
  801. return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
  802. }
  803. static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = {
  804. .set_sysclk = stm32_sai_set_sysclk,
  805. .set_fmt = stm32_sai_set_dai_fmt,
  806. .set_tdm_slot = stm32_sai_set_dai_tdm_slot,
  807. .startup = stm32_sai_startup,
  808. .hw_params = stm32_sai_hw_params,
  809. .trigger = stm32_sai_trigger,
  810. .shutdown = stm32_sai_shutdown,
  811. };
  812. static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream,
  813. int channel, unsigned long hwoff,
  814. void *buf, unsigned long bytes)
  815. {
  816. struct snd_pcm_runtime *runtime = substream->runtime;
  817. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  818. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  819. struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
  820. int *ptr = (int *)(runtime->dma_area + hwoff +
  821. channel * (runtime->dma_bytes / runtime->channels));
  822. ssize_t cnt = bytes_to_samples(runtime, bytes);
  823. unsigned int frm_cnt = sai->spdif_frm_cnt;
  824. unsigned int byte;
  825. unsigned int mask;
  826. do {
  827. *ptr = ((*ptr >> 8) & 0x00ffffff);
  828. /* Set channel status bit */
  829. byte = frm_cnt >> 3;
  830. mask = 1 << (frm_cnt - (byte << 3));
  831. if (sai->iec958.status[byte] & mask)
  832. *ptr |= 0x04000000;
  833. ptr++;
  834. if (!(cnt % 2))
  835. frm_cnt++;
  836. if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES)
  837. frm_cnt = 0;
  838. } while (--cnt);
  839. sai->spdif_frm_cnt = frm_cnt;
  840. return 0;
  841. }
  842. /* No support of mmap in S/PDIF mode */
  843. static const struct snd_pcm_hardware stm32_sai_pcm_hw_spdif = {
  844. .info = SNDRV_PCM_INFO_INTERLEAVED,
  845. .buffer_bytes_max = 8 * PAGE_SIZE,
  846. .period_bytes_min = 1024,
  847. .period_bytes_max = PAGE_SIZE,
  848. .periods_min = 2,
  849. .periods_max = 8,
  850. };
  851. static const struct snd_pcm_hardware stm32_sai_pcm_hw = {
  852. .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP,
  853. .buffer_bytes_max = 8 * PAGE_SIZE,
  854. .period_bytes_min = 1024, /* 5ms at 48kHz */
  855. .period_bytes_max = PAGE_SIZE,
  856. .periods_min = 2,
  857. .periods_max = 8,
  858. };
  859. static struct snd_soc_dai_driver stm32_sai_playback_dai[] = {
  860. {
  861. .probe = stm32_sai_dai_probe,
  862. .pcm_new = stm32_sai_pcm_new,
  863. .id = 1, /* avoid call to fmt_single_name() */
  864. .playback = {
  865. .channels_min = 1,
  866. .channels_max = 2,
  867. .rate_min = 8000,
  868. .rate_max = 192000,
  869. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  870. /* DMA does not support 24 bits transfers */
  871. .formats =
  872. SNDRV_PCM_FMTBIT_S8 |
  873. SNDRV_PCM_FMTBIT_S16_LE |
  874. SNDRV_PCM_FMTBIT_S32_LE,
  875. },
  876. .ops = &stm32_sai_pcm_dai_ops,
  877. }
  878. };
  879. static struct snd_soc_dai_driver stm32_sai_capture_dai[] = {
  880. {
  881. .probe = stm32_sai_dai_probe,
  882. .id = 1, /* avoid call to fmt_single_name() */
  883. .capture = {
  884. .channels_min = 1,
  885. .channels_max = 2,
  886. .rate_min = 8000,
  887. .rate_max = 192000,
  888. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  889. /* DMA does not support 24 bits transfers */
  890. .formats =
  891. SNDRV_PCM_FMTBIT_S8 |
  892. SNDRV_PCM_FMTBIT_S16_LE |
  893. SNDRV_PCM_FMTBIT_S32_LE,
  894. },
  895. .ops = &stm32_sai_pcm_dai_ops,
  896. }
  897. };
  898. static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = {
  899. .pcm_hardware = &stm32_sai_pcm_hw,
  900. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  901. };
  902. static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = {
  903. .pcm_hardware = &stm32_sai_pcm_hw_spdif,
  904. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  905. .process = stm32_sai_pcm_process_spdif,
  906. };
  907. static const struct snd_soc_component_driver stm32_component = {
  908. .name = "stm32-sai",
  909. };
  910. static const struct of_device_id stm32_sai_sub_ids[] = {
  911. { .compatible = "st,stm32-sai-sub-a",
  912. .data = (void *)STM_SAI_A_ID},
  913. { .compatible = "st,stm32-sai-sub-b",
  914. .data = (void *)STM_SAI_B_ID},
  915. {}
  916. };
  917. MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids);
  918. static int stm32_sai_sub_parse_of(struct platform_device *pdev,
  919. struct stm32_sai_sub_data *sai)
  920. {
  921. struct device_node *np = pdev->dev.of_node;
  922. struct resource *res;
  923. void __iomem *base;
  924. struct of_phandle_args args;
  925. int ret;
  926. if (!np)
  927. return -ENODEV;
  928. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  929. base = devm_ioremap_resource(&pdev->dev, res);
  930. if (IS_ERR(base))
  931. return PTR_ERR(base);
  932. sai->phys_addr = res->start;
  933. sai->regmap_config = &stm32_sai_sub_regmap_config_f4;
  934. /* Note: PDM registers not available for H7 sub-block B */
  935. if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai))
  936. sai->regmap_config = &stm32_sai_sub_regmap_config_h7;
  937. sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck",
  938. base, sai->regmap_config);
  939. if (IS_ERR(sai->regmap)) {
  940. dev_err(&pdev->dev, "Failed to initialize MMIO\n");
  941. return PTR_ERR(sai->regmap);
  942. }
  943. /* Get direction property */
  944. if (of_property_match_string(np, "dma-names", "tx") >= 0) {
  945. sai->dir = SNDRV_PCM_STREAM_PLAYBACK;
  946. } else if (of_property_match_string(np, "dma-names", "rx") >= 0) {
  947. sai->dir = SNDRV_PCM_STREAM_CAPTURE;
  948. } else {
  949. dev_err(&pdev->dev, "Unsupported direction\n");
  950. return -EINVAL;
  951. }
  952. /* Get spdif iec60958 property */
  953. sai->spdif = false;
  954. if (of_get_property(np, "st,iec60958", NULL)) {
  955. if (!STM_SAI_HAS_SPDIF(sai) ||
  956. sai->dir == SNDRV_PCM_STREAM_CAPTURE) {
  957. dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n");
  958. return -EINVAL;
  959. }
  960. stm32_sai_init_iec958_status(sai);
  961. sai->spdif = true;
  962. sai->master = true;
  963. }
  964. /* Get synchronization property */
  965. args.np = NULL;
  966. ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args);
  967. if (ret < 0 && ret != -ENOENT) {
  968. dev_err(&pdev->dev, "Failed to get st,sync property\n");
  969. return ret;
  970. }
  971. sai->sync = SAI_SYNC_NONE;
  972. if (args.np) {
  973. if (args.np == np) {
  974. dev_err(&pdev->dev, "%s sync own reference\n",
  975. np->name);
  976. of_node_put(args.np);
  977. return -EINVAL;
  978. }
  979. sai->np_sync_provider = of_get_parent(args.np);
  980. if (!sai->np_sync_provider) {
  981. dev_err(&pdev->dev, "%s parent node not found\n",
  982. np->name);
  983. of_node_put(args.np);
  984. return -ENODEV;
  985. }
  986. sai->sync = SAI_SYNC_INTERNAL;
  987. if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) {
  988. if (!STM_SAI_HAS_EXT_SYNC(sai)) {
  989. dev_err(&pdev->dev,
  990. "External synchro not supported\n");
  991. of_node_put(args.np);
  992. return -EINVAL;
  993. }
  994. sai->sync = SAI_SYNC_EXTERNAL;
  995. sai->synci = args.args[0];
  996. if (sai->synci < 1 ||
  997. (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) {
  998. dev_err(&pdev->dev, "Wrong SAI index\n");
  999. of_node_put(args.np);
  1000. return -EINVAL;
  1001. }
  1002. if (of_property_match_string(args.np, "compatible",
  1003. "st,stm32-sai-sub-a") >= 0)
  1004. sai->synco = STM_SAI_SYNC_OUT_A;
  1005. if (of_property_match_string(args.np, "compatible",
  1006. "st,stm32-sai-sub-b") >= 0)
  1007. sai->synco = STM_SAI_SYNC_OUT_B;
  1008. if (!sai->synco) {
  1009. dev_err(&pdev->dev, "Unknown SAI sub-block\n");
  1010. of_node_put(args.np);
  1011. return -EINVAL;
  1012. }
  1013. }
  1014. dev_dbg(&pdev->dev, "%s synchronized with %s\n",
  1015. pdev->name, args.np->full_name);
  1016. }
  1017. of_node_put(args.np);
  1018. sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck");
  1019. if (IS_ERR(sai->sai_ck)) {
  1020. dev_err(&pdev->dev, "Missing kernel clock sai_ck\n");
  1021. return PTR_ERR(sai->sai_ck);
  1022. }
  1023. return 0;
  1024. }
  1025. static int stm32_sai_sub_dais_init(struct platform_device *pdev,
  1026. struct stm32_sai_sub_data *sai)
  1027. {
  1028. sai->cpu_dai_drv = devm_kzalloc(&pdev->dev,
  1029. sizeof(struct snd_soc_dai_driver),
  1030. GFP_KERNEL);
  1031. if (!sai->cpu_dai_drv)
  1032. return -ENOMEM;
  1033. if (STM_SAI_IS_PLAYBACK(sai)) {
  1034. memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai,
  1035. sizeof(stm32_sai_playback_dai));
  1036. sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name;
  1037. } else {
  1038. memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai,
  1039. sizeof(stm32_sai_capture_dai));
  1040. sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name;
  1041. }
  1042. sai->cpu_dai_drv->name = dev_name(&pdev->dev);
  1043. return 0;
  1044. }
  1045. static int stm32_sai_sub_probe(struct platform_device *pdev)
  1046. {
  1047. struct stm32_sai_sub_data *sai;
  1048. const struct of_device_id *of_id;
  1049. const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config;
  1050. int ret;
  1051. sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
  1052. if (!sai)
  1053. return -ENOMEM;
  1054. of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev);
  1055. if (!of_id)
  1056. return -EINVAL;
  1057. sai->id = (uintptr_t)of_id->data;
  1058. sai->pdev = pdev;
  1059. mutex_init(&sai->ctrl_lock);
  1060. platform_set_drvdata(pdev, sai);
  1061. sai->pdata = dev_get_drvdata(pdev->dev.parent);
  1062. if (!sai->pdata) {
  1063. dev_err(&pdev->dev, "Parent device data not available\n");
  1064. return -EINVAL;
  1065. }
  1066. ret = stm32_sai_sub_parse_of(pdev, sai);
  1067. if (ret)
  1068. return ret;
  1069. ret = stm32_sai_sub_dais_init(pdev, sai);
  1070. if (ret)
  1071. return ret;
  1072. ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr,
  1073. IRQF_SHARED, dev_name(&pdev->dev), sai);
  1074. if (ret) {
  1075. dev_err(&pdev->dev, "IRQ request returned %d\n", ret);
  1076. return ret;
  1077. }
  1078. ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component,
  1079. sai->cpu_dai_drv, 1);
  1080. if (ret)
  1081. return ret;
  1082. if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
  1083. conf = &stm32_sai_pcm_config_spdif;
  1084. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, conf, 0);
  1085. if (ret) {
  1086. dev_err(&pdev->dev, "Could not register pcm dma\n");
  1087. return ret;
  1088. }
  1089. return 0;
  1090. }
  1091. static struct platform_driver stm32_sai_sub_driver = {
  1092. .driver = {
  1093. .name = "st,stm32-sai-sub",
  1094. .of_match_table = stm32_sai_sub_ids,
  1095. },
  1096. .probe = stm32_sai_sub_probe,
  1097. };
  1098. module_platform_driver(stm32_sai_sub_driver);
  1099. MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
  1100. MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
  1101. MODULE_ALIAS("platform:st,stm32-sai-sub");
  1102. MODULE_LICENSE("GPL v2");