stm32_sai_sub.c 39 KB

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