mxs-saif.c 19 KB

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