designware_i2s.c 18 KB

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