dwc-i2s.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * ALSA SoC Synopsys I2S Audio Layer
  3. *
  4. * sound/soc/dwc/designware_i2s.c
  5. *
  6. * Copyright (C) 2010 ST Microelectronics
  7. * Rajeev Kumar <rajeevkumar.linux@gmail.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/pm_runtime.h>
  21. #include <sound/designware_i2s.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/dmaengine_pcm.h>
  26. #include "local.h"
  27. static inline void i2s_write_reg(void __iomem *io_base, int reg, u32 val)
  28. {
  29. writel(val, io_base + reg);
  30. }
  31. static inline u32 i2s_read_reg(void __iomem *io_base, int reg)
  32. {
  33. return readl(io_base + reg);
  34. }
  35. static inline void i2s_disable_channels(struct dw_i2s_dev *dev, u32 stream)
  36. {
  37. u32 i = 0;
  38. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  39. for (i = 0; i < 4; i++)
  40. i2s_write_reg(dev->i2s_base, TER(i), 0);
  41. } else {
  42. for (i = 0; i < 4; i++)
  43. i2s_write_reg(dev->i2s_base, RER(i), 0);
  44. }
  45. }
  46. static inline void i2s_clear_irqs(struct dw_i2s_dev *dev, u32 stream)
  47. {
  48. u32 i = 0;
  49. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  50. for (i = 0; i < 4; i++)
  51. i2s_read_reg(dev->i2s_base, TOR(i));
  52. } else {
  53. for (i = 0; i < 4; i++)
  54. i2s_read_reg(dev->i2s_base, ROR(i));
  55. }
  56. }
  57. static inline void i2s_disable_irqs(struct dw_i2s_dev *dev, u32 stream,
  58. int chan_nr)
  59. {
  60. u32 i, irq;
  61. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  62. for (i = 0; i < (chan_nr / 2); i++) {
  63. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  64. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x30);
  65. }
  66. } else {
  67. for (i = 0; i < (chan_nr / 2); i++) {
  68. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  69. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x03);
  70. }
  71. }
  72. }
  73. static inline void i2s_enable_irqs(struct dw_i2s_dev *dev, u32 stream,
  74. int chan_nr)
  75. {
  76. u32 i, irq;
  77. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  78. for (i = 0; i < (chan_nr / 2); i++) {
  79. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  80. i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x30);
  81. }
  82. } else {
  83. for (i = 0; i < (chan_nr / 2); i++) {
  84. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  85. i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x03);
  86. }
  87. }
  88. }
  89. static irqreturn_t i2s_irq_handler(int irq, void *dev_id)
  90. {
  91. struct dw_i2s_dev *dev = dev_id;
  92. bool irq_valid = false;
  93. u32 isr[4];
  94. int i;
  95. for (i = 0; i < 4; i++)
  96. isr[i] = i2s_read_reg(dev->i2s_base, ISR(i));
  97. i2s_clear_irqs(dev, SNDRV_PCM_STREAM_PLAYBACK);
  98. i2s_clear_irqs(dev, SNDRV_PCM_STREAM_CAPTURE);
  99. for (i = 0; i < 4; i++) {
  100. /*
  101. * Check if TX fifo is empty. If empty fill FIFO with samples
  102. * NOTE: Only two channels supported
  103. */
  104. if ((isr[i] & ISR_TXFE) && (i == 0) && dev->use_pio) {
  105. dw_pcm_push_tx(dev);
  106. irq_valid = true;
  107. }
  108. /*
  109. * Data available. Retrieve samples from FIFO
  110. * NOTE: Only two channels supported
  111. */
  112. if ((isr[i] & ISR_RXDA) && (i == 0) && dev->use_pio) {
  113. dw_pcm_pop_rx(dev);
  114. irq_valid = true;
  115. }
  116. /* Error Handling: TX */
  117. if (isr[i] & ISR_TXFO) {
  118. dev_err(dev->dev, "TX overrun (ch_id=%d)\n", i);
  119. irq_valid = true;
  120. }
  121. /* Error Handling: TX */
  122. if (isr[i] & ISR_RXFO) {
  123. dev_err(dev->dev, "RX overrun (ch_id=%d)\n", i);
  124. irq_valid = true;
  125. }
  126. }
  127. if (irq_valid)
  128. return IRQ_HANDLED;
  129. else
  130. return IRQ_NONE;
  131. }
  132. static void i2s_start(struct dw_i2s_dev *dev,
  133. struct snd_pcm_substream *substream)
  134. {
  135. struct i2s_clk_config_data *config = &dev->config;
  136. i2s_write_reg(dev->i2s_base, IER, 1);
  137. i2s_enable_irqs(dev, substream->stream, config->chan_nr);
  138. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  139. i2s_write_reg(dev->i2s_base, ITER, 1);
  140. else
  141. i2s_write_reg(dev->i2s_base, IRER, 1);
  142. i2s_write_reg(dev->i2s_base, CER, 1);
  143. }
  144. static void i2s_stop(struct dw_i2s_dev *dev,
  145. struct snd_pcm_substream *substream)
  146. {
  147. i2s_clear_irqs(dev, substream->stream);
  148. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  149. i2s_write_reg(dev->i2s_base, ITER, 0);
  150. else
  151. i2s_write_reg(dev->i2s_base, IRER, 0);
  152. i2s_disable_irqs(dev, substream->stream, 8);
  153. if (!dev->active) {
  154. i2s_write_reg(dev->i2s_base, CER, 0);
  155. i2s_write_reg(dev->i2s_base, IER, 0);
  156. }
  157. }
  158. static int dw_i2s_startup(struct snd_pcm_substream *substream,
  159. struct snd_soc_dai *cpu_dai)
  160. {
  161. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  162. union dw_i2s_snd_dma_data *dma_data = NULL;
  163. if (!(dev->capability & DWC_I2S_RECORD) &&
  164. (substream->stream == SNDRV_PCM_STREAM_CAPTURE))
  165. return -EINVAL;
  166. if (!(dev->capability & DWC_I2S_PLAY) &&
  167. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  168. return -EINVAL;
  169. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  170. dma_data = &dev->play_dma_data;
  171. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  172. dma_data = &dev->capture_dma_data;
  173. snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)dma_data);
  174. return 0;
  175. }
  176. static void dw_i2s_config(struct dw_i2s_dev *dev, int stream)
  177. {
  178. u32 ch_reg;
  179. struct i2s_clk_config_data *config = &dev->config;
  180. i2s_disable_channels(dev, stream);
  181. for (ch_reg = 0; ch_reg < (config->chan_nr / 2); ch_reg++) {
  182. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  183. i2s_write_reg(dev->i2s_base, TCR(ch_reg),
  184. dev->xfer_resolution);
  185. i2s_write_reg(dev->i2s_base, TFCR(ch_reg),
  186. dev->fifo_th - 1);
  187. i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
  188. } else {
  189. i2s_write_reg(dev->i2s_base, RCR(ch_reg),
  190. dev->xfer_resolution);
  191. i2s_write_reg(dev->i2s_base, RFCR(ch_reg),
  192. dev->fifo_th - 1);
  193. i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
  194. }
  195. }
  196. }
  197. static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
  198. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  199. {
  200. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  201. struct i2s_clk_config_data *config = &dev->config;
  202. int ret;
  203. switch (params_format(params)) {
  204. case SNDRV_PCM_FORMAT_S16_LE:
  205. config->data_width = 16;
  206. dev->ccr = 0x00;
  207. dev->xfer_resolution = 0x02;
  208. break;
  209. case SNDRV_PCM_FORMAT_S24_LE:
  210. config->data_width = 24;
  211. dev->ccr = 0x08;
  212. dev->xfer_resolution = 0x04;
  213. break;
  214. case SNDRV_PCM_FORMAT_S32_LE:
  215. config->data_width = 32;
  216. dev->ccr = 0x10;
  217. dev->xfer_resolution = 0x05;
  218. break;
  219. default:
  220. dev_err(dev->dev, "designware-i2s: unsupported PCM fmt");
  221. return -EINVAL;
  222. }
  223. config->chan_nr = params_channels(params);
  224. switch (config->chan_nr) {
  225. case EIGHT_CHANNEL_SUPPORT:
  226. case SIX_CHANNEL_SUPPORT:
  227. case FOUR_CHANNEL_SUPPORT:
  228. case TWO_CHANNEL_SUPPORT:
  229. break;
  230. default:
  231. dev_err(dev->dev, "channel not supported\n");
  232. return -EINVAL;
  233. }
  234. dw_i2s_config(dev, substream->stream);
  235. i2s_write_reg(dev->i2s_base, CCR, dev->ccr);
  236. config->sample_rate = params_rate(params);
  237. if (dev->capability & DW_I2S_MASTER) {
  238. if (dev->i2s_clk_cfg) {
  239. ret = dev->i2s_clk_cfg(config);
  240. if (ret < 0) {
  241. dev_err(dev->dev, "runtime audio clk config fail\n");
  242. return ret;
  243. }
  244. } else {
  245. u32 bitclk = config->sample_rate *
  246. config->data_width * 2;
  247. ret = clk_set_rate(dev->clk, bitclk);
  248. if (ret) {
  249. dev_err(dev->dev, "Can't set I2S clock rate: %d\n",
  250. ret);
  251. return ret;
  252. }
  253. }
  254. }
  255. return 0;
  256. }
  257. static void dw_i2s_shutdown(struct snd_pcm_substream *substream,
  258. struct snd_soc_dai *dai)
  259. {
  260. snd_soc_dai_set_dma_data(dai, substream, NULL);
  261. }
  262. static int dw_i2s_prepare(struct snd_pcm_substream *substream,
  263. struct snd_soc_dai *dai)
  264. {
  265. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  266. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  267. i2s_write_reg(dev->i2s_base, TXFFR, 1);
  268. else
  269. i2s_write_reg(dev->i2s_base, RXFFR, 1);
  270. return 0;
  271. }
  272. static int dw_i2s_trigger(struct snd_pcm_substream *substream,
  273. int cmd, struct snd_soc_dai *dai)
  274. {
  275. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  276. int ret = 0;
  277. switch (cmd) {
  278. case SNDRV_PCM_TRIGGER_START:
  279. case SNDRV_PCM_TRIGGER_RESUME:
  280. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  281. dev->active++;
  282. i2s_start(dev, substream);
  283. break;
  284. case SNDRV_PCM_TRIGGER_STOP:
  285. case SNDRV_PCM_TRIGGER_SUSPEND:
  286. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  287. dev->active--;
  288. i2s_stop(dev, substream);
  289. break;
  290. default:
  291. ret = -EINVAL;
  292. break;
  293. }
  294. return ret;
  295. }
  296. static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  297. {
  298. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  299. int ret = 0;
  300. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  301. case SND_SOC_DAIFMT_CBM_CFM:
  302. if (dev->capability & DW_I2S_SLAVE)
  303. ret = 0;
  304. else
  305. ret = -EINVAL;
  306. break;
  307. case SND_SOC_DAIFMT_CBS_CFS:
  308. if (dev->capability & DW_I2S_MASTER)
  309. ret = 0;
  310. else
  311. ret = -EINVAL;
  312. break;
  313. case SND_SOC_DAIFMT_CBM_CFS:
  314. case SND_SOC_DAIFMT_CBS_CFM:
  315. ret = -EINVAL;
  316. break;
  317. default:
  318. dev_dbg(dev->dev, "dwc : Invalid master/slave format\n");
  319. ret = -EINVAL;
  320. break;
  321. }
  322. return ret;
  323. }
  324. static const struct snd_soc_dai_ops dw_i2s_dai_ops = {
  325. .startup = dw_i2s_startup,
  326. .shutdown = dw_i2s_shutdown,
  327. .hw_params = dw_i2s_hw_params,
  328. .prepare = dw_i2s_prepare,
  329. .trigger = dw_i2s_trigger,
  330. .set_fmt = dw_i2s_set_fmt,
  331. };
  332. static const struct snd_soc_component_driver dw_i2s_component = {
  333. .name = "dw-i2s",
  334. };
  335. #ifdef CONFIG_PM
  336. static int dw_i2s_runtime_suspend(struct device *dev)
  337. {
  338. struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
  339. if (dw_dev->capability & DW_I2S_MASTER)
  340. clk_disable(dw_dev->clk);
  341. return 0;
  342. }
  343. static int dw_i2s_runtime_resume(struct device *dev)
  344. {
  345. struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
  346. if (dw_dev->capability & DW_I2S_MASTER)
  347. clk_enable(dw_dev->clk);
  348. return 0;
  349. }
  350. static int dw_i2s_suspend(struct snd_soc_dai *dai)
  351. {
  352. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  353. if (dev->capability & DW_I2S_MASTER)
  354. clk_disable(dev->clk);
  355. return 0;
  356. }
  357. static int dw_i2s_resume(struct snd_soc_dai *dai)
  358. {
  359. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  360. if (dev->capability & DW_I2S_MASTER)
  361. clk_enable(dev->clk);
  362. if (dai->playback_active)
  363. dw_i2s_config(dev, SNDRV_PCM_STREAM_PLAYBACK);
  364. if (dai->capture_active)
  365. dw_i2s_config(dev, SNDRV_PCM_STREAM_CAPTURE);
  366. return 0;
  367. }
  368. #else
  369. #define dw_i2s_suspend NULL
  370. #define dw_i2s_resume NULL
  371. #endif
  372. /*
  373. * The following tables allow a direct lookup of various parameters
  374. * defined in the I2S block's configuration in terms of sound system
  375. * parameters. Each table is sized to the number of entries possible
  376. * according to the number of configuration bits describing an I2S
  377. * block parameter.
  378. */
  379. /* Maximum bit resolution of a channel - not uniformly spaced */
  380. static const u32 fifo_width[COMP_MAX_WORDSIZE] = {
  381. 12, 16, 20, 24, 32, 0, 0, 0
  382. };
  383. /* Width of (DMA) bus */
  384. static const u32 bus_widths[COMP_MAX_DATA_WIDTH] = {
  385. DMA_SLAVE_BUSWIDTH_1_BYTE,
  386. DMA_SLAVE_BUSWIDTH_2_BYTES,
  387. DMA_SLAVE_BUSWIDTH_4_BYTES,
  388. DMA_SLAVE_BUSWIDTH_UNDEFINED
  389. };
  390. /* PCM format to support channel resolution */
  391. static const u32 formats[COMP_MAX_WORDSIZE] = {
  392. SNDRV_PCM_FMTBIT_S16_LE,
  393. SNDRV_PCM_FMTBIT_S16_LE,
  394. SNDRV_PCM_FMTBIT_S24_LE,
  395. SNDRV_PCM_FMTBIT_S24_LE,
  396. SNDRV_PCM_FMTBIT_S32_LE,
  397. 0,
  398. 0,
  399. 0
  400. };
  401. static int dw_configure_dai(struct dw_i2s_dev *dev,
  402. struct snd_soc_dai_driver *dw_i2s_dai,
  403. unsigned int rates)
  404. {
  405. /*
  406. * Read component parameter registers to extract
  407. * the I2S block's configuration.
  408. */
  409. u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
  410. u32 comp2 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp2);
  411. u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
  412. u32 idx;
  413. if (dev->capability & DWC_I2S_RECORD &&
  414. dev->quirks & DW_I2S_QUIRK_COMP_PARAM1)
  415. comp1 = comp1 & ~BIT(5);
  416. if (dev->capability & DWC_I2S_PLAY &&
  417. dev->quirks & DW_I2S_QUIRK_COMP_PARAM1)
  418. comp1 = comp1 & ~BIT(6);
  419. if (COMP1_TX_ENABLED(comp1)) {
  420. dev_dbg(dev->dev, " designware: play supported\n");
  421. idx = COMP1_TX_WORDSIZE_0(comp1);
  422. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  423. return -EINVAL;
  424. if (dev->quirks & DW_I2S_QUIRK_16BIT_IDX_OVERRIDE)
  425. idx = 1;
  426. dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
  427. dw_i2s_dai->playback.channels_max =
  428. 1 << (COMP1_TX_CHANNELS(comp1) + 1);
  429. dw_i2s_dai->playback.formats = formats[idx];
  430. dw_i2s_dai->playback.rates = rates;
  431. }
  432. if (COMP1_RX_ENABLED(comp1)) {
  433. dev_dbg(dev->dev, "designware: record supported\n");
  434. idx = COMP2_RX_WORDSIZE_0(comp2);
  435. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  436. return -EINVAL;
  437. if (dev->quirks & DW_I2S_QUIRK_16BIT_IDX_OVERRIDE)
  438. idx = 1;
  439. dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
  440. dw_i2s_dai->capture.channels_max =
  441. 1 << (COMP1_RX_CHANNELS(comp1) + 1);
  442. dw_i2s_dai->capture.formats = formats[idx];
  443. dw_i2s_dai->capture.rates = rates;
  444. }
  445. if (COMP1_MODE_EN(comp1)) {
  446. dev_dbg(dev->dev, "designware: i2s master mode supported\n");
  447. dev->capability |= DW_I2S_MASTER;
  448. } else {
  449. dev_dbg(dev->dev, "designware: i2s slave mode supported\n");
  450. dev->capability |= DW_I2S_SLAVE;
  451. }
  452. dev->fifo_th = fifo_depth / 2;
  453. return 0;
  454. }
  455. static int dw_configure_dai_by_pd(struct dw_i2s_dev *dev,
  456. struct snd_soc_dai_driver *dw_i2s_dai,
  457. struct resource *res,
  458. const struct i2s_platform_data *pdata)
  459. {
  460. u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
  461. u32 idx = COMP1_APB_DATA_WIDTH(comp1);
  462. int ret;
  463. if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
  464. return -EINVAL;
  465. ret = dw_configure_dai(dev, dw_i2s_dai, pdata->snd_rates);
  466. if (ret < 0)
  467. return ret;
  468. if (dev->quirks & DW_I2S_QUIRK_16BIT_IDX_OVERRIDE)
  469. idx = 1;
  470. /* Set DMA slaves info */
  471. dev->play_dma_data.pd.data = pdata->play_dma_data;
  472. dev->capture_dma_data.pd.data = pdata->capture_dma_data;
  473. dev->play_dma_data.pd.addr = res->start + I2S_TXDMA;
  474. dev->capture_dma_data.pd.addr = res->start + I2S_RXDMA;
  475. dev->play_dma_data.pd.max_burst = 16;
  476. dev->capture_dma_data.pd.max_burst = 16;
  477. dev->play_dma_data.pd.addr_width = bus_widths[idx];
  478. dev->capture_dma_data.pd.addr_width = bus_widths[idx];
  479. dev->play_dma_data.pd.filter = pdata->filter;
  480. dev->capture_dma_data.pd.filter = pdata->filter;
  481. return 0;
  482. }
  483. static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
  484. struct snd_soc_dai_driver *dw_i2s_dai,
  485. struct resource *res)
  486. {
  487. u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
  488. u32 comp2 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_2);
  489. u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
  490. u32 idx = COMP1_APB_DATA_WIDTH(comp1);
  491. u32 idx2;
  492. int ret;
  493. if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
  494. return -EINVAL;
  495. ret = dw_configure_dai(dev, dw_i2s_dai, SNDRV_PCM_RATE_8000_192000);
  496. if (ret < 0)
  497. return ret;
  498. if (COMP1_TX_ENABLED(comp1)) {
  499. idx2 = COMP1_TX_WORDSIZE_0(comp1);
  500. dev->capability |= DWC_I2S_PLAY;
  501. dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
  502. dev->play_dma_data.dt.addr_width = bus_widths[idx];
  503. dev->play_dma_data.dt.fifo_size = fifo_depth *
  504. (fifo_width[idx2]) >> 8;
  505. dev->play_dma_data.dt.maxburst = 16;
  506. }
  507. if (COMP1_RX_ENABLED(comp1)) {
  508. idx2 = COMP2_RX_WORDSIZE_0(comp2);
  509. dev->capability |= DWC_I2S_RECORD;
  510. dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
  511. dev->capture_dma_data.dt.addr_width = bus_widths[idx];
  512. dev->capture_dma_data.dt.fifo_size = fifo_depth *
  513. (fifo_width[idx2] >> 8);
  514. dev->capture_dma_data.dt.maxburst = 16;
  515. }
  516. return 0;
  517. }
  518. static int dw_i2s_probe(struct platform_device *pdev)
  519. {
  520. const struct i2s_platform_data *pdata = pdev->dev.platform_data;
  521. struct dw_i2s_dev *dev;
  522. struct resource *res;
  523. int ret, irq;
  524. struct snd_soc_dai_driver *dw_i2s_dai;
  525. const char *clk_id;
  526. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  527. if (!dev)
  528. return -ENOMEM;
  529. dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
  530. if (!dw_i2s_dai)
  531. return -ENOMEM;
  532. dw_i2s_dai->ops = &dw_i2s_dai_ops;
  533. dw_i2s_dai->suspend = dw_i2s_suspend;
  534. dw_i2s_dai->resume = dw_i2s_resume;
  535. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  536. dev->i2s_base = devm_ioremap_resource(&pdev->dev, res);
  537. if (IS_ERR(dev->i2s_base))
  538. return PTR_ERR(dev->i2s_base);
  539. dev->dev = &pdev->dev;
  540. irq = platform_get_irq(pdev, 0);
  541. if (irq >= 0) {
  542. ret = devm_request_irq(&pdev->dev, irq, i2s_irq_handler, 0,
  543. pdev->name, dev);
  544. if (ret < 0) {
  545. dev_err(&pdev->dev, "failed to request irq\n");
  546. return ret;
  547. }
  548. }
  549. dev->i2s_reg_comp1 = I2S_COMP_PARAM_1;
  550. dev->i2s_reg_comp2 = I2S_COMP_PARAM_2;
  551. if (pdata) {
  552. dev->capability = pdata->cap;
  553. clk_id = NULL;
  554. dev->quirks = pdata->quirks;
  555. if (dev->quirks & DW_I2S_QUIRK_COMP_REG_OFFSET) {
  556. dev->i2s_reg_comp1 = pdata->i2s_reg_comp1;
  557. dev->i2s_reg_comp2 = pdata->i2s_reg_comp2;
  558. }
  559. ret = dw_configure_dai_by_pd(dev, dw_i2s_dai, res, pdata);
  560. } else {
  561. clk_id = "i2sclk";
  562. ret = dw_configure_dai_by_dt(dev, dw_i2s_dai, res);
  563. }
  564. if (ret < 0)
  565. return ret;
  566. if (dev->capability & DW_I2S_MASTER) {
  567. if (pdata) {
  568. dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
  569. if (!dev->i2s_clk_cfg) {
  570. dev_err(&pdev->dev, "no clock configure method\n");
  571. return -ENODEV;
  572. }
  573. }
  574. dev->clk = devm_clk_get(&pdev->dev, clk_id);
  575. if (IS_ERR(dev->clk))
  576. return PTR_ERR(dev->clk);
  577. ret = clk_prepare_enable(dev->clk);
  578. if (ret < 0)
  579. return ret;
  580. }
  581. dev_set_drvdata(&pdev->dev, dev);
  582. ret = devm_snd_soc_register_component(&pdev->dev, &dw_i2s_component,
  583. dw_i2s_dai, 1);
  584. if (ret != 0) {
  585. dev_err(&pdev->dev, "not able to register dai\n");
  586. goto err_clk_disable;
  587. }
  588. if (!pdata) {
  589. if (irq >= 0) {
  590. ret = dw_pcm_register(pdev);
  591. dev->use_pio = true;
  592. } else {
  593. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL,
  594. 0);
  595. dev->use_pio = false;
  596. }
  597. if (ret) {
  598. dev_err(&pdev->dev, "could not register pcm: %d\n",
  599. ret);
  600. goto err_clk_disable;
  601. }
  602. }
  603. pm_runtime_enable(&pdev->dev);
  604. return 0;
  605. err_clk_disable:
  606. if (dev->capability & DW_I2S_MASTER)
  607. clk_disable_unprepare(dev->clk);
  608. return ret;
  609. }
  610. static int dw_i2s_remove(struct platform_device *pdev)
  611. {
  612. struct dw_i2s_dev *dev = dev_get_drvdata(&pdev->dev);
  613. if (dev->capability & DW_I2S_MASTER)
  614. clk_disable_unprepare(dev->clk);
  615. pm_runtime_disable(&pdev->dev);
  616. return 0;
  617. }
  618. #ifdef CONFIG_OF
  619. static const struct of_device_id dw_i2s_of_match[] = {
  620. { .compatible = "snps,designware-i2s", },
  621. {},
  622. };
  623. MODULE_DEVICE_TABLE(of, dw_i2s_of_match);
  624. #endif
  625. static const struct dev_pm_ops dwc_pm_ops = {
  626. SET_RUNTIME_PM_OPS(dw_i2s_runtime_suspend, dw_i2s_runtime_resume, NULL)
  627. };
  628. static struct platform_driver dw_i2s_driver = {
  629. .probe = dw_i2s_probe,
  630. .remove = dw_i2s_remove,
  631. .driver = {
  632. .name = "designware-i2s",
  633. .of_match_table = of_match_ptr(dw_i2s_of_match),
  634. .pm = &dwc_pm_ops,
  635. },
  636. };
  637. module_platform_driver(dw_i2s_driver);
  638. MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
  639. MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
  640. MODULE_LICENSE("GPL");
  641. MODULE_ALIAS("platform:designware_i2s");