mxs-saif.c 20 KB

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