mxs-saif.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 need to match mclk ratio.
  108. * For 32x mclk, set saif clk as 512*fs.
  109. * For 48x mclk, set saif clk as 384*fs.
  110. *
  111. * If MCLK is not used, we just set saif clk to 512*fs.
  112. */
  113. clk_prepare_enable(master_saif->clk);
  114. if (master_saif->mclk_in_use) {
  115. if (mclk % 32 == 0) {
  116. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  117. ret = clk_set_rate(master_saif->clk, 512 * rate);
  118. } else if (mclk % 48 == 0) {
  119. scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
  120. ret = clk_set_rate(master_saif->clk, 384 * rate);
  121. } else {
  122. /* SAIF MCLK should be either 32x or 48x */
  123. clk_disable_unprepare(master_saif->clk);
  124. return -EINVAL;
  125. }
  126. } else {
  127. ret = clk_set_rate(master_saif->clk, 512 * rate);
  128. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  129. }
  130. clk_disable_unprepare(master_saif->clk);
  131. if (ret)
  132. return ret;
  133. master_saif->cur_rate = rate;
  134. if (!master_saif->mclk_in_use) {
  135. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  136. return 0;
  137. }
  138. /*
  139. * Program the over-sample rate for MCLK output
  140. *
  141. * The available MCLK range is 32x, 48x... 512x. The rate
  142. * could be from 8kHz to 192kH.
  143. */
  144. switch (mclk / rate) {
  145. case 32:
  146. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
  147. break;
  148. case 64:
  149. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  150. break;
  151. case 128:
  152. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  153. break;
  154. case 256:
  155. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  156. break;
  157. case 512:
  158. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  159. break;
  160. case 48:
  161. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  162. break;
  163. case 96:
  164. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  165. break;
  166. case 192:
  167. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  168. break;
  169. case 384:
  170. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  171. break;
  172. default:
  173. return -EINVAL;
  174. }
  175. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  176. return 0;
  177. }
  178. /*
  179. * Put and disable MCLK.
  180. */
  181. int mxs_saif_put_mclk(unsigned int saif_id)
  182. {
  183. struct mxs_saif *saif = mxs_saif[saif_id];
  184. u32 stat;
  185. if (!saif)
  186. return -EINVAL;
  187. stat = __raw_readl(saif->base + SAIF_STAT);
  188. if (stat & BM_SAIF_STAT_BUSY) {
  189. dev_err(saif->dev, "error: busy\n");
  190. return -EBUSY;
  191. }
  192. clk_disable_unprepare(saif->clk);
  193. /* disable MCLK output */
  194. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  195. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  196. __raw_writel(BM_SAIF_CTRL_RUN,
  197. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  198. saif->mclk_in_use = 0;
  199. return 0;
  200. }
  201. EXPORT_SYMBOL_GPL(mxs_saif_put_mclk);
  202. /*
  203. * Get MCLK and set clock rate, then enable it
  204. *
  205. * This interface is used for codecs who are using MCLK provided
  206. * by saif.
  207. */
  208. int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
  209. unsigned int rate)
  210. {
  211. struct mxs_saif *saif = mxs_saif[saif_id];
  212. u32 stat;
  213. int ret;
  214. struct mxs_saif *master_saif;
  215. if (!saif)
  216. return -EINVAL;
  217. /* Clear Reset */
  218. __raw_writel(BM_SAIF_CTRL_SFTRST,
  219. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  220. /* FIXME: need clear clk gate for register r/w */
  221. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  222. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  223. master_saif = mxs_saif_get_master(saif);
  224. if (saif != master_saif) {
  225. dev_err(saif->dev, "can not get mclk from a non-master saif\n");
  226. return -EINVAL;
  227. }
  228. stat = __raw_readl(saif->base + SAIF_STAT);
  229. if (stat & BM_SAIF_STAT_BUSY) {
  230. dev_err(saif->dev, "error: busy\n");
  231. return -EBUSY;
  232. }
  233. saif->mclk_in_use = 1;
  234. ret = mxs_saif_set_clk(saif, mclk, rate);
  235. if (ret)
  236. return ret;
  237. ret = clk_prepare_enable(saif->clk);
  238. if (ret)
  239. return ret;
  240. /* enable MCLK output */
  241. __raw_writel(BM_SAIF_CTRL_RUN,
  242. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  243. return 0;
  244. }
  245. EXPORT_SYMBOL_GPL(mxs_saif_get_mclk);
  246. /*
  247. * SAIF DAI format configuration.
  248. * Should only be called when port is inactive.
  249. */
  250. static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  251. {
  252. u32 scr, stat;
  253. u32 scr0;
  254. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  255. stat = __raw_readl(saif->base + SAIF_STAT);
  256. if (stat & BM_SAIF_STAT_BUSY) {
  257. dev_err(cpu_dai->dev, "error: busy\n");
  258. return -EBUSY;
  259. }
  260. scr0 = __raw_readl(saif->base + SAIF_CTRL);
  261. scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
  262. & ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
  263. scr = 0;
  264. /* DAI mode */
  265. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  266. case SND_SOC_DAIFMT_I2S:
  267. /* data frame low 1clk before data */
  268. scr |= BM_SAIF_CTRL_DELAY;
  269. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  270. break;
  271. case SND_SOC_DAIFMT_LEFT_J:
  272. /* data frame high with data */
  273. scr &= ~BM_SAIF_CTRL_DELAY;
  274. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  275. scr &= ~BM_SAIF_CTRL_JUSTIFY;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. /* DAI clock inversion */
  281. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  282. case SND_SOC_DAIFMT_IB_IF:
  283. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  284. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  285. break;
  286. case SND_SOC_DAIFMT_IB_NF:
  287. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  288. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  289. break;
  290. case SND_SOC_DAIFMT_NB_IF:
  291. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  292. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  293. break;
  294. case SND_SOC_DAIFMT_NB_NF:
  295. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  296. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  297. break;
  298. }
  299. /*
  300. * Note: We simply just support master mode since SAIF TX can only
  301. * work as master.
  302. * Here the master is relative to codec side.
  303. * Saif internally could be slave when working on EXTMASTER mode.
  304. * We just hide this to machine driver.
  305. */
  306. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  307. case SND_SOC_DAIFMT_CBS_CFS:
  308. if (saif->id == saif->master_id)
  309. scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
  310. else
  311. scr |= BM_SAIF_CTRL_SLAVE_MODE;
  312. __raw_writel(scr | scr0, saif->base + SAIF_CTRL);
  313. break;
  314. default:
  315. return -EINVAL;
  316. }
  317. return 0;
  318. }
  319. static int mxs_saif_startup(struct snd_pcm_substream *substream,
  320. struct snd_soc_dai *cpu_dai)
  321. {
  322. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  323. /* clear error status to 0 for each re-open */
  324. saif->fifo_underrun = 0;
  325. saif->fifo_overrun = 0;
  326. /* Clear Reset for normal operations */
  327. __raw_writel(BM_SAIF_CTRL_SFTRST,
  328. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  329. /* clear clock gate */
  330. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  331. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  332. return 0;
  333. }
  334. /*
  335. * Should only be called when port is inactive.
  336. * although can be called multiple times by upper layers.
  337. */
  338. static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
  339. struct snd_pcm_hw_params *params,
  340. struct snd_soc_dai *cpu_dai)
  341. {
  342. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  343. struct mxs_saif *master_saif;
  344. u32 scr, stat;
  345. int ret;
  346. master_saif = mxs_saif_get_master(saif);
  347. if (!master_saif)
  348. return -EINVAL;
  349. /* mclk should already be set */
  350. if (!saif->mclk && saif->mclk_in_use) {
  351. dev_err(cpu_dai->dev, "set mclk first\n");
  352. return -EINVAL;
  353. }
  354. stat = __raw_readl(saif->base + SAIF_STAT);
  355. if (stat & BM_SAIF_STAT_BUSY) {
  356. dev_err(cpu_dai->dev, "error: busy\n");
  357. return -EBUSY;
  358. }
  359. /*
  360. * Set saif clk based on sample rate.
  361. * If mclk is used, we also set mclk, if not, saif->mclk is
  362. * default 0, means not used.
  363. */
  364. ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
  365. if (ret) {
  366. dev_err(cpu_dai->dev, "unable to get proper clk\n");
  367. return ret;
  368. }
  369. /* prepare clk in hw_param, enable in trigger */
  370. clk_prepare(saif->clk);
  371. if (saif != master_saif) {
  372. /*
  373. * Set an initial clock rate for the saif internal logic to work
  374. * properly. This is important when working in EXTMASTER mode
  375. * that uses the other saif's BITCLK&LRCLK but it still needs a
  376. * basic clock which should be fast enough for the internal
  377. * logic.
  378. */
  379. clk_enable(saif->clk);
  380. ret = clk_set_rate(saif->clk, 24000000);
  381. clk_disable(saif->clk);
  382. if (ret)
  383. return ret;
  384. clk_prepare(master_saif->clk);
  385. }
  386. scr = __raw_readl(saif->base + SAIF_CTRL);
  387. scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
  388. scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  389. switch (params_format(params)) {
  390. case SNDRV_PCM_FORMAT_S16_LE:
  391. scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
  392. break;
  393. case SNDRV_PCM_FORMAT_S20_3LE:
  394. scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
  395. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  396. break;
  397. case SNDRV_PCM_FORMAT_S24_LE:
  398. scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
  399. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  400. break;
  401. default:
  402. return -EINVAL;
  403. }
  404. /* Tx/Rx config */
  405. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  406. /* enable TX mode */
  407. scr &= ~BM_SAIF_CTRL_READ_MODE;
  408. } else {
  409. /* enable RX mode */
  410. scr |= BM_SAIF_CTRL_READ_MODE;
  411. }
  412. __raw_writel(scr, saif->base + SAIF_CTRL);
  413. return 0;
  414. }
  415. static int mxs_saif_prepare(struct snd_pcm_substream *substream,
  416. struct snd_soc_dai *cpu_dai)
  417. {
  418. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  419. /* enable FIFO error irqs */
  420. __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
  421. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  422. return 0;
  423. }
  424. static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
  425. struct snd_soc_dai *cpu_dai)
  426. {
  427. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  428. struct mxs_saif *master_saif;
  429. u32 delay;
  430. int ret;
  431. master_saif = mxs_saif_get_master(saif);
  432. if (!master_saif)
  433. return -EINVAL;
  434. switch (cmd) {
  435. case SNDRV_PCM_TRIGGER_START:
  436. case SNDRV_PCM_TRIGGER_RESUME:
  437. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  438. if (saif->state == MXS_SAIF_STATE_RUNNING)
  439. return 0;
  440. dev_dbg(cpu_dai->dev, "start\n");
  441. ret = clk_enable(master_saif->clk);
  442. if (ret) {
  443. dev_err(saif->dev, "Failed to enable master clock\n");
  444. return ret;
  445. }
  446. /*
  447. * If the saif's master is not itself, we also need to enable
  448. * itself clk for its internal basic logic to work.
  449. */
  450. if (saif != master_saif) {
  451. ret = clk_enable(saif->clk);
  452. if (ret) {
  453. dev_err(saif->dev, "Failed to enable master clock\n");
  454. clk_disable(master_saif->clk);
  455. return ret;
  456. }
  457. __raw_writel(BM_SAIF_CTRL_RUN,
  458. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  459. }
  460. if (!master_saif->mclk_in_use)
  461. __raw_writel(BM_SAIF_CTRL_RUN,
  462. master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
  463. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  464. /*
  465. * write data to saif data register to trigger
  466. * the transfer.
  467. * For 24-bit format the 32-bit FIFO register stores
  468. * only one channel, so we need to write twice.
  469. * This is also safe for the other non 24-bit formats.
  470. */
  471. __raw_writel(0, saif->base + SAIF_DATA);
  472. __raw_writel(0, saif->base + SAIF_DATA);
  473. } else {
  474. /*
  475. * read data from saif data register to trigger
  476. * the receive.
  477. * For 24-bit format the 32-bit FIFO register stores
  478. * only one channel, so we need to read twice.
  479. * This is also safe for the other non 24-bit formats.
  480. */
  481. __raw_readl(saif->base + SAIF_DATA);
  482. __raw_readl(saif->base + SAIF_DATA);
  483. }
  484. master_saif->ongoing = 1;
  485. saif->state = MXS_SAIF_STATE_RUNNING;
  486. dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
  487. __raw_readl(saif->base + SAIF_CTRL),
  488. __raw_readl(saif->base + SAIF_STAT));
  489. dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
  490. __raw_readl(master_saif->base + SAIF_CTRL),
  491. __raw_readl(master_saif->base + SAIF_STAT));
  492. break;
  493. case SNDRV_PCM_TRIGGER_SUSPEND:
  494. case SNDRV_PCM_TRIGGER_STOP:
  495. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  496. if (saif->state == MXS_SAIF_STATE_STOPPED)
  497. return 0;
  498. dev_dbg(cpu_dai->dev, "stop\n");
  499. /* wait a while for the current sample to complete */
  500. delay = USEC_PER_SEC / master_saif->cur_rate;
  501. if (!master_saif->mclk_in_use) {
  502. __raw_writel(BM_SAIF_CTRL_RUN,
  503. master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  504. udelay(delay);
  505. }
  506. clk_disable(master_saif->clk);
  507. if (saif != master_saif) {
  508. __raw_writel(BM_SAIF_CTRL_RUN,
  509. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  510. udelay(delay);
  511. clk_disable(saif->clk);
  512. }
  513. master_saif->ongoing = 0;
  514. saif->state = MXS_SAIF_STATE_STOPPED;
  515. break;
  516. default:
  517. return -EINVAL;
  518. }
  519. return 0;
  520. }
  521. #define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
  522. #define MXS_SAIF_FORMATS \
  523. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  524. SNDRV_PCM_FMTBIT_S24_LE)
  525. static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
  526. .startup = mxs_saif_startup,
  527. .trigger = mxs_saif_trigger,
  528. .prepare = mxs_saif_prepare,
  529. .hw_params = mxs_saif_hw_params,
  530. .set_sysclk = mxs_saif_set_dai_sysclk,
  531. .set_fmt = mxs_saif_set_dai_fmt,
  532. };
  533. static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
  534. {
  535. struct mxs_saif *saif = dev_get_drvdata(dai->dev);
  536. snd_soc_dai_set_drvdata(dai, saif);
  537. return 0;
  538. }
  539. static struct snd_soc_dai_driver mxs_saif_dai = {
  540. .name = "mxs-saif",
  541. .probe = mxs_saif_dai_probe,
  542. .playback = {
  543. .channels_min = 2,
  544. .channels_max = 2,
  545. .rates = MXS_SAIF_RATES,
  546. .formats = MXS_SAIF_FORMATS,
  547. },
  548. .capture = {
  549. .channels_min = 2,
  550. .channels_max = 2,
  551. .rates = MXS_SAIF_RATES,
  552. .formats = MXS_SAIF_FORMATS,
  553. },
  554. .ops = &mxs_saif_dai_ops,
  555. };
  556. static const struct snd_soc_component_driver mxs_saif_component = {
  557. .name = "mxs-saif",
  558. };
  559. static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
  560. {
  561. struct mxs_saif *saif = dev_id;
  562. unsigned int stat;
  563. stat = __raw_readl(saif->base + SAIF_STAT);
  564. if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
  565. BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
  566. return IRQ_NONE;
  567. if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
  568. dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
  569. __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
  570. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  571. }
  572. if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
  573. dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
  574. __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
  575. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  576. }
  577. dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
  578. __raw_readl(saif->base + SAIF_CTRL),
  579. __raw_readl(saif->base + SAIF_STAT));
  580. return IRQ_HANDLED;
  581. }
  582. static int mxs_saif_mclk_init(struct platform_device *pdev)
  583. {
  584. struct mxs_saif *saif = platform_get_drvdata(pdev);
  585. struct device_node *np = pdev->dev.of_node;
  586. struct clk *clk;
  587. int ret;
  588. clk = clk_register_divider(&pdev->dev, "mxs_saif_mclk",
  589. __clk_get_name(saif->clk), 0,
  590. saif->base + SAIF_CTRL,
  591. BP_SAIF_CTRL_BITCLK_MULT_RATE, 3,
  592. 0, NULL);
  593. if (IS_ERR(clk)) {
  594. ret = PTR_ERR(clk);
  595. if (ret == -EEXIST)
  596. return 0;
  597. dev_err(&pdev->dev, "failed to register mclk: %d\n", ret);
  598. return PTR_ERR(clk);
  599. }
  600. ret = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  601. if (ret)
  602. return ret;
  603. return 0;
  604. }
  605. static int mxs_saif_probe(struct platform_device *pdev)
  606. {
  607. struct device_node *np = pdev->dev.of_node;
  608. struct resource *iores;
  609. struct mxs_saif *saif;
  610. int irq, ret = 0;
  611. struct device_node *master;
  612. if (!np)
  613. return -EINVAL;
  614. saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
  615. if (!saif)
  616. return -ENOMEM;
  617. ret = of_alias_get_id(np, "saif");
  618. if (ret < 0)
  619. return ret;
  620. else
  621. saif->id = ret;
  622. /*
  623. * If there is no "fsl,saif-master" phandle, it's a saif
  624. * master. Otherwise, it's a slave and its phandle points
  625. * to the master.
  626. */
  627. master = of_parse_phandle(np, "fsl,saif-master", 0);
  628. if (!master) {
  629. saif->master_id = saif->id;
  630. } else {
  631. ret = of_alias_get_id(master, "saif");
  632. if (ret < 0)
  633. return ret;
  634. else
  635. saif->master_id = ret;
  636. }
  637. if (saif->master_id >= ARRAY_SIZE(mxs_saif)) {
  638. dev_err(&pdev->dev, "get wrong master id\n");
  639. return -EINVAL;
  640. }
  641. mxs_saif[saif->id] = saif;
  642. saif->clk = devm_clk_get(&pdev->dev, NULL);
  643. if (IS_ERR(saif->clk)) {
  644. ret = PTR_ERR(saif->clk);
  645. dev_err(&pdev->dev, "Cannot get the clock: %d\n",
  646. ret);
  647. return ret;
  648. }
  649. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  650. saif->base = devm_ioremap_resource(&pdev->dev, iores);
  651. if (IS_ERR(saif->base))
  652. return PTR_ERR(saif->base);
  653. irq = platform_get_irq(pdev, 0);
  654. if (irq < 0) {
  655. ret = irq;
  656. dev_err(&pdev->dev, "failed to get irq resource: %d\n",
  657. ret);
  658. return ret;
  659. }
  660. saif->dev = &pdev->dev;
  661. ret = devm_request_irq(&pdev->dev, irq, mxs_saif_irq, 0,
  662. dev_name(&pdev->dev), saif);
  663. if (ret) {
  664. dev_err(&pdev->dev, "failed to request irq\n");
  665. return ret;
  666. }
  667. platform_set_drvdata(pdev, saif);
  668. /* We only support saif0 being tx and clock master */
  669. if (saif->id == 0) {
  670. ret = mxs_saif_mclk_init(pdev);
  671. if (ret)
  672. dev_warn(&pdev->dev, "failed to init clocks\n");
  673. }
  674. ret = devm_snd_soc_register_component(&pdev->dev, &mxs_saif_component,
  675. &mxs_saif_dai, 1);
  676. if (ret) {
  677. dev_err(&pdev->dev, "register DAI failed\n");
  678. return ret;
  679. }
  680. ret = mxs_pcm_platform_register(&pdev->dev);
  681. if (ret) {
  682. dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
  683. return ret;
  684. }
  685. return 0;
  686. }
  687. static const struct of_device_id mxs_saif_dt_ids[] = {
  688. { .compatible = "fsl,imx28-saif", },
  689. { /* sentinel */ }
  690. };
  691. MODULE_DEVICE_TABLE(of, mxs_saif_dt_ids);
  692. static struct platform_driver mxs_saif_driver = {
  693. .probe = mxs_saif_probe,
  694. .driver = {
  695. .name = "mxs-saif",
  696. .of_match_table = mxs_saif_dt_ids,
  697. },
  698. };
  699. module_platform_driver(mxs_saif_driver);
  700. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  701. MODULE_DESCRIPTION("MXS ASoC SAIF driver");
  702. MODULE_LICENSE("GPL");
  703. MODULE_ALIAS("platform:mxs-saif");