mxs-saif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 <sound/saif.h>
  34. #include <asm/mach-types.h>
  35. #include <mach/hardware.h>
  36. #include <mach/mxs.h>
  37. #include "mxs-saif.h"
  38. static struct mxs_saif *mxs_saif[2];
  39. /*
  40. * SAIF is a little different with other normal SOC DAIs on clock using.
  41. *
  42. * For MXS, two SAIF modules are instantiated on-chip.
  43. * Each SAIF has a set of clock pins and can be operating in master
  44. * mode simultaneously if they are connected to different off-chip codecs.
  45. * Also, one of the two SAIFs can master or drive the clock pins while the
  46. * other SAIF, in slave mode, receives clocking from the master SAIF.
  47. * This also means that both SAIFs must operate at the same sample rate.
  48. *
  49. * We abstract this as each saif has a master, the master could be
  50. * himself or other saifs. In the generic saif driver, saif does not need
  51. * to know the different clkmux. Saif only needs to know who is his master
  52. * and operating his master to generate the proper clock rate for him.
  53. * The master id is provided in mach-specific layer according to different
  54. * clkmux setting.
  55. */
  56. static int mxs_saif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  57. int clk_id, unsigned int freq, int dir)
  58. {
  59. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  60. switch (clk_id) {
  61. case MXS_SAIF_MCLK:
  62. saif->mclk = freq;
  63. break;
  64. default:
  65. return -EINVAL;
  66. }
  67. return 0;
  68. }
  69. /*
  70. * Since SAIF may work on EXTMASTER mode, IOW, it's working BITCLK&LRCLK
  71. * is provided by other SAIF, we provide a interface here to get its master
  72. * from its master_id.
  73. * Note that the master could be himself.
  74. */
  75. static inline struct mxs_saif *mxs_saif_get_master(struct mxs_saif * saif)
  76. {
  77. return mxs_saif[saif->master_id];
  78. }
  79. /*
  80. * Set SAIF clock and MCLK
  81. */
  82. static int mxs_saif_set_clk(struct mxs_saif *saif,
  83. unsigned int mclk,
  84. unsigned int rate)
  85. {
  86. u32 scr;
  87. int ret;
  88. struct mxs_saif *master_saif;
  89. dev_dbg(saif->dev, "mclk %d rate %d\n", mclk, rate);
  90. /* Set master saif to generate proper clock */
  91. master_saif = mxs_saif_get_master(saif);
  92. if (!master_saif)
  93. return -EINVAL;
  94. dev_dbg(saif->dev, "master saif%d\n", master_saif->id);
  95. /* Checking if can playback and capture simutaneously */
  96. if (master_saif->ongoing && rate != master_saif->cur_rate) {
  97. dev_err(saif->dev,
  98. "can not change clock, master saif%d(rate %d) is ongoing\n",
  99. master_saif->id, master_saif->cur_rate);
  100. return -EINVAL;
  101. }
  102. scr = __raw_readl(master_saif->base + SAIF_CTRL);
  103. scr &= ~BM_SAIF_CTRL_BITCLK_MULT_RATE;
  104. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  105. /*
  106. * Set SAIF clock
  107. *
  108. * The SAIF clock should be either 384*fs or 512*fs.
  109. * If MCLK is used, the SAIF clk ratio need to match mclk ratio.
  110. * For 32x mclk, set saif clk as 512*fs.
  111. * For 48x mclk, set saif clk as 384*fs.
  112. *
  113. * If MCLK is not used, we just set saif clk to 512*fs.
  114. */
  115. clk_prepare_enable(master_saif->clk);
  116. if (master_saif->mclk_in_use) {
  117. if (mclk % 32 == 0) {
  118. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  119. ret = clk_set_rate(master_saif->clk, 512 * rate);
  120. } else if (mclk % 48 == 0) {
  121. scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
  122. ret = clk_set_rate(master_saif->clk, 384 * rate);
  123. } else {
  124. /* SAIF MCLK should be either 32x or 48x */
  125. clk_disable_unprepare(master_saif->clk);
  126. return -EINVAL;
  127. }
  128. } else {
  129. ret = clk_set_rate(master_saif->clk, 512 * rate);
  130. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  131. }
  132. clk_disable_unprepare(master_saif->clk);
  133. if (ret)
  134. return ret;
  135. master_saif->cur_rate = rate;
  136. if (!master_saif->mclk_in_use) {
  137. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  138. return 0;
  139. }
  140. /*
  141. * Program the over-sample rate for MCLK output
  142. *
  143. * The available MCLK range is 32x, 48x... 512x. The rate
  144. * could be from 8kHz to 192kH.
  145. */
  146. switch (mclk / rate) {
  147. case 32:
  148. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
  149. break;
  150. case 64:
  151. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  152. break;
  153. case 128:
  154. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  155. break;
  156. case 256:
  157. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  158. break;
  159. case 512:
  160. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  161. break;
  162. case 48:
  163. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  164. break;
  165. case 96:
  166. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  167. break;
  168. case 192:
  169. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  170. break;
  171. case 384:
  172. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  178. return 0;
  179. }
  180. /*
  181. * Put and disable MCLK.
  182. */
  183. int mxs_saif_put_mclk(unsigned int saif_id)
  184. {
  185. struct mxs_saif *saif = mxs_saif[saif_id];
  186. u32 stat;
  187. if (!saif)
  188. return -EINVAL;
  189. stat = __raw_readl(saif->base + SAIF_STAT);
  190. if (stat & BM_SAIF_STAT_BUSY) {
  191. dev_err(saif->dev, "error: busy\n");
  192. return -EBUSY;
  193. }
  194. clk_disable_unprepare(saif->clk);
  195. /* disable MCLK output */
  196. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  197. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  198. __raw_writel(BM_SAIF_CTRL_RUN,
  199. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  200. saif->mclk_in_use = 0;
  201. return 0;
  202. }
  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. /*
  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. snd_soc_dai_set_dma_data(cpu_dai, substream, &saif->dma_param);
  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. u32 scr, stat;
  345. int ret;
  346. /* mclk should already be set */
  347. if (!saif->mclk && saif->mclk_in_use) {
  348. dev_err(cpu_dai->dev, "set mclk first\n");
  349. return -EINVAL;
  350. }
  351. stat = __raw_readl(saif->base + SAIF_STAT);
  352. if (stat & BM_SAIF_STAT_BUSY) {
  353. dev_err(cpu_dai->dev, "error: busy\n");
  354. return -EBUSY;
  355. }
  356. /*
  357. * Set saif clk based on sample rate.
  358. * If mclk is used, we also set mclk, if not, saif->mclk is
  359. * default 0, means not used.
  360. */
  361. ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
  362. if (ret) {
  363. dev_err(cpu_dai->dev, "unable to get proper clk\n");
  364. return ret;
  365. }
  366. scr = __raw_readl(saif->base + SAIF_CTRL);
  367. scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
  368. scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  369. switch (params_format(params)) {
  370. case SNDRV_PCM_FORMAT_S16_LE:
  371. scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
  372. break;
  373. case SNDRV_PCM_FORMAT_S20_3LE:
  374. scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
  375. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  376. break;
  377. case SNDRV_PCM_FORMAT_S24_LE:
  378. scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
  379. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  380. break;
  381. default:
  382. return -EINVAL;
  383. }
  384. /* Tx/Rx config */
  385. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  386. /* enable TX mode */
  387. scr &= ~BM_SAIF_CTRL_READ_MODE;
  388. } else {
  389. /* enable RX mode */
  390. scr |= BM_SAIF_CTRL_READ_MODE;
  391. }
  392. __raw_writel(scr, saif->base + SAIF_CTRL);
  393. return 0;
  394. }
  395. static int mxs_saif_prepare(struct snd_pcm_substream *substream,
  396. struct snd_soc_dai *cpu_dai)
  397. {
  398. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  399. /* enable FIFO error irqs */
  400. __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
  401. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  402. return 0;
  403. }
  404. static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
  405. struct snd_soc_dai *cpu_dai)
  406. {
  407. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  408. struct mxs_saif *master_saif;
  409. u32 delay;
  410. master_saif = mxs_saif_get_master(saif);
  411. if (!master_saif)
  412. return -EINVAL;
  413. switch (cmd) {
  414. case SNDRV_PCM_TRIGGER_START:
  415. case SNDRV_PCM_TRIGGER_RESUME:
  416. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  417. dev_dbg(cpu_dai->dev, "start\n");
  418. clk_enable(master_saif->clk);
  419. if (!master_saif->mclk_in_use)
  420. __raw_writel(BM_SAIF_CTRL_RUN,
  421. master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
  422. /*
  423. * If the saif's master is not himself, we also need to enable
  424. * itself clk for its internal basic logic to work.
  425. */
  426. if (saif != master_saif) {
  427. clk_enable(saif->clk);
  428. __raw_writel(BM_SAIF_CTRL_RUN,
  429. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  430. }
  431. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  432. /*
  433. * write a data to saif data register to trigger
  434. * the transfer
  435. */
  436. __raw_writel(0, saif->base + SAIF_DATA);
  437. } else {
  438. /*
  439. * read a data from saif data register to trigger
  440. * the receive
  441. */
  442. __raw_readl(saif->base + SAIF_DATA);
  443. }
  444. master_saif->ongoing = 1;
  445. dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
  446. __raw_readl(saif->base + SAIF_CTRL),
  447. __raw_readl(saif->base + SAIF_STAT));
  448. dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
  449. __raw_readl(master_saif->base + SAIF_CTRL),
  450. __raw_readl(master_saif->base + SAIF_STAT));
  451. break;
  452. case SNDRV_PCM_TRIGGER_SUSPEND:
  453. case SNDRV_PCM_TRIGGER_STOP:
  454. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  455. dev_dbg(cpu_dai->dev, "stop\n");
  456. /* wait a while for the current sample to complete */
  457. delay = USEC_PER_SEC / master_saif->cur_rate;
  458. if (!master_saif->mclk_in_use) {
  459. __raw_writel(BM_SAIF_CTRL_RUN,
  460. master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  461. udelay(delay);
  462. }
  463. clk_disable(master_saif->clk);
  464. if (saif != master_saif) {
  465. __raw_writel(BM_SAIF_CTRL_RUN,
  466. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  467. udelay(delay);
  468. clk_disable(saif->clk);
  469. }
  470. master_saif->ongoing = 0;
  471. break;
  472. default:
  473. return -EINVAL;
  474. }
  475. return 0;
  476. }
  477. #define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
  478. #define MXS_SAIF_FORMATS \
  479. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  480. SNDRV_PCM_FMTBIT_S24_LE)
  481. static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
  482. .startup = mxs_saif_startup,
  483. .trigger = mxs_saif_trigger,
  484. .prepare = mxs_saif_prepare,
  485. .hw_params = mxs_saif_hw_params,
  486. .set_sysclk = mxs_saif_set_dai_sysclk,
  487. .set_fmt = mxs_saif_set_dai_fmt,
  488. };
  489. static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
  490. {
  491. struct mxs_saif *saif = dev_get_drvdata(dai->dev);
  492. snd_soc_dai_set_drvdata(dai, saif);
  493. return 0;
  494. }
  495. static struct snd_soc_dai_driver mxs_saif_dai = {
  496. .name = "mxs-saif",
  497. .probe = mxs_saif_dai_probe,
  498. .playback = {
  499. .channels_min = 2,
  500. .channels_max = 2,
  501. .rates = MXS_SAIF_RATES,
  502. .formats = MXS_SAIF_FORMATS,
  503. },
  504. .capture = {
  505. .channels_min = 2,
  506. .channels_max = 2,
  507. .rates = MXS_SAIF_RATES,
  508. .formats = MXS_SAIF_FORMATS,
  509. },
  510. .ops = &mxs_saif_dai_ops,
  511. };
  512. static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
  513. {
  514. struct mxs_saif *saif = dev_id;
  515. unsigned int stat;
  516. stat = __raw_readl(saif->base + SAIF_STAT);
  517. if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
  518. BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
  519. return IRQ_NONE;
  520. if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
  521. dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
  522. __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
  523. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  524. }
  525. if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
  526. dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
  527. __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
  528. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  529. }
  530. dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
  531. __raw_readl(saif->base + SAIF_CTRL),
  532. __raw_readl(saif->base + SAIF_STAT));
  533. return IRQ_HANDLED;
  534. }
  535. static int __devinit mxs_saif_probe(struct platform_device *pdev)
  536. {
  537. struct device_node *np = pdev->dev.of_node;
  538. struct resource *iores, *dmares;
  539. struct mxs_saif *saif;
  540. struct mxs_saif_platform_data *pdata;
  541. int ret = 0;
  542. if (!np && pdev->id >= ARRAY_SIZE(mxs_saif))
  543. return -EINVAL;
  544. saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
  545. if (!saif)
  546. return -ENOMEM;
  547. if (np) {
  548. struct device_node *master;
  549. saif->id = of_alias_get_id(np, "saif");
  550. if (saif->id < 0)
  551. return saif->id;
  552. /*
  553. * If there is no "fsl,saif-master" phandle, it's a saif
  554. * master. Otherwise, it's a slave and its phandle points
  555. * to the master.
  556. */
  557. master = of_parse_phandle(np, "fsl,saif-master", 0);
  558. if (!master) {
  559. saif->master_id = saif->id;
  560. } else {
  561. saif->master_id = of_alias_get_id(master, "saif");
  562. if (saif->master_id < 0)
  563. return saif->master_id;
  564. }
  565. } else {
  566. saif->id = pdev->id;
  567. pdata = pdev->dev.platform_data;
  568. if (pdata && !pdata->master_mode)
  569. saif->master_id = pdata->master_id;
  570. else
  571. saif->master_id = saif->id;
  572. }
  573. if (saif->master_id < 0 || saif->master_id >= ARRAY_SIZE(mxs_saif)) {
  574. dev_err(&pdev->dev, "get wrong master id\n");
  575. return -EINVAL;
  576. }
  577. mxs_saif[saif->id] = saif;
  578. saif->clk = clk_get(&pdev->dev, NULL);
  579. if (IS_ERR(saif->clk)) {
  580. ret = PTR_ERR(saif->clk);
  581. dev_err(&pdev->dev, "Cannot get the clock: %d\n",
  582. ret);
  583. return ret;
  584. }
  585. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  586. saif->base = devm_request_and_ioremap(&pdev->dev, iores);
  587. if (!saif->base) {
  588. dev_err(&pdev->dev, "ioremap failed\n");
  589. ret = -ENODEV;
  590. goto failed_get_resource;
  591. }
  592. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  593. if (!dmares) {
  594. /*
  595. * TODO: This is a temporary solution and should be changed
  596. * to use generic DMA binding later when the helplers get in.
  597. */
  598. ret = of_property_read_u32(np, "fsl,saif-dma-channel",
  599. &saif->dma_param.chan_num);
  600. if (ret) {
  601. dev_err(&pdev->dev, "failed to get dma channel\n");
  602. goto failed_get_resource;
  603. }
  604. } else {
  605. saif->dma_param.chan_num = dmares->start;
  606. }
  607. saif->irq = platform_get_irq(pdev, 0);
  608. if (saif->irq < 0) {
  609. ret = saif->irq;
  610. dev_err(&pdev->dev, "failed to get irq resource: %d\n",
  611. ret);
  612. goto failed_get_resource;
  613. }
  614. saif->dev = &pdev->dev;
  615. ret = devm_request_irq(&pdev->dev, saif->irq, mxs_saif_irq, 0,
  616. "mxs-saif", saif);
  617. if (ret) {
  618. dev_err(&pdev->dev, "failed to request irq\n");
  619. goto failed_get_resource;
  620. }
  621. saif->dma_param.chan_irq = platform_get_irq(pdev, 1);
  622. if (saif->dma_param.chan_irq < 0) {
  623. ret = saif->dma_param.chan_irq;
  624. dev_err(&pdev->dev, "failed to get dma irq resource: %d\n",
  625. ret);
  626. goto failed_get_resource;
  627. }
  628. platform_set_drvdata(pdev, saif);
  629. ret = snd_soc_register_dai(&pdev->dev, &mxs_saif_dai);
  630. if (ret) {
  631. dev_err(&pdev->dev, "register DAI failed\n");
  632. goto failed_get_resource;
  633. }
  634. ret = mxs_pcm_platform_register(&pdev->dev);
  635. if (ret) {
  636. dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
  637. goto failed_pdev_alloc;
  638. }
  639. return 0;
  640. failed_pdev_alloc:
  641. snd_soc_unregister_dai(&pdev->dev);
  642. failed_get_resource:
  643. clk_put(saif->clk);
  644. return ret;
  645. }
  646. static int __devexit mxs_saif_remove(struct platform_device *pdev)
  647. {
  648. struct mxs_saif *saif = platform_get_drvdata(pdev);
  649. mxs_pcm_platform_unregister(&pdev->dev);
  650. snd_soc_unregister_dai(&pdev->dev);
  651. clk_put(saif->clk);
  652. return 0;
  653. }
  654. static const struct of_device_id mxs_saif_dt_ids[] = {
  655. { .compatible = "fsl,imx28-saif", },
  656. { /* sentinel */ }
  657. };
  658. MODULE_DEVICE_TABLE(of, mxs_saif_dt_ids);
  659. static struct platform_driver mxs_saif_driver = {
  660. .probe = mxs_saif_probe,
  661. .remove = __devexit_p(mxs_saif_remove),
  662. .driver = {
  663. .name = "mxs-saif",
  664. .owner = THIS_MODULE,
  665. .of_match_table = mxs_saif_dt_ids,
  666. },
  667. };
  668. module_platform_driver(mxs_saif_driver);
  669. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  670. MODULE_DESCRIPTION("MXS ASoC SAIF driver");
  671. MODULE_LICENSE("GPL");