designware_i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. /* common register for all channel */
  25. #define IER 0x000
  26. #define IRER 0x004
  27. #define ITER 0x008
  28. #define CER 0x00C
  29. #define CCR 0x010
  30. #define RXFFR 0x014
  31. #define TXFFR 0x018
  32. /* I2STxRxRegisters for all channels */
  33. #define LRBR_LTHR(x) (0x40 * x + 0x020)
  34. #define RRBR_RTHR(x) (0x40 * x + 0x024)
  35. #define RER(x) (0x40 * x + 0x028)
  36. #define TER(x) (0x40 * x + 0x02C)
  37. #define RCR(x) (0x40 * x + 0x030)
  38. #define TCR(x) (0x40 * x + 0x034)
  39. #define ISR(x) (0x40 * x + 0x038)
  40. #define IMR(x) (0x40 * x + 0x03C)
  41. #define ROR(x) (0x40 * x + 0x040)
  42. #define TOR(x) (0x40 * x + 0x044)
  43. #define RFCR(x) (0x40 * x + 0x048)
  44. #define TFCR(x) (0x40 * x + 0x04C)
  45. #define RFF(x) (0x40 * x + 0x050)
  46. #define TFF(x) (0x40 * x + 0x054)
  47. /* I2SCOMPRegisters */
  48. #define I2S_COMP_PARAM_2 0x01F0
  49. #define I2S_COMP_PARAM_1 0x01F4
  50. #define I2S_COMP_VERSION 0x01F8
  51. #define I2S_COMP_TYPE 0x01FC
  52. #define MAX_CHANNEL_NUM 8
  53. #define MIN_CHANNEL_NUM 2
  54. struct dw_i2s_dev {
  55. void __iomem *i2s_base;
  56. struct clk *clk;
  57. int active;
  58. unsigned int capability;
  59. struct device *dev;
  60. /* data related to DMA transfers b/w i2s and DMAC */
  61. struct i2s_dma_data play_dma_data;
  62. struct i2s_dma_data capture_dma_data;
  63. struct i2s_clk_config_data config;
  64. int (*i2s_clk_cfg)(struct i2s_clk_config_data *config);
  65. };
  66. static inline void i2s_write_reg(void __iomem *io_base, int reg, u32 val)
  67. {
  68. writel(val, io_base + reg);
  69. }
  70. static inline u32 i2s_read_reg(void __iomem *io_base, int reg)
  71. {
  72. return readl(io_base + reg);
  73. }
  74. static inline void i2s_disable_channels(struct dw_i2s_dev *dev, u32 stream)
  75. {
  76. u32 i = 0;
  77. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  78. for (i = 0; i < 4; i++)
  79. i2s_write_reg(dev->i2s_base, TER(i), 0);
  80. } else {
  81. for (i = 0; i < 4; i++)
  82. i2s_write_reg(dev->i2s_base, RER(i), 0);
  83. }
  84. }
  85. static inline void i2s_clear_irqs(struct dw_i2s_dev *dev, u32 stream)
  86. {
  87. u32 i = 0;
  88. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  89. for (i = 0; i < 4; i++)
  90. i2s_write_reg(dev->i2s_base, TOR(i), 0);
  91. } else {
  92. for (i = 0; i < 4; i++)
  93. i2s_write_reg(dev->i2s_base, ROR(i), 0);
  94. }
  95. }
  96. static void i2s_start(struct dw_i2s_dev *dev,
  97. struct snd_pcm_substream *substream)
  98. {
  99. i2s_write_reg(dev->i2s_base, IER, 1);
  100. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  101. i2s_write_reg(dev->i2s_base, ITER, 1);
  102. else
  103. i2s_write_reg(dev->i2s_base, IRER, 1);
  104. i2s_write_reg(dev->i2s_base, CER, 1);
  105. }
  106. static void i2s_stop(struct dw_i2s_dev *dev,
  107. struct snd_pcm_substream *substream)
  108. {
  109. u32 i = 0, irq;
  110. i2s_clear_irqs(dev, substream->stream);
  111. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  112. i2s_write_reg(dev->i2s_base, ITER, 0);
  113. for (i = 0; i < 4; i++) {
  114. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  115. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x30);
  116. }
  117. } else {
  118. i2s_write_reg(dev->i2s_base, IRER, 0);
  119. for (i = 0; i < 4; i++) {
  120. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  121. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x03);
  122. }
  123. }
  124. if (!dev->active) {
  125. i2s_write_reg(dev->i2s_base, CER, 0);
  126. i2s_write_reg(dev->i2s_base, IER, 0);
  127. }
  128. }
  129. static int dw_i2s_startup(struct snd_pcm_substream *substream,
  130. struct snd_soc_dai *cpu_dai)
  131. {
  132. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  133. struct i2s_dma_data *dma_data = NULL;
  134. if (!(dev->capability & DWC_I2S_RECORD) &&
  135. (substream->stream == SNDRV_PCM_STREAM_CAPTURE))
  136. return -EINVAL;
  137. if (!(dev->capability & DWC_I2S_PLAY) &&
  138. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  139. return -EINVAL;
  140. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  141. dma_data = &dev->play_dma_data;
  142. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  143. dma_data = &dev->capture_dma_data;
  144. snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)dma_data);
  145. return 0;
  146. }
  147. static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
  148. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  149. {
  150. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  151. struct i2s_clk_config_data *config = &dev->config;
  152. u32 ccr, xfer_resolution, ch_reg, irq;
  153. int ret;
  154. switch (params_format(params)) {
  155. case SNDRV_PCM_FORMAT_S16_LE:
  156. config->data_width = 16;
  157. ccr = 0x00;
  158. xfer_resolution = 0x02;
  159. break;
  160. case SNDRV_PCM_FORMAT_S24_LE:
  161. config->data_width = 24;
  162. ccr = 0x08;
  163. xfer_resolution = 0x04;
  164. break;
  165. case SNDRV_PCM_FORMAT_S32_LE:
  166. config->data_width = 32;
  167. ccr = 0x10;
  168. xfer_resolution = 0x05;
  169. break;
  170. default:
  171. dev_err(dev->dev, "designware-i2s: unsuppted PCM fmt");
  172. return -EINVAL;
  173. }
  174. config->chan_nr = params_channels(params);
  175. switch (config->chan_nr) {
  176. case EIGHT_CHANNEL_SUPPORT:
  177. ch_reg = 3;
  178. break;
  179. case SIX_CHANNEL_SUPPORT:
  180. ch_reg = 2;
  181. break;
  182. case FOUR_CHANNEL_SUPPORT:
  183. ch_reg = 1;
  184. break;
  185. case TWO_CHANNEL_SUPPORT:
  186. ch_reg = 0;
  187. break;
  188. default:
  189. dev_err(dev->dev, "channel not supported\n");
  190. return -EINVAL;
  191. }
  192. i2s_disable_channels(dev, substream->stream);
  193. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  194. i2s_write_reg(dev->i2s_base, TCR(ch_reg), xfer_resolution);
  195. i2s_write_reg(dev->i2s_base, TFCR(ch_reg), 0x02);
  196. irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
  197. i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x30);
  198. i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
  199. } else {
  200. i2s_write_reg(dev->i2s_base, RCR(ch_reg), xfer_resolution);
  201. i2s_write_reg(dev->i2s_base, RFCR(ch_reg), 0x07);
  202. irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
  203. i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x03);
  204. i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
  205. }
  206. i2s_write_reg(dev->i2s_base, CCR, ccr);
  207. config->sample_rate = params_rate(params);
  208. if (!dev->i2s_clk_cfg)
  209. return -EINVAL;
  210. ret = dev->i2s_clk_cfg(config);
  211. if (ret < 0) {
  212. dev_err(dev->dev, "runtime audio clk config fail\n");
  213. return ret;
  214. }
  215. return 0;
  216. }
  217. static void dw_i2s_shutdown(struct snd_pcm_substream *substream,
  218. struct snd_soc_dai *dai)
  219. {
  220. snd_soc_dai_set_dma_data(dai, substream, NULL);
  221. }
  222. static int dw_i2s_prepare(struct snd_pcm_substream *substream,
  223. struct snd_soc_dai *dai)
  224. {
  225. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  226. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  227. i2s_write_reg(dev->i2s_base, TXFFR, 1);
  228. else
  229. i2s_write_reg(dev->i2s_base, RXFFR, 1);
  230. return 0;
  231. }
  232. static int dw_i2s_trigger(struct snd_pcm_substream *substream,
  233. int cmd, struct snd_soc_dai *dai)
  234. {
  235. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  236. int ret = 0;
  237. switch (cmd) {
  238. case SNDRV_PCM_TRIGGER_START:
  239. case SNDRV_PCM_TRIGGER_RESUME:
  240. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  241. dev->active++;
  242. i2s_start(dev, substream);
  243. break;
  244. case SNDRV_PCM_TRIGGER_STOP:
  245. case SNDRV_PCM_TRIGGER_SUSPEND:
  246. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  247. dev->active--;
  248. i2s_stop(dev, substream);
  249. break;
  250. default:
  251. ret = -EINVAL;
  252. break;
  253. }
  254. return ret;
  255. }
  256. static struct snd_soc_dai_ops dw_i2s_dai_ops = {
  257. .startup = dw_i2s_startup,
  258. .shutdown = dw_i2s_shutdown,
  259. .hw_params = dw_i2s_hw_params,
  260. .prepare = dw_i2s_prepare,
  261. .trigger = dw_i2s_trigger,
  262. };
  263. static const struct snd_soc_component_driver dw_i2s_component = {
  264. .name = "dw-i2s",
  265. };
  266. #ifdef CONFIG_PM
  267. static int dw_i2s_suspend(struct snd_soc_dai *dai)
  268. {
  269. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  270. clk_disable(dev->clk);
  271. return 0;
  272. }
  273. static int dw_i2s_resume(struct snd_soc_dai *dai)
  274. {
  275. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  276. clk_enable(dev->clk);
  277. return 0;
  278. }
  279. #else
  280. #define dw_i2s_suspend NULL
  281. #define dw_i2s_resume NULL
  282. #endif
  283. static int dw_i2s_probe(struct platform_device *pdev)
  284. {
  285. const struct i2s_platform_data *pdata = pdev->dev.platform_data;
  286. struct dw_i2s_dev *dev;
  287. struct resource *res;
  288. int ret;
  289. unsigned int cap;
  290. struct snd_soc_dai_driver *dw_i2s_dai;
  291. if (!pdata) {
  292. dev_err(&pdev->dev, "Invalid platform data\n");
  293. return -EINVAL;
  294. }
  295. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. if (!res) {
  297. dev_err(&pdev->dev, "no i2s resource defined\n");
  298. return -ENODEV;
  299. }
  300. if (!devm_request_mem_region(&pdev->dev, res->start,
  301. resource_size(res), pdev->name)) {
  302. dev_err(&pdev->dev, "i2s region already claimed\n");
  303. return -EBUSY;
  304. }
  305. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  306. if (!dev) {
  307. dev_warn(&pdev->dev, "kzalloc fail\n");
  308. return -ENOMEM;
  309. }
  310. dev->i2s_base = devm_ioremap(&pdev->dev, res->start,
  311. resource_size(res));
  312. if (!dev->i2s_base) {
  313. dev_err(&pdev->dev, "ioremap fail for i2s_region\n");
  314. return -ENOMEM;
  315. }
  316. cap = pdata->cap;
  317. dev->capability = cap;
  318. dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
  319. /* Set DMA slaves info */
  320. dev->play_dma_data.data = pdata->play_dma_data;
  321. dev->capture_dma_data.data = pdata->capture_dma_data;
  322. dev->play_dma_data.addr = res->start + I2S_TXDMA;
  323. dev->capture_dma_data.addr = res->start + I2S_RXDMA;
  324. dev->play_dma_data.max_burst = 16;
  325. dev->capture_dma_data.max_burst = 16;
  326. dev->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  327. dev->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  328. dev->play_dma_data.filter = pdata->filter;
  329. dev->capture_dma_data.filter = pdata->filter;
  330. dev->clk = clk_get(&pdev->dev, NULL);
  331. if (IS_ERR(dev->clk))
  332. return PTR_ERR(dev->clk);
  333. ret = clk_enable(dev->clk);
  334. if (ret < 0)
  335. goto err_clk_put;
  336. dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
  337. if (!dw_i2s_dai) {
  338. dev_err(&pdev->dev, "mem allocation failed for dai driver\n");
  339. ret = -ENOMEM;
  340. goto err_clk_disable;
  341. }
  342. if (cap & DWC_I2S_PLAY) {
  343. dev_dbg(&pdev->dev, " designware: play supported\n");
  344. dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
  345. dw_i2s_dai->playback.channels_max = pdata->channel;
  346. dw_i2s_dai->playback.formats = pdata->snd_fmts;
  347. dw_i2s_dai->playback.rates = pdata->snd_rates;
  348. }
  349. if (cap & DWC_I2S_RECORD) {
  350. dev_dbg(&pdev->dev, "designware: record supported\n");
  351. dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
  352. dw_i2s_dai->capture.channels_max = pdata->channel;
  353. dw_i2s_dai->capture.formats = pdata->snd_fmts;
  354. dw_i2s_dai->capture.rates = pdata->snd_rates;
  355. }
  356. dw_i2s_dai->ops = &dw_i2s_dai_ops;
  357. dw_i2s_dai->suspend = dw_i2s_suspend;
  358. dw_i2s_dai->resume = dw_i2s_resume;
  359. dev->dev = &pdev->dev;
  360. dev_set_drvdata(&pdev->dev, dev);
  361. ret = snd_soc_register_component(&pdev->dev, &dw_i2s_component,
  362. dw_i2s_dai, 1);
  363. if (ret != 0) {
  364. dev_err(&pdev->dev, "not able to register dai\n");
  365. goto err_clk_disable;
  366. }
  367. return 0;
  368. err_clk_disable:
  369. clk_disable(dev->clk);
  370. err_clk_put:
  371. clk_put(dev->clk);
  372. return ret;
  373. }
  374. static int dw_i2s_remove(struct platform_device *pdev)
  375. {
  376. struct dw_i2s_dev *dev = dev_get_drvdata(&pdev->dev);
  377. snd_soc_unregister_component(&pdev->dev);
  378. clk_put(dev->clk);
  379. return 0;
  380. }
  381. static struct platform_driver dw_i2s_driver = {
  382. .probe = dw_i2s_probe,
  383. .remove = dw_i2s_remove,
  384. .driver = {
  385. .name = "designware-i2s",
  386. .owner = THIS_MODULE,
  387. },
  388. };
  389. module_platform_driver(dw_i2s_driver);
  390. MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
  391. MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
  392. MODULE_LICENSE("GPL");
  393. MODULE_ALIAS("platform:designware_i2s");