mxs-saif.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright 2011 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/clk.h>
  26. #include <linux/clk-provider.h>
  27. #include <linux/delay.h>
  28. #include <linux/time.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include "mxs-saif.h"
  34. #define MXS_SET_ADDR 0x4
  35. #define MXS_CLR_ADDR 0x8
  36. static struct mxs_saif *mxs_saif[2];
  37. /*
  38. * SAIF is a little different with other normal SOC DAIs on clock using.
  39. *
  40. * For MXS, two SAIF modules are instantiated on-chip.
  41. * Each SAIF has a set of clock pins and can be operating in master
  42. * mode simultaneously if they are connected to different off-chip codecs.
  43. * Also, one of the two SAIFs can master or drive the clock pins while the
  44. * other SAIF, in slave mode, receives clocking from the master SAIF.
  45. * This also means that both SAIFs must operate at the same sample rate.
  46. *
  47. * We abstract this as each saif has a master, the master could be
  48. * itself or other saifs. In the generic saif driver, saif does not need
  49. * to know the different clkmux. Saif only needs to know who is its master
  50. * and operating its master to generate the proper clock rate for it.
  51. * The master id is provided in mach-specific layer according to different
  52. * clkmux setting.
  53. */
  54. static int mxs_saif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  55. int clk_id, unsigned int freq, int dir)
  56. {
  57. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  58. switch (clk_id) {
  59. case MXS_SAIF_MCLK:
  60. saif->mclk = freq;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * Since SAIF may work on EXTMASTER mode, IOW, it's working BITCLK&LRCLK
  69. * is provided by other SAIF, we provide a interface here to get its master
  70. * from its master_id.
  71. * Note that the master could be itself.
  72. */
  73. static inline struct mxs_saif *mxs_saif_get_master(struct mxs_saif * saif)
  74. {
  75. return mxs_saif[saif->master_id];
  76. }
  77. /*
  78. * Set SAIF clock and MCLK
  79. */
  80. static int mxs_saif_set_clk(struct mxs_saif *saif,
  81. unsigned int mclk,
  82. unsigned int rate)
  83. {
  84. u32 scr;
  85. int ret;
  86. struct mxs_saif *master_saif;
  87. dev_dbg(saif->dev, "mclk %d rate %d\n", mclk, rate);
  88. /* Set master saif to generate proper clock */
  89. master_saif = mxs_saif_get_master(saif);
  90. if (!master_saif)
  91. return -EINVAL;
  92. dev_dbg(saif->dev, "master saif%d\n", master_saif->id);
  93. /* Checking if can playback and capture simutaneously */
  94. if (master_saif->ongoing && rate != master_saif->cur_rate) {
  95. dev_err(saif->dev,
  96. "can not change clock, master saif%d(rate %d) is ongoing\n",
  97. master_saif->id, master_saif->cur_rate);
  98. return -EINVAL;
  99. }
  100. scr = __raw_readl(master_saif->base + SAIF_CTRL);
  101. scr &= ~BM_SAIF_CTRL_BITCLK_MULT_RATE;
  102. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  103. /*
  104. * Set SAIF clock
  105. *
  106. * The SAIF clock should be either 384*fs or 512*fs.
  107. * If MCLK is used, the SAIF clk ratio needs to match mclk ratio.
  108. * For 256x, 128x, 64x, and 32x sub-rates, set saif clk as 512*fs.
  109. * For 192x, 96x, and 48x sub-rates, set saif clk as 384*fs.
  110. *
  111. * If MCLK is not used, we just set saif clk to 512*fs.
  112. */
  113. ret = clk_prepare_enable(master_saif->clk);
  114. if (ret)
  115. return ret;
  116. if (master_saif->mclk_in_use) {
  117. switch (mclk / rate) {
  118. case 32:
  119. case 64:
  120. case 128:
  121. case 256:
  122. case 512:
  123. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  124. ret = clk_set_rate(master_saif->clk, 512 * rate);
  125. break;
  126. case 48:
  127. case 96:
  128. case 192:
  129. case 384:
  130. scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
  131. ret = clk_set_rate(master_saif->clk, 384 * rate);
  132. break;
  133. default:
  134. /* SAIF MCLK should be a sub-rate of 512x or 384x */
  135. clk_disable_unprepare(master_saif->clk);
  136. return -EINVAL;
  137. }
  138. } else {
  139. ret = clk_set_rate(master_saif->clk, 512 * rate);
  140. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  141. }
  142. clk_disable_unprepare(master_saif->clk);
  143. if (ret)
  144. return ret;
  145. master_saif->cur_rate = rate;
  146. if (!master_saif->mclk_in_use) {
  147. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  148. return 0;
  149. }
  150. /*
  151. * Program the over-sample rate for MCLK output
  152. *
  153. * The available MCLK range is 32x, 48x... 512x. The rate
  154. * could be from 8kHz to 192kH.
  155. */
  156. switch (mclk / rate) {
  157. case 32:
  158. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
  159. break;
  160. case 64:
  161. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  162. break;
  163. case 128:
  164. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  165. break;
  166. case 256:
  167. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  168. break;
  169. case 512:
  170. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  171. break;
  172. case 48:
  173. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  174. break;
  175. case 96:
  176. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  177. break;
  178. case 192:
  179. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  180. break;
  181. case 384:
  182. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  188. return 0;
  189. }
  190. /*
  191. * Put and disable MCLK.
  192. */
  193. int mxs_saif_put_mclk(unsigned int saif_id)
  194. {
  195. struct mxs_saif *saif = mxs_saif[saif_id];
  196. u32 stat;
  197. if (!saif)
  198. return -EINVAL;
  199. stat = __raw_readl(saif->base + SAIF_STAT);
  200. if (stat & BM_SAIF_STAT_BUSY) {
  201. dev_err(saif->dev, "error: busy\n");
  202. return -EBUSY;
  203. }
  204. clk_disable_unprepare(saif->clk);
  205. /* disable MCLK output */
  206. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  207. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  208. __raw_writel(BM_SAIF_CTRL_RUN,
  209. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  210. saif->mclk_in_use = 0;
  211. return 0;
  212. }
  213. EXPORT_SYMBOL_GPL(mxs_saif_put_mclk);
  214. /*
  215. * Get MCLK and set clock rate, then enable it
  216. *
  217. * This interface is used for codecs who are using MCLK provided
  218. * by saif.
  219. */
  220. int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
  221. unsigned int rate)
  222. {
  223. struct mxs_saif *saif = mxs_saif[saif_id];
  224. u32 stat;
  225. int ret;
  226. struct mxs_saif *master_saif;
  227. if (!saif)
  228. return -EINVAL;
  229. /* Clear Reset */
  230. __raw_writel(BM_SAIF_CTRL_SFTRST,
  231. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  232. /* FIXME: need clear clk gate for register r/w */
  233. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  234. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  235. master_saif = mxs_saif_get_master(saif);
  236. if (saif != master_saif) {
  237. dev_err(saif->dev, "can not get mclk from a non-master saif\n");
  238. return -EINVAL;
  239. }
  240. stat = __raw_readl(saif->base + SAIF_STAT);
  241. if (stat & BM_SAIF_STAT_BUSY) {
  242. dev_err(saif->dev, "error: busy\n");
  243. return -EBUSY;
  244. }
  245. saif->mclk_in_use = 1;
  246. ret = mxs_saif_set_clk(saif, mclk, rate);
  247. if (ret)
  248. return ret;
  249. ret = clk_prepare_enable(saif->clk);
  250. if (ret)
  251. return ret;
  252. /* enable MCLK output */
  253. __raw_writel(BM_SAIF_CTRL_RUN,
  254. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  255. return 0;
  256. }
  257. EXPORT_SYMBOL_GPL(mxs_saif_get_mclk);
  258. /*
  259. * SAIF DAI format configuration.
  260. * Should only be called when port is inactive.
  261. */
  262. static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  263. {
  264. u32 scr, stat;
  265. u32 scr0;
  266. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  267. stat = __raw_readl(saif->base + SAIF_STAT);
  268. if (stat & BM_SAIF_STAT_BUSY) {
  269. dev_err(cpu_dai->dev, "error: busy\n");
  270. return -EBUSY;
  271. }
  272. /* If SAIF1 is configured as slave, the clk gate needs to be cleared
  273. * before the register can be written.
  274. */
  275. if (saif->id != saif->master_id) {
  276. __raw_writel(BM_SAIF_CTRL_SFTRST,
  277. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  278. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  279. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  280. }
  281. scr0 = __raw_readl(saif->base + SAIF_CTRL);
  282. scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
  283. & ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
  284. scr = 0;
  285. /* DAI mode */
  286. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  287. case SND_SOC_DAIFMT_I2S:
  288. /* data frame low 1clk before data */
  289. scr |= BM_SAIF_CTRL_DELAY;
  290. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  291. break;
  292. case SND_SOC_DAIFMT_LEFT_J:
  293. /* data frame high with data */
  294. scr &= ~BM_SAIF_CTRL_DELAY;
  295. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  296. scr &= ~BM_SAIF_CTRL_JUSTIFY;
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. /* DAI clock inversion */
  302. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  303. case SND_SOC_DAIFMT_IB_IF:
  304. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  305. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  306. break;
  307. case SND_SOC_DAIFMT_IB_NF:
  308. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  309. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  310. break;
  311. case SND_SOC_DAIFMT_NB_IF:
  312. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  313. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  314. break;
  315. case SND_SOC_DAIFMT_NB_NF:
  316. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  317. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  318. break;
  319. }
  320. /*
  321. * Note: We simply just support master mode since SAIF TX can only
  322. * work as master.
  323. * Here the master is relative to codec side.
  324. * Saif internally could be slave when working on EXTMASTER mode.
  325. * We just hide this to machine driver.
  326. */
  327. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  328. case SND_SOC_DAIFMT_CBS_CFS:
  329. if (saif->id == saif->master_id)
  330. scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
  331. else
  332. scr |= BM_SAIF_CTRL_SLAVE_MODE;
  333. __raw_writel(scr | scr0, saif->base + SAIF_CTRL);
  334. break;
  335. default:
  336. return -EINVAL;
  337. }
  338. return 0;
  339. }
  340. static int mxs_saif_startup(struct snd_pcm_substream *substream,
  341. struct snd_soc_dai *cpu_dai)
  342. {
  343. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  344. int ret;
  345. /* clear error status to 0 for each re-open */
  346. saif->fifo_underrun = 0;
  347. saif->fifo_overrun = 0;
  348. /* Clear Reset for normal operations */
  349. __raw_writel(BM_SAIF_CTRL_SFTRST,
  350. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  351. /* clear clock gate */
  352. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  353. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  354. ret = clk_prepare(saif->clk);
  355. if (ret)
  356. return ret;
  357. return 0;
  358. }
  359. static void mxs_saif_shutdown(struct snd_pcm_substream *substream,
  360. struct snd_soc_dai *cpu_dai)
  361. {
  362. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  363. clk_unprepare(saif->clk);
  364. }
  365. /*
  366. * Should only be called when port is inactive.
  367. * although can be called multiple times by upper layers.
  368. */
  369. static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
  370. struct snd_pcm_hw_params *params,
  371. struct snd_soc_dai *cpu_dai)
  372. {
  373. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  374. struct mxs_saif *master_saif;
  375. u32 scr, stat;
  376. int ret;
  377. master_saif = mxs_saif_get_master(saif);
  378. if (!master_saif)
  379. return -EINVAL;
  380. /* mclk should already be set */
  381. if (!saif->mclk && saif->mclk_in_use) {
  382. dev_err(cpu_dai->dev, "set mclk first\n");
  383. return -EINVAL;
  384. }
  385. stat = __raw_readl(saif->base + SAIF_STAT);
  386. if (!saif->mclk_in_use && (stat & BM_SAIF_STAT_BUSY)) {
  387. dev_err(cpu_dai->dev, "error: busy\n");
  388. return -EBUSY;
  389. }
  390. /*
  391. * Set saif clk based on sample rate.
  392. * If mclk is used, we also set mclk, if not, saif->mclk is
  393. * default 0, means not used.
  394. */
  395. ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
  396. if (ret) {
  397. dev_err(cpu_dai->dev, "unable to get proper clk\n");
  398. return ret;
  399. }
  400. if (saif != master_saif) {
  401. /*
  402. * Set an initial clock rate for the saif internal logic to work
  403. * properly. This is important when working in EXTMASTER mode
  404. * that uses the other saif's BITCLK&LRCLK but it still needs a
  405. * basic clock which should be fast enough for the internal
  406. * logic.
  407. */
  408. clk_enable(saif->clk);
  409. ret = clk_set_rate(saif->clk, 24000000);
  410. clk_disable(saif->clk);
  411. if (ret)
  412. return ret;
  413. ret = clk_prepare(master_saif->clk);
  414. if (ret)
  415. return ret;
  416. }
  417. scr = __raw_readl(saif->base + SAIF_CTRL);
  418. scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
  419. scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  420. switch (params_format(params)) {
  421. case SNDRV_PCM_FORMAT_S16_LE:
  422. scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
  423. break;
  424. case SNDRV_PCM_FORMAT_S20_3LE:
  425. scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
  426. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  427. break;
  428. case SNDRV_PCM_FORMAT_S24_LE:
  429. scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
  430. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  431. break;
  432. default:
  433. return -EINVAL;
  434. }
  435. /* Tx/Rx config */
  436. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  437. /* enable TX mode */
  438. scr &= ~BM_SAIF_CTRL_READ_MODE;
  439. } else {
  440. /* enable RX mode */
  441. scr |= BM_SAIF_CTRL_READ_MODE;
  442. }
  443. __raw_writel(scr, saif->base + SAIF_CTRL);
  444. return 0;
  445. }
  446. static int mxs_saif_prepare(struct snd_pcm_substream *substream,
  447. struct snd_soc_dai *cpu_dai)
  448. {
  449. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  450. /* enable FIFO error irqs */
  451. __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
  452. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  453. return 0;
  454. }
  455. static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
  456. struct snd_soc_dai *cpu_dai)
  457. {
  458. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  459. struct mxs_saif *master_saif;
  460. u32 delay;
  461. int ret;
  462. master_saif = mxs_saif_get_master(saif);
  463. if (!master_saif)
  464. return -EINVAL;
  465. switch (cmd) {
  466. case SNDRV_PCM_TRIGGER_START:
  467. case SNDRV_PCM_TRIGGER_RESUME:
  468. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  469. if (saif->state == MXS_SAIF_STATE_RUNNING)
  470. return 0;
  471. dev_dbg(cpu_dai->dev, "start\n");
  472. ret = clk_enable(master_saif->clk);
  473. if (ret) {
  474. dev_err(saif->dev, "Failed to enable master clock\n");
  475. return ret;
  476. }
  477. /*
  478. * If the saif's master is not itself, we also need to enable
  479. * itself clk for its internal basic logic to work.
  480. */
  481. if (saif != master_saif) {
  482. ret = clk_enable(saif->clk);
  483. if (ret) {
  484. dev_err(saif->dev, "Failed to enable master clock\n");
  485. clk_disable(master_saif->clk);
  486. return ret;
  487. }
  488. __raw_writel(BM_SAIF_CTRL_RUN,
  489. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  490. }
  491. if (!master_saif->mclk_in_use)
  492. __raw_writel(BM_SAIF_CTRL_RUN,
  493. master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
  494. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  495. /*
  496. * write data to saif data register to trigger
  497. * the transfer.
  498. * For 24-bit format the 32-bit FIFO register stores
  499. * only one channel, so we need to write twice.
  500. * This is also safe for the other non 24-bit formats.
  501. */
  502. __raw_writel(0, saif->base + SAIF_DATA);
  503. __raw_writel(0, saif->base + SAIF_DATA);
  504. } else {
  505. /*
  506. * read data from saif data register to trigger
  507. * the receive.
  508. * For 24-bit format the 32-bit FIFO register stores
  509. * only one channel, so we need to read twice.
  510. * This is also safe for the other non 24-bit formats.
  511. */
  512. __raw_readl(saif->base + SAIF_DATA);
  513. __raw_readl(saif->base + SAIF_DATA);
  514. }
  515. master_saif->ongoing = 1;
  516. saif->state = MXS_SAIF_STATE_RUNNING;
  517. dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
  518. __raw_readl(saif->base + SAIF_CTRL),
  519. __raw_readl(saif->base + SAIF_STAT));
  520. dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
  521. __raw_readl(master_saif->base + SAIF_CTRL),
  522. __raw_readl(master_saif->base + SAIF_STAT));
  523. break;
  524. case SNDRV_PCM_TRIGGER_SUSPEND:
  525. case SNDRV_PCM_TRIGGER_STOP:
  526. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  527. if (saif->state == MXS_SAIF_STATE_STOPPED)
  528. return 0;
  529. dev_dbg(cpu_dai->dev, "stop\n");
  530. /* wait a while for the current sample to complete */
  531. delay = USEC_PER_SEC / master_saif->cur_rate;
  532. if (!master_saif->mclk_in_use) {
  533. __raw_writel(BM_SAIF_CTRL_RUN,
  534. master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  535. udelay(delay);
  536. }
  537. clk_disable(master_saif->clk);
  538. if (saif != master_saif) {
  539. __raw_writel(BM_SAIF_CTRL_RUN,
  540. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  541. udelay(delay);
  542. clk_disable(saif->clk);
  543. }
  544. master_saif->ongoing = 0;
  545. saif->state = MXS_SAIF_STATE_STOPPED;
  546. break;
  547. default:
  548. return -EINVAL;
  549. }
  550. return 0;
  551. }
  552. #define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
  553. #define MXS_SAIF_FORMATS \
  554. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  555. SNDRV_PCM_FMTBIT_S24_LE)
  556. static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
  557. .startup = mxs_saif_startup,
  558. .shutdown = mxs_saif_shutdown,
  559. .trigger = mxs_saif_trigger,
  560. .prepare = mxs_saif_prepare,
  561. .hw_params = mxs_saif_hw_params,
  562. .set_sysclk = mxs_saif_set_dai_sysclk,
  563. .set_fmt = mxs_saif_set_dai_fmt,
  564. };
  565. static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
  566. {
  567. struct mxs_saif *saif = dev_get_drvdata(dai->dev);
  568. snd_soc_dai_set_drvdata(dai, saif);
  569. return 0;
  570. }
  571. static struct snd_soc_dai_driver mxs_saif_dai = {
  572. .name = "mxs-saif",
  573. .probe = mxs_saif_dai_probe,
  574. .playback = {
  575. .channels_min = 2,
  576. .channels_max = 2,
  577. .rates = MXS_SAIF_RATES,
  578. .formats = MXS_SAIF_FORMATS,
  579. },
  580. .capture = {
  581. .channels_min = 2,
  582. .channels_max = 2,
  583. .rates = MXS_SAIF_RATES,
  584. .formats = MXS_SAIF_FORMATS,
  585. },
  586. .ops = &mxs_saif_dai_ops,
  587. };
  588. static const struct snd_soc_component_driver mxs_saif_component = {
  589. .name = "mxs-saif",
  590. };
  591. static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
  592. {
  593. struct mxs_saif *saif = dev_id;
  594. unsigned int stat;
  595. stat = __raw_readl(saif->base + SAIF_STAT);
  596. if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
  597. BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
  598. return IRQ_NONE;
  599. if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
  600. dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
  601. __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
  602. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  603. }
  604. if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
  605. dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
  606. __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
  607. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  608. }
  609. dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
  610. __raw_readl(saif->base + SAIF_CTRL),
  611. __raw_readl(saif->base + SAIF_STAT));
  612. return IRQ_HANDLED;
  613. }
  614. static int mxs_saif_mclk_init(struct platform_device *pdev)
  615. {
  616. struct mxs_saif *saif = platform_get_drvdata(pdev);
  617. struct device_node *np = pdev->dev.of_node;
  618. struct clk *clk;
  619. int ret;
  620. clk = clk_register_divider(&pdev->dev, "mxs_saif_mclk",
  621. __clk_get_name(saif->clk), 0,
  622. saif->base + SAIF_CTRL,
  623. BP_SAIF_CTRL_BITCLK_MULT_RATE, 3,
  624. 0, NULL);
  625. if (IS_ERR(clk)) {
  626. ret = PTR_ERR(clk);
  627. if (ret == -EEXIST)
  628. return 0;
  629. dev_err(&pdev->dev, "failed to register mclk: %d\n", ret);
  630. return PTR_ERR(clk);
  631. }
  632. ret = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  633. if (ret)
  634. return ret;
  635. return 0;
  636. }
  637. static int mxs_saif_probe(struct platform_device *pdev)
  638. {
  639. struct device_node *np = pdev->dev.of_node;
  640. struct resource *iores;
  641. struct mxs_saif *saif;
  642. int irq, ret = 0;
  643. struct device_node *master;
  644. if (!np)
  645. return -EINVAL;
  646. saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
  647. if (!saif)
  648. return -ENOMEM;
  649. ret = of_alias_get_id(np, "saif");
  650. if (ret < 0)
  651. return ret;
  652. else
  653. saif->id = ret;
  654. if (saif->id >= ARRAY_SIZE(mxs_saif)) {
  655. dev_err(&pdev->dev, "get wrong saif id\n");
  656. return -EINVAL;
  657. }
  658. /*
  659. * If there is no "fsl,saif-master" phandle, it's a saif
  660. * master. Otherwise, it's a slave and its phandle points
  661. * to the master.
  662. */
  663. master = of_parse_phandle(np, "fsl,saif-master", 0);
  664. if (!master) {
  665. saif->master_id = saif->id;
  666. } else {
  667. ret = of_alias_get_id(master, "saif");
  668. if (ret < 0)
  669. return ret;
  670. else
  671. saif->master_id = ret;
  672. if (saif->master_id >= ARRAY_SIZE(mxs_saif)) {
  673. dev_err(&pdev->dev, "get wrong master id\n");
  674. return -EINVAL;
  675. }
  676. }
  677. mxs_saif[saif->id] = saif;
  678. saif->clk = devm_clk_get(&pdev->dev, NULL);
  679. if (IS_ERR(saif->clk)) {
  680. ret = PTR_ERR(saif->clk);
  681. dev_err(&pdev->dev, "Cannot get the clock: %d\n",
  682. ret);
  683. return ret;
  684. }
  685. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  686. saif->base = devm_ioremap_resource(&pdev->dev, iores);
  687. if (IS_ERR(saif->base))
  688. return PTR_ERR(saif->base);
  689. irq = platform_get_irq(pdev, 0);
  690. if (irq < 0) {
  691. ret = irq;
  692. dev_err(&pdev->dev, "failed to get irq resource: %d\n",
  693. ret);
  694. return ret;
  695. }
  696. saif->dev = &pdev->dev;
  697. ret = devm_request_irq(&pdev->dev, irq, mxs_saif_irq, 0,
  698. dev_name(&pdev->dev), saif);
  699. if (ret) {
  700. dev_err(&pdev->dev, "failed to request irq\n");
  701. return ret;
  702. }
  703. platform_set_drvdata(pdev, saif);
  704. /* We only support saif0 being tx and clock master */
  705. if (saif->id == 0) {
  706. ret = mxs_saif_mclk_init(pdev);
  707. if (ret)
  708. dev_warn(&pdev->dev, "failed to init clocks\n");
  709. }
  710. ret = devm_snd_soc_register_component(&pdev->dev, &mxs_saif_component,
  711. &mxs_saif_dai, 1);
  712. if (ret) {
  713. dev_err(&pdev->dev, "register DAI failed\n");
  714. return ret;
  715. }
  716. ret = mxs_pcm_platform_register(&pdev->dev);
  717. if (ret) {
  718. dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
  719. return ret;
  720. }
  721. return 0;
  722. }
  723. static const struct of_device_id mxs_saif_dt_ids[] = {
  724. { .compatible = "fsl,imx28-saif", },
  725. { /* sentinel */ }
  726. };
  727. MODULE_DEVICE_TABLE(of, mxs_saif_dt_ids);
  728. static struct platform_driver mxs_saif_driver = {
  729. .probe = mxs_saif_probe,
  730. .driver = {
  731. .name = "mxs-saif",
  732. .of_match_table = mxs_saif_dt_ids,
  733. },
  734. };
  735. module_platform_driver(mxs_saif_driver);
  736. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  737. MODULE_DESCRIPTION("MXS ASoC SAIF driver");
  738. MODULE_LICENSE("GPL");
  739. MODULE_ALIAS("platform:mxs-saif");