jz4740-i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * You should have received a copy of the GNU General Public License along
  10. * with this program; if not, write to the Free Software Foundation, Inc.,
  11. * 675 Mass Ave, Cambridge, MA 02139, USA.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include "jz4740-i2s.h"
  29. #include "jz4740-pcm.h"
  30. #define JZ_REG_AIC_CONF 0x00
  31. #define JZ_REG_AIC_CTRL 0x04
  32. #define JZ_REG_AIC_I2S_FMT 0x10
  33. #define JZ_REG_AIC_FIFO_STATUS 0x14
  34. #define JZ_REG_AIC_I2S_STATUS 0x1c
  35. #define JZ_REG_AIC_CLK_DIV 0x30
  36. #define JZ_REG_AIC_FIFO 0x34
  37. #define JZ_AIC_CONF_FIFO_RX_THRESHOLD_MASK (0xf << 12)
  38. #define JZ_AIC_CONF_FIFO_TX_THRESHOLD_MASK (0xf << 8)
  39. #define JZ_AIC_CONF_OVERFLOW_PLAY_LAST BIT(6)
  40. #define JZ_AIC_CONF_INTERNAL_CODEC BIT(5)
  41. #define JZ_AIC_CONF_I2S BIT(4)
  42. #define JZ_AIC_CONF_RESET BIT(3)
  43. #define JZ_AIC_CONF_BIT_CLK_MASTER BIT(2)
  44. #define JZ_AIC_CONF_SYNC_CLK_MASTER BIT(1)
  45. #define JZ_AIC_CONF_ENABLE BIT(0)
  46. #define JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET 12
  47. #define JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET 8
  48. #define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK (0x7 << 19)
  49. #define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK (0x7 << 16)
  50. #define JZ_AIC_CTRL_ENABLE_RX_DMA BIT(15)
  51. #define JZ_AIC_CTRL_ENABLE_TX_DMA BIT(14)
  52. #define JZ_AIC_CTRL_MONO_TO_STEREO BIT(11)
  53. #define JZ_AIC_CTRL_SWITCH_ENDIANNESS BIT(10)
  54. #define JZ_AIC_CTRL_SIGNED_TO_UNSIGNED BIT(9)
  55. #define JZ_AIC_CTRL_FLUSH BIT(8)
  56. #define JZ_AIC_CTRL_ENABLE_ROR_INT BIT(6)
  57. #define JZ_AIC_CTRL_ENABLE_TUR_INT BIT(5)
  58. #define JZ_AIC_CTRL_ENABLE_RFS_INT BIT(4)
  59. #define JZ_AIC_CTRL_ENABLE_TFS_INT BIT(3)
  60. #define JZ_AIC_CTRL_ENABLE_LOOPBACK BIT(2)
  61. #define JZ_AIC_CTRL_ENABLE_PLAYBACK BIT(1)
  62. #define JZ_AIC_CTRL_ENABLE_CAPTURE BIT(0)
  63. #define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET 19
  64. #define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET 16
  65. #define JZ_AIC_I2S_FMT_DISABLE_BIT_CLK BIT(12)
  66. #define JZ_AIC_I2S_FMT_ENABLE_SYS_CLK BIT(4)
  67. #define JZ_AIC_I2S_FMT_MSB BIT(0)
  68. #define JZ_AIC_I2S_STATUS_BUSY BIT(2)
  69. #define JZ_AIC_CLK_DIV_MASK 0xf
  70. struct jz4740_i2s {
  71. struct resource *mem;
  72. void __iomem *base;
  73. dma_addr_t phys_base;
  74. struct clk *clk_aic;
  75. struct clk *clk_i2s;
  76. struct jz4740_pcm_config pcm_config_playback;
  77. struct jz4740_pcm_config pcm_config_capture;
  78. };
  79. static inline uint32_t jz4740_i2s_read(const struct jz4740_i2s *i2s,
  80. unsigned int reg)
  81. {
  82. return readl(i2s->base + reg);
  83. }
  84. static inline void jz4740_i2s_write(const struct jz4740_i2s *i2s,
  85. unsigned int reg, uint32_t value)
  86. {
  87. writel(value, i2s->base + reg);
  88. }
  89. static int jz4740_i2s_startup(struct snd_pcm_substream *substream,
  90. struct snd_soc_dai *dai)
  91. {
  92. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  93. uint32_t conf, ctrl;
  94. if (dai->active)
  95. return 0;
  96. ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
  97. ctrl |= JZ_AIC_CTRL_FLUSH;
  98. jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
  99. clk_enable(i2s->clk_i2s);
  100. conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
  101. conf |= JZ_AIC_CONF_ENABLE;
  102. jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
  103. return 0;
  104. }
  105. static void jz4740_i2s_shutdown(struct snd_pcm_substream *substream,
  106. struct snd_soc_dai *dai)
  107. {
  108. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  109. uint32_t conf;
  110. if (dai->active)
  111. return;
  112. conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
  113. conf &= ~JZ_AIC_CONF_ENABLE;
  114. jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
  115. clk_disable(i2s->clk_i2s);
  116. }
  117. static int jz4740_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  118. struct snd_soc_dai *dai)
  119. {
  120. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  121. uint32_t ctrl;
  122. uint32_t mask;
  123. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  124. mask = JZ_AIC_CTRL_ENABLE_PLAYBACK | JZ_AIC_CTRL_ENABLE_TX_DMA;
  125. else
  126. mask = JZ_AIC_CTRL_ENABLE_CAPTURE | JZ_AIC_CTRL_ENABLE_RX_DMA;
  127. ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
  128. switch (cmd) {
  129. case SNDRV_PCM_TRIGGER_START:
  130. case SNDRV_PCM_TRIGGER_RESUME:
  131. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  132. ctrl |= mask;
  133. break;
  134. case SNDRV_PCM_TRIGGER_STOP:
  135. case SNDRV_PCM_TRIGGER_SUSPEND:
  136. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  137. ctrl &= ~mask;
  138. break;
  139. default:
  140. return -EINVAL;
  141. }
  142. jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
  143. return 0;
  144. }
  145. static int jz4740_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  146. {
  147. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  148. uint32_t format = 0;
  149. uint32_t conf;
  150. conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
  151. conf &= ~(JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER);
  152. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  153. case SND_SOC_DAIFMT_CBS_CFS:
  154. conf |= JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER;
  155. format |= JZ_AIC_I2S_FMT_ENABLE_SYS_CLK;
  156. break;
  157. case SND_SOC_DAIFMT_CBM_CFS:
  158. conf |= JZ_AIC_CONF_SYNC_CLK_MASTER;
  159. break;
  160. case SND_SOC_DAIFMT_CBS_CFM:
  161. conf |= JZ_AIC_CONF_BIT_CLK_MASTER;
  162. break;
  163. case SND_SOC_DAIFMT_CBM_CFM:
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  169. case SND_SOC_DAIFMT_MSB:
  170. format |= JZ_AIC_I2S_FMT_MSB;
  171. break;
  172. case SND_SOC_DAIFMT_I2S:
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  178. case SND_SOC_DAIFMT_NB_NF:
  179. break;
  180. default:
  181. return -EINVAL;
  182. }
  183. jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
  184. jz4740_i2s_write(i2s, JZ_REG_AIC_I2S_FMT, format);
  185. return 0;
  186. }
  187. static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
  188. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  189. {
  190. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  191. enum jz4740_dma_width dma_width;
  192. struct jz4740_pcm_config *pcm_config;
  193. unsigned int sample_size;
  194. uint32_t ctrl;
  195. ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
  196. switch (params_format(params)) {
  197. case SNDRV_PCM_FORMAT_S8:
  198. sample_size = 0;
  199. dma_width = JZ4740_DMA_WIDTH_8BIT;
  200. break;
  201. case SNDRV_PCM_FORMAT_S16:
  202. sample_size = 1;
  203. dma_width = JZ4740_DMA_WIDTH_16BIT;
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  209. ctrl &= ~JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK;
  210. ctrl |= sample_size << JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET;
  211. if (params_channels(params) == 1)
  212. ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO;
  213. else
  214. ctrl &= ~JZ_AIC_CTRL_MONO_TO_STEREO;
  215. pcm_config = &i2s->pcm_config_playback;
  216. pcm_config->dma_config.dst_width = dma_width;
  217. } else {
  218. ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK;
  219. ctrl |= sample_size << JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET;
  220. pcm_config = &i2s->pcm_config_capture;
  221. pcm_config->dma_config.src_width = dma_width;
  222. }
  223. jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
  224. snd_soc_dai_set_dma_data(dai, substream, pcm_config);
  225. return 0;
  226. }
  227. static int jz4740_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  228. unsigned int freq, int dir)
  229. {
  230. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  231. struct clk *parent;
  232. int ret = 0;
  233. switch (clk_id) {
  234. case JZ4740_I2S_CLKSRC_EXT:
  235. parent = clk_get(NULL, "ext");
  236. clk_set_parent(i2s->clk_i2s, parent);
  237. break;
  238. case JZ4740_I2S_CLKSRC_PLL:
  239. parent = clk_get(NULL, "pll half");
  240. clk_set_parent(i2s->clk_i2s, parent);
  241. ret = clk_set_rate(i2s->clk_i2s, freq);
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. clk_put(parent);
  247. return ret;
  248. }
  249. static int jz4740_i2s_suspend(struct snd_soc_dai *dai)
  250. {
  251. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  252. uint32_t conf;
  253. if (dai->active) {
  254. conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
  255. conf &= ~JZ_AIC_CONF_ENABLE;
  256. jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
  257. clk_disable(i2s->clk_i2s);
  258. }
  259. clk_disable(i2s->clk_aic);
  260. return 0;
  261. }
  262. static int jz4740_i2s_resume(struct snd_soc_dai *dai)
  263. {
  264. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  265. uint32_t conf;
  266. clk_enable(i2s->clk_aic);
  267. if (dai->active) {
  268. clk_enable(i2s->clk_i2s);
  269. conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
  270. conf |= JZ_AIC_CONF_ENABLE;
  271. jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
  272. }
  273. return 0;
  274. }
  275. static void jz4740_i2c_init_pcm_config(struct jz4740_i2s *i2s)
  276. {
  277. struct jz4740_dma_config *dma_config;
  278. /* Playback */
  279. dma_config = &i2s->pcm_config_playback.dma_config;
  280. dma_config->src_width = JZ4740_DMA_WIDTH_32BIT,
  281. dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
  282. dma_config->request_type = JZ4740_DMA_TYPE_AIC_TRANSMIT;
  283. dma_config->flags = JZ4740_DMA_SRC_AUTOINC;
  284. dma_config->mode = JZ4740_DMA_MODE_SINGLE;
  285. i2s->pcm_config_playback.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
  286. /* Capture */
  287. dma_config = &i2s->pcm_config_capture.dma_config;
  288. dma_config->dst_width = JZ4740_DMA_WIDTH_32BIT,
  289. dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
  290. dma_config->request_type = JZ4740_DMA_TYPE_AIC_RECEIVE;
  291. dma_config->flags = JZ4740_DMA_DST_AUTOINC;
  292. dma_config->mode = JZ4740_DMA_MODE_SINGLE;
  293. i2s->pcm_config_capture.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
  294. }
  295. static int jz4740_i2s_dai_probe(struct snd_soc_dai *dai)
  296. {
  297. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  298. uint32_t conf;
  299. clk_enable(i2s->clk_aic);
  300. jz4740_i2c_init_pcm_config(i2s);
  301. conf = (7 << JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET) |
  302. (8 << JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET) |
  303. JZ_AIC_CONF_OVERFLOW_PLAY_LAST |
  304. JZ_AIC_CONF_I2S |
  305. JZ_AIC_CONF_INTERNAL_CODEC;
  306. jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, JZ_AIC_CONF_RESET);
  307. jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
  308. return 0;
  309. }
  310. static int jz4740_i2s_dai_remove(struct snd_soc_dai *dai)
  311. {
  312. struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  313. clk_disable(i2s->clk_aic);
  314. return 0;
  315. }
  316. static struct snd_soc_dai_ops jz4740_i2s_dai_ops = {
  317. .startup = jz4740_i2s_startup,
  318. .shutdown = jz4740_i2s_shutdown,
  319. .trigger = jz4740_i2s_trigger,
  320. .hw_params = jz4740_i2s_hw_params,
  321. .set_fmt = jz4740_i2s_set_fmt,
  322. .set_sysclk = jz4740_i2s_set_sysclk,
  323. };
  324. #define JZ4740_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
  325. SNDRV_PCM_FMTBIT_S16_LE)
  326. static struct snd_soc_dai_driver jz4740_i2s_dai = {
  327. .probe = jz4740_i2s_dai_probe,
  328. .remove = jz4740_i2s_dai_remove,
  329. .playback = {
  330. .channels_min = 1,
  331. .channels_max = 2,
  332. .rates = SNDRV_PCM_RATE_8000_48000,
  333. .formats = JZ4740_I2S_FMTS,
  334. },
  335. .capture = {
  336. .channels_min = 2,
  337. .channels_max = 2,
  338. .rates = SNDRV_PCM_RATE_8000_48000,
  339. .formats = JZ4740_I2S_FMTS,
  340. },
  341. .symmetric_rates = 1,
  342. .ops = &jz4740_i2s_dai_ops,
  343. .suspend = jz4740_i2s_suspend,
  344. .resume = jz4740_i2s_resume,
  345. };
  346. static int __devinit jz4740_i2s_dev_probe(struct platform_device *pdev)
  347. {
  348. struct jz4740_i2s *i2s;
  349. int ret;
  350. i2s = kzalloc(sizeof(*i2s), GFP_KERNEL);
  351. if (!i2s)
  352. return -ENOMEM;
  353. i2s->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  354. if (!i2s->mem) {
  355. ret = -ENOENT;
  356. goto err_free;
  357. }
  358. i2s->mem = request_mem_region(i2s->mem->start, resource_size(i2s->mem),
  359. pdev->name);
  360. if (!i2s->mem) {
  361. ret = -EBUSY;
  362. goto err_free;
  363. }
  364. i2s->base = ioremap_nocache(i2s->mem->start, resource_size(i2s->mem));
  365. if (!i2s->base) {
  366. ret = -EBUSY;
  367. goto err_release_mem_region;
  368. }
  369. i2s->phys_base = i2s->mem->start;
  370. i2s->clk_aic = clk_get(&pdev->dev, "aic");
  371. if (IS_ERR(i2s->clk_aic)) {
  372. ret = PTR_ERR(i2s->clk_aic);
  373. goto err_iounmap;
  374. }
  375. i2s->clk_i2s = clk_get(&pdev->dev, "i2s");
  376. if (IS_ERR(i2s->clk_i2s)) {
  377. ret = PTR_ERR(i2s->clk_i2s);
  378. goto err_clk_put_aic;
  379. }
  380. platform_set_drvdata(pdev, i2s);
  381. ret = snd_soc_register_dai(&pdev->dev, &jz4740_i2s_dai);
  382. if (ret) {
  383. dev_err(&pdev->dev, "Failed to register DAI\n");
  384. goto err_clk_put_i2s;
  385. }
  386. return 0;
  387. err_clk_put_i2s:
  388. clk_put(i2s->clk_i2s);
  389. err_clk_put_aic:
  390. clk_put(i2s->clk_aic);
  391. err_iounmap:
  392. iounmap(i2s->base);
  393. err_release_mem_region:
  394. release_mem_region(i2s->mem->start, resource_size(i2s->mem));
  395. err_free:
  396. kfree(i2s);
  397. return ret;
  398. }
  399. static int __devexit jz4740_i2s_dev_remove(struct platform_device *pdev)
  400. {
  401. struct jz4740_i2s *i2s = platform_get_drvdata(pdev);
  402. snd_soc_unregister_dai(&pdev->dev);
  403. clk_put(i2s->clk_i2s);
  404. clk_put(i2s->clk_aic);
  405. iounmap(i2s->base);
  406. release_mem_region(i2s->mem->start, resource_size(i2s->mem));
  407. platform_set_drvdata(pdev, NULL);
  408. kfree(i2s);
  409. return 0;
  410. }
  411. static struct platform_driver jz4740_i2s_driver = {
  412. .probe = jz4740_i2s_dev_probe,
  413. .remove = __devexit_p(jz4740_i2s_dev_remove),
  414. .driver = {
  415. .name = "jz4740-i2s",
  416. .owner = THIS_MODULE,
  417. },
  418. };
  419. static int __init jz4740_i2s_init(void)
  420. {
  421. return platform_driver_register(&jz4740_i2s_driver);
  422. }
  423. module_init(jz4740_i2s_init);
  424. static void __exit jz4740_i2s_exit(void)
  425. {
  426. platform_driver_unregister(&jz4740_i2s_driver);
  427. }
  428. module_exit(jz4740_i2s_exit);
  429. MODULE_AUTHOR("Lars-Peter Clausen, <lars@metafoo.de>");
  430. MODULE_DESCRIPTION("Ingenic JZ4740 SoC I2S driver");
  431. MODULE_LICENSE("GPL");
  432. MODULE_ALIAS("platform:jz4740-i2s");