mxs-saif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/clk.h>
  24. #include <linux/delay.h>
  25. #include <linux/time.h>
  26. #include <linux/fsl/mxs-dma.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include <sound/saif.h>
  32. #include <asm/mach-types.h>
  33. #include <mach/hardware.h>
  34. #include <mach/mxs.h>
  35. #include "mxs-saif.h"
  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. * himself 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 his master
  50. * and operating his master to generate the proper clock rate for him.
  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 himself.
  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. /*
  202. * Get MCLK and set clock rate, then enable it
  203. *
  204. * This interface is used for codecs who are using MCLK provided
  205. * by saif.
  206. */
  207. int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
  208. unsigned int rate)
  209. {
  210. struct mxs_saif *saif = mxs_saif[saif_id];
  211. u32 stat;
  212. int ret;
  213. struct mxs_saif *master_saif;
  214. if (!saif)
  215. return -EINVAL;
  216. /* Clear Reset */
  217. __raw_writel(BM_SAIF_CTRL_SFTRST,
  218. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  219. /* FIXME: need clear clk gate for register r/w */
  220. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  221. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  222. master_saif = mxs_saif_get_master(saif);
  223. if (saif != master_saif) {
  224. dev_err(saif->dev, "can not get mclk from a non-master saif\n");
  225. return -EINVAL;
  226. }
  227. stat = __raw_readl(saif->base + SAIF_STAT);
  228. if (stat & BM_SAIF_STAT_BUSY) {
  229. dev_err(saif->dev, "error: busy\n");
  230. return -EBUSY;
  231. }
  232. saif->mclk_in_use = 1;
  233. ret = mxs_saif_set_clk(saif, mclk, rate);
  234. if (ret)
  235. return ret;
  236. ret = clk_prepare_enable(saif->clk);
  237. if (ret)
  238. return ret;
  239. /* enable MCLK output */
  240. __raw_writel(BM_SAIF_CTRL_RUN,
  241. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  242. return 0;
  243. }
  244. /*
  245. * SAIF DAI format configuration.
  246. * Should only be called when port is inactive.
  247. */
  248. static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  249. {
  250. u32 scr, stat;
  251. u32 scr0;
  252. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  253. stat = __raw_readl(saif->base + SAIF_STAT);
  254. if (stat & BM_SAIF_STAT_BUSY) {
  255. dev_err(cpu_dai->dev, "error: busy\n");
  256. return -EBUSY;
  257. }
  258. scr0 = __raw_readl(saif->base + SAIF_CTRL);
  259. scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
  260. & ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
  261. scr = 0;
  262. /* DAI mode */
  263. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  264. case SND_SOC_DAIFMT_I2S:
  265. /* data frame low 1clk before data */
  266. scr |= BM_SAIF_CTRL_DELAY;
  267. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  268. break;
  269. case SND_SOC_DAIFMT_LEFT_J:
  270. /* data frame high with data */
  271. scr &= ~BM_SAIF_CTRL_DELAY;
  272. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  273. scr &= ~BM_SAIF_CTRL_JUSTIFY;
  274. break;
  275. default:
  276. return -EINVAL;
  277. }
  278. /* DAI clock inversion */
  279. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  280. case SND_SOC_DAIFMT_IB_IF:
  281. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  282. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  283. break;
  284. case SND_SOC_DAIFMT_IB_NF:
  285. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  286. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  287. break;
  288. case SND_SOC_DAIFMT_NB_IF:
  289. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  290. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  291. break;
  292. case SND_SOC_DAIFMT_NB_NF:
  293. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  294. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  295. break;
  296. }
  297. /*
  298. * Note: We simply just support master mode since SAIF TX can only
  299. * work as master.
  300. * Here the master is relative to codec side.
  301. * Saif internally could be slave when working on EXTMASTER mode.
  302. * We just hide this to machine driver.
  303. */
  304. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  305. case SND_SOC_DAIFMT_CBS_CFS:
  306. if (saif->id == saif->master_id)
  307. scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
  308. else
  309. scr |= BM_SAIF_CTRL_SLAVE_MODE;
  310. __raw_writel(scr | scr0, saif->base + SAIF_CTRL);
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. return 0;
  316. }
  317. static int mxs_saif_startup(struct snd_pcm_substream *substream,
  318. struct snd_soc_dai *cpu_dai)
  319. {
  320. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  321. snd_soc_dai_set_dma_data(cpu_dai, substream, &saif->dma_param);
  322. /* clear error status to 0 for each re-open */
  323. saif->fifo_underrun = 0;
  324. saif->fifo_overrun = 0;
  325. /* Clear Reset for normal operations */
  326. __raw_writel(BM_SAIF_CTRL_SFTRST,
  327. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  328. /* clear clock gate */
  329. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  330. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  331. return 0;
  332. }
  333. /*
  334. * Should only be called when port is inactive.
  335. * although can be called multiple times by upper layers.
  336. */
  337. static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
  338. struct snd_pcm_hw_params *params,
  339. struct snd_soc_dai *cpu_dai)
  340. {
  341. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  342. u32 scr, stat;
  343. int ret;
  344. /* mclk should already be set */
  345. if (!saif->mclk && saif->mclk_in_use) {
  346. dev_err(cpu_dai->dev, "set mclk first\n");
  347. return -EINVAL;
  348. }
  349. stat = __raw_readl(saif->base + SAIF_STAT);
  350. if (stat & BM_SAIF_STAT_BUSY) {
  351. dev_err(cpu_dai->dev, "error: busy\n");
  352. return -EBUSY;
  353. }
  354. /*
  355. * Set saif clk based on sample rate.
  356. * If mclk is used, we also set mclk, if not, saif->mclk is
  357. * default 0, means not used.
  358. */
  359. ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
  360. if (ret) {
  361. dev_err(cpu_dai->dev, "unable to get proper clk\n");
  362. return ret;
  363. }
  364. scr = __raw_readl(saif->base + SAIF_CTRL);
  365. scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
  366. scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  367. switch (params_format(params)) {
  368. case SNDRV_PCM_FORMAT_S16_LE:
  369. scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
  370. break;
  371. case SNDRV_PCM_FORMAT_S20_3LE:
  372. scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
  373. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  374. break;
  375. case SNDRV_PCM_FORMAT_S24_LE:
  376. scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
  377. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  378. break;
  379. default:
  380. return -EINVAL;
  381. }
  382. /* Tx/Rx config */
  383. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  384. /* enable TX mode */
  385. scr &= ~BM_SAIF_CTRL_READ_MODE;
  386. } else {
  387. /* enable RX mode */
  388. scr |= BM_SAIF_CTRL_READ_MODE;
  389. }
  390. __raw_writel(scr, saif->base + SAIF_CTRL);
  391. return 0;
  392. }
  393. static int mxs_saif_prepare(struct snd_pcm_substream *substream,
  394. struct snd_soc_dai *cpu_dai)
  395. {
  396. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  397. /* enable FIFO error irqs */
  398. __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
  399. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  400. return 0;
  401. }
  402. static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
  403. struct snd_soc_dai *cpu_dai)
  404. {
  405. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  406. struct mxs_saif *master_saif;
  407. u32 delay;
  408. master_saif = mxs_saif_get_master(saif);
  409. if (!master_saif)
  410. return -EINVAL;
  411. switch (cmd) {
  412. case SNDRV_PCM_TRIGGER_START:
  413. case SNDRV_PCM_TRIGGER_RESUME:
  414. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  415. dev_dbg(cpu_dai->dev, "start\n");
  416. clk_enable(master_saif->clk);
  417. if (!master_saif->mclk_in_use)
  418. __raw_writel(BM_SAIF_CTRL_RUN,
  419. master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
  420. /*
  421. * If the saif's master is not himself, we also need to enable
  422. * itself clk for its internal basic logic to work.
  423. */
  424. if (saif != master_saif) {
  425. clk_enable(saif->clk);
  426. __raw_writel(BM_SAIF_CTRL_RUN,
  427. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  428. }
  429. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  430. /*
  431. * write a data to saif data register to trigger
  432. * the transfer
  433. */
  434. __raw_writel(0, saif->base + SAIF_DATA);
  435. } else {
  436. /*
  437. * read a data from saif data register to trigger
  438. * the receive
  439. */
  440. __raw_readl(saif->base + SAIF_DATA);
  441. }
  442. master_saif->ongoing = 1;
  443. dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
  444. __raw_readl(saif->base + SAIF_CTRL),
  445. __raw_readl(saif->base + SAIF_STAT));
  446. dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
  447. __raw_readl(master_saif->base + SAIF_CTRL),
  448. __raw_readl(master_saif->base + SAIF_STAT));
  449. break;
  450. case SNDRV_PCM_TRIGGER_SUSPEND:
  451. case SNDRV_PCM_TRIGGER_STOP:
  452. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  453. dev_dbg(cpu_dai->dev, "stop\n");
  454. /* wait a while for the current sample to complete */
  455. delay = USEC_PER_SEC / master_saif->cur_rate;
  456. if (!master_saif->mclk_in_use) {
  457. __raw_writel(BM_SAIF_CTRL_RUN,
  458. master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  459. udelay(delay);
  460. }
  461. clk_disable(master_saif->clk);
  462. if (saif != master_saif) {
  463. __raw_writel(BM_SAIF_CTRL_RUN,
  464. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  465. udelay(delay);
  466. clk_disable(saif->clk);
  467. }
  468. master_saif->ongoing = 0;
  469. break;
  470. default:
  471. return -EINVAL;
  472. }
  473. return 0;
  474. }
  475. #define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
  476. #define MXS_SAIF_FORMATS \
  477. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  478. SNDRV_PCM_FMTBIT_S24_LE)
  479. static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
  480. .startup = mxs_saif_startup,
  481. .trigger = mxs_saif_trigger,
  482. .prepare = mxs_saif_prepare,
  483. .hw_params = mxs_saif_hw_params,
  484. .set_sysclk = mxs_saif_set_dai_sysclk,
  485. .set_fmt = mxs_saif_set_dai_fmt,
  486. };
  487. static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
  488. {
  489. struct mxs_saif *saif = dev_get_drvdata(dai->dev);
  490. snd_soc_dai_set_drvdata(dai, saif);
  491. return 0;
  492. }
  493. static struct snd_soc_dai_driver mxs_saif_dai = {
  494. .name = "mxs-saif",
  495. .probe = mxs_saif_dai_probe,
  496. .playback = {
  497. .channels_min = 2,
  498. .channels_max = 2,
  499. .rates = MXS_SAIF_RATES,
  500. .formats = MXS_SAIF_FORMATS,
  501. },
  502. .capture = {
  503. .channels_min = 2,
  504. .channels_max = 2,
  505. .rates = MXS_SAIF_RATES,
  506. .formats = MXS_SAIF_FORMATS,
  507. },
  508. .ops = &mxs_saif_dai_ops,
  509. };
  510. static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
  511. {
  512. struct mxs_saif *saif = dev_id;
  513. unsigned int stat;
  514. stat = __raw_readl(saif->base + SAIF_STAT);
  515. if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
  516. BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
  517. return IRQ_NONE;
  518. if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
  519. dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
  520. __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
  521. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  522. }
  523. if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
  524. dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
  525. __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
  526. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  527. }
  528. dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
  529. __raw_readl(saif->base + SAIF_CTRL),
  530. __raw_readl(saif->base + SAIF_STAT));
  531. return IRQ_HANDLED;
  532. }
  533. static int mxs_saif_probe(struct platform_device *pdev)
  534. {
  535. struct resource *iores, *dmares;
  536. struct mxs_saif *saif;
  537. struct mxs_saif_platform_data *pdata;
  538. int ret = 0;
  539. if (pdev->id >= ARRAY_SIZE(mxs_saif))
  540. return -EINVAL;
  541. saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
  542. if (!saif)
  543. return -ENOMEM;
  544. mxs_saif[pdev->id] = saif;
  545. saif->id = pdev->id;
  546. pdata = pdev->dev.platform_data;
  547. if (pdata && !pdata->master_mode) {
  548. saif->master_id = pdata->master_id;
  549. if (saif->master_id < 0 ||
  550. saif->master_id >= ARRAY_SIZE(mxs_saif) ||
  551. saif->master_id == saif->id) {
  552. dev_err(&pdev->dev, "get wrong master id\n");
  553. return -EINVAL;
  554. }
  555. } else {
  556. saif->master_id = saif->id;
  557. }
  558. saif->clk = clk_get(&pdev->dev, NULL);
  559. if (IS_ERR(saif->clk)) {
  560. ret = PTR_ERR(saif->clk);
  561. dev_err(&pdev->dev, "Cannot get the clock: %d\n",
  562. ret);
  563. return ret;
  564. }
  565. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  566. saif->base = devm_request_and_ioremap(&pdev->dev, iores);
  567. if (!saif->base) {
  568. dev_err(&pdev->dev, "ioremap failed\n");
  569. ret = -ENODEV;
  570. goto failed_get_resource;
  571. }
  572. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  573. if (!dmares) {
  574. ret = -ENODEV;
  575. dev_err(&pdev->dev, "failed to get dma resource: %d\n",
  576. ret);
  577. goto failed_get_resource;
  578. }
  579. saif->dma_param.chan_num = dmares->start;
  580. saif->irq = platform_get_irq(pdev, 0);
  581. if (saif->irq < 0) {
  582. ret = saif->irq;
  583. dev_err(&pdev->dev, "failed to get irq resource: %d\n",
  584. ret);
  585. goto failed_get_resource;
  586. }
  587. saif->dev = &pdev->dev;
  588. ret = devm_request_irq(&pdev->dev, saif->irq, mxs_saif_irq, 0,
  589. "mxs-saif", saif);
  590. if (ret) {
  591. dev_err(&pdev->dev, "failed to request irq\n");
  592. goto failed_get_resource;
  593. }
  594. saif->dma_param.chan_irq = platform_get_irq(pdev, 1);
  595. if (saif->dma_param.chan_irq < 0) {
  596. ret = saif->dma_param.chan_irq;
  597. dev_err(&pdev->dev, "failed to get dma irq resource: %d\n",
  598. ret);
  599. goto failed_get_resource;
  600. }
  601. platform_set_drvdata(pdev, saif);
  602. ret = snd_soc_register_dai(&pdev->dev, &mxs_saif_dai);
  603. if (ret) {
  604. dev_err(&pdev->dev, "register DAI failed\n");
  605. goto failed_get_resource;
  606. }
  607. saif->soc_platform_pdev = platform_device_alloc(
  608. "mxs-pcm-audio", pdev->id);
  609. if (!saif->soc_platform_pdev) {
  610. ret = -ENOMEM;
  611. goto failed_pdev_alloc;
  612. }
  613. platform_set_drvdata(saif->soc_platform_pdev, saif);
  614. ret = platform_device_add(saif->soc_platform_pdev);
  615. if (ret) {
  616. dev_err(&pdev->dev, "failed to add soc platform device\n");
  617. goto failed_pdev_add;
  618. }
  619. return 0;
  620. failed_pdev_add:
  621. platform_device_put(saif->soc_platform_pdev);
  622. failed_pdev_alloc:
  623. snd_soc_unregister_dai(&pdev->dev);
  624. failed_get_resource:
  625. clk_put(saif->clk);
  626. return ret;
  627. }
  628. static int __devexit mxs_saif_remove(struct platform_device *pdev)
  629. {
  630. struct mxs_saif *saif = platform_get_drvdata(pdev);
  631. platform_device_unregister(saif->soc_platform_pdev);
  632. snd_soc_unregister_dai(&pdev->dev);
  633. clk_put(saif->clk);
  634. return 0;
  635. }
  636. static struct platform_driver mxs_saif_driver = {
  637. .probe = mxs_saif_probe,
  638. .remove = __devexit_p(mxs_saif_remove),
  639. .driver = {
  640. .name = "mxs-saif",
  641. .owner = THIS_MODULE,
  642. },
  643. };
  644. module_platform_driver(mxs_saif_driver);
  645. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  646. MODULE_DESCRIPTION("MXS ASoC SAIF driver");
  647. MODULE_LICENSE("GPL");