img-i2s-in.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * IMG I2S input controller driver
  3. *
  4. * Copyright (C) 2015 Imagination Technologies Ltd.
  5. *
  6. * Author: Damien Horsley <Damien.Horsley@imgtec.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reset.h>
  19. #include <sound/core.h>
  20. #include <sound/dmaengine_pcm.h>
  21. #include <sound/initval.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #define IMG_I2S_IN_RX_FIFO 0x0
  26. #define IMG_I2S_IN_CTL 0x4
  27. #define IMG_I2S_IN_CTL_ACTIVE_CHAN_MASK 0xfffffffc
  28. #define IMG_I2S_IN_CTL_ACTIVE_CH_SHIFT 2
  29. #define IMG_I2S_IN_CTL_16PACK_MASK BIT(1)
  30. #define IMG_I2S_IN_CTL_ME_MASK BIT(0)
  31. #define IMG_I2S_IN_CH_CTL 0x4
  32. #define IMG_I2S_IN_CH_CTL_CCDEL_MASK 0x38000
  33. #define IMG_I2S_IN_CH_CTL_CCDEL_SHIFT 15
  34. #define IMG_I2S_IN_CH_CTL_FEN_MASK BIT(14)
  35. #define IMG_I2S_IN_CH_CTL_FMODE_MASK BIT(13)
  36. #define IMG_I2S_IN_CH_CTL_16PACK_MASK BIT(12)
  37. #define IMG_I2S_IN_CH_CTL_JUST_MASK BIT(10)
  38. #define IMG_I2S_IN_CH_CTL_PACKH_MASK BIT(9)
  39. #define IMG_I2S_IN_CH_CTL_CLK_TRANS_MASK BIT(8)
  40. #define IMG_I2S_IN_CH_CTL_BLKP_MASK BIT(7)
  41. #define IMG_I2S_IN_CH_CTL_FIFO_FLUSH_MASK BIT(6)
  42. #define IMG_I2S_IN_CH_CTL_LRD_MASK BIT(3)
  43. #define IMG_I2S_IN_CH_CTL_FW_MASK BIT(2)
  44. #define IMG_I2S_IN_CH_CTL_SW_MASK BIT(1)
  45. #define IMG_I2S_IN_CH_CTL_ME_MASK BIT(0)
  46. #define IMG_I2S_IN_CH_STRIDE 0x20
  47. struct img_i2s_in {
  48. void __iomem *base;
  49. struct clk *clk_sys;
  50. struct snd_dmaengine_dai_dma_data dma_data;
  51. struct device *dev;
  52. unsigned int max_i2s_chan;
  53. void __iomem *channel_base;
  54. unsigned int active_channels;
  55. struct snd_soc_dai_driver dai_driver;
  56. };
  57. static inline void img_i2s_in_writel(struct img_i2s_in *i2s, u32 val, u32 reg)
  58. {
  59. writel(val, i2s->base + reg);
  60. }
  61. static inline u32 img_i2s_in_readl(struct img_i2s_in *i2s, u32 reg)
  62. {
  63. return readl(i2s->base + reg);
  64. }
  65. static inline void img_i2s_in_ch_writel(struct img_i2s_in *i2s, u32 chan,
  66. u32 val, u32 reg)
  67. {
  68. writel(val, i2s->channel_base + (chan * IMG_I2S_IN_CH_STRIDE) + reg);
  69. }
  70. static inline u32 img_i2s_in_ch_readl(struct img_i2s_in *i2s, u32 chan,
  71. u32 reg)
  72. {
  73. return readl(i2s->channel_base + (chan * IMG_I2S_IN_CH_STRIDE) + reg);
  74. }
  75. static inline void img_i2s_in_ch_disable(struct img_i2s_in *i2s, u32 chan)
  76. {
  77. u32 reg;
  78. reg = img_i2s_in_ch_readl(i2s, chan, IMG_I2S_IN_CH_CTL);
  79. reg &= ~IMG_I2S_IN_CH_CTL_ME_MASK;
  80. img_i2s_in_ch_writel(i2s, chan, reg, IMG_I2S_IN_CH_CTL);
  81. }
  82. static inline void img_i2s_in_ch_enable(struct img_i2s_in *i2s, u32 chan)
  83. {
  84. u32 reg;
  85. reg = img_i2s_in_ch_readl(i2s, chan, IMG_I2S_IN_CH_CTL);
  86. reg |= IMG_I2S_IN_CH_CTL_ME_MASK;
  87. img_i2s_in_ch_writel(i2s, chan, reg, IMG_I2S_IN_CH_CTL);
  88. }
  89. static inline void img_i2s_in_disable(struct img_i2s_in *i2s)
  90. {
  91. u32 reg;
  92. reg = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
  93. reg &= ~IMG_I2S_IN_CTL_ME_MASK;
  94. img_i2s_in_writel(i2s, reg, IMG_I2S_IN_CTL);
  95. }
  96. static inline void img_i2s_in_enable(struct img_i2s_in *i2s)
  97. {
  98. u32 reg;
  99. reg = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
  100. reg |= IMG_I2S_IN_CTL_ME_MASK;
  101. img_i2s_in_writel(i2s, reg, IMG_I2S_IN_CTL);
  102. }
  103. static inline void img_i2s_in_flush(struct img_i2s_in *i2s)
  104. {
  105. int i;
  106. u32 reg;
  107. for (i = 0; i < i2s->active_channels; i++) {
  108. reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
  109. reg |= IMG_I2S_IN_CH_CTL_FIFO_FLUSH_MASK;
  110. img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
  111. reg &= ~IMG_I2S_IN_CH_CTL_FIFO_FLUSH_MASK;
  112. img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
  113. }
  114. }
  115. static int img_i2s_in_trigger(struct snd_pcm_substream *substream, int cmd,
  116. struct snd_soc_dai *dai)
  117. {
  118. struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
  119. switch (cmd) {
  120. case SNDRV_PCM_TRIGGER_START:
  121. case SNDRV_PCM_TRIGGER_RESUME:
  122. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  123. img_i2s_in_enable(i2s);
  124. break;
  125. case SNDRV_PCM_TRIGGER_STOP:
  126. case SNDRV_PCM_TRIGGER_SUSPEND:
  127. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  128. img_i2s_in_disable(i2s);
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. return 0;
  134. }
  135. static int img_i2s_in_check_rate(struct img_i2s_in *i2s,
  136. unsigned int sample_rate, unsigned int frame_size,
  137. unsigned int *bclk_filter_enable,
  138. unsigned int *bclk_filter_value)
  139. {
  140. unsigned int bclk_freq, cur_freq;
  141. bclk_freq = sample_rate * frame_size;
  142. cur_freq = clk_get_rate(i2s->clk_sys);
  143. if (cur_freq >= bclk_freq * 8) {
  144. *bclk_filter_enable = 1;
  145. *bclk_filter_value = 0;
  146. } else if (cur_freq >= bclk_freq * 7) {
  147. *bclk_filter_enable = 1;
  148. *bclk_filter_value = 1;
  149. } else if (cur_freq >= bclk_freq * 6) {
  150. *bclk_filter_enable = 0;
  151. *bclk_filter_value = 0;
  152. } else {
  153. dev_err(i2s->dev,
  154. "Sys clock rate %u insufficient for sample rate %u\n",
  155. cur_freq, sample_rate);
  156. return -EINVAL;
  157. }
  158. return 0;
  159. }
  160. static int img_i2s_in_hw_params(struct snd_pcm_substream *substream,
  161. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  162. {
  163. struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
  164. unsigned int rate, channels, i2s_channels, frame_size;
  165. unsigned int bclk_filter_enable, bclk_filter_value;
  166. int i, ret = 0;
  167. u32 reg, control_mask, chan_control_mask;
  168. u32 control_set = 0, chan_control_set = 0;
  169. snd_pcm_format_t format;
  170. rate = params_rate(params);
  171. format = params_format(params);
  172. channels = params_channels(params);
  173. i2s_channels = channels / 2;
  174. switch (format) {
  175. case SNDRV_PCM_FORMAT_S32_LE:
  176. frame_size = 64;
  177. chan_control_set |= IMG_I2S_IN_CH_CTL_SW_MASK;
  178. chan_control_set |= IMG_I2S_IN_CH_CTL_FW_MASK;
  179. chan_control_set |= IMG_I2S_IN_CH_CTL_PACKH_MASK;
  180. break;
  181. case SNDRV_PCM_FORMAT_S24_LE:
  182. frame_size = 64;
  183. chan_control_set |= IMG_I2S_IN_CH_CTL_SW_MASK;
  184. chan_control_set |= IMG_I2S_IN_CH_CTL_FW_MASK;
  185. break;
  186. case SNDRV_PCM_FORMAT_S16_LE:
  187. frame_size = 32;
  188. control_set |= IMG_I2S_IN_CTL_16PACK_MASK;
  189. chan_control_set |= IMG_I2S_IN_CH_CTL_16PACK_MASK;
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. if ((channels < 2) ||
  195. (channels > (i2s->max_i2s_chan * 2)) ||
  196. (channels % 2))
  197. return -EINVAL;
  198. control_set |= ((i2s_channels - 1) << IMG_I2S_IN_CTL_ACTIVE_CH_SHIFT);
  199. ret = img_i2s_in_check_rate(i2s, rate, frame_size,
  200. &bclk_filter_enable, &bclk_filter_value);
  201. if (ret < 0)
  202. return ret;
  203. if (bclk_filter_enable)
  204. chan_control_set |= IMG_I2S_IN_CH_CTL_FEN_MASK;
  205. if (bclk_filter_value)
  206. chan_control_set |= IMG_I2S_IN_CH_CTL_FMODE_MASK;
  207. control_mask = IMG_I2S_IN_CTL_16PACK_MASK |
  208. IMG_I2S_IN_CTL_ACTIVE_CHAN_MASK;
  209. chan_control_mask = IMG_I2S_IN_CH_CTL_16PACK_MASK |
  210. IMG_I2S_IN_CH_CTL_FEN_MASK |
  211. IMG_I2S_IN_CH_CTL_FMODE_MASK |
  212. IMG_I2S_IN_CH_CTL_SW_MASK |
  213. IMG_I2S_IN_CH_CTL_FW_MASK |
  214. IMG_I2S_IN_CH_CTL_PACKH_MASK;
  215. reg = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
  216. reg = (reg & ~control_mask) | control_set;
  217. img_i2s_in_writel(i2s, reg, IMG_I2S_IN_CTL);
  218. for (i = 0; i < i2s->active_channels; i++)
  219. img_i2s_in_ch_disable(i2s, i);
  220. for (i = 0; i < i2s->max_i2s_chan; i++) {
  221. reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
  222. reg = (reg & ~chan_control_mask) | chan_control_set;
  223. img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
  224. }
  225. i2s->active_channels = i2s_channels;
  226. img_i2s_in_flush(i2s);
  227. for (i = 0; i < i2s->active_channels; i++)
  228. img_i2s_in_ch_enable(i2s, i);
  229. return 0;
  230. }
  231. static int img_i2s_in_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  232. {
  233. struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
  234. int i;
  235. u32 chan_control_mask, lrd_set = 0, blkp_set = 0, chan_control_set = 0;
  236. u32 reg;
  237. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  238. case SND_SOC_DAIFMT_NB_NF:
  239. lrd_set |= IMG_I2S_IN_CH_CTL_LRD_MASK;
  240. break;
  241. case SND_SOC_DAIFMT_NB_IF:
  242. break;
  243. case SND_SOC_DAIFMT_IB_NF:
  244. lrd_set |= IMG_I2S_IN_CH_CTL_LRD_MASK;
  245. blkp_set |= IMG_I2S_IN_CH_CTL_BLKP_MASK;
  246. break;
  247. case SND_SOC_DAIFMT_IB_IF:
  248. blkp_set |= IMG_I2S_IN_CH_CTL_BLKP_MASK;
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  254. case SND_SOC_DAIFMT_I2S:
  255. chan_control_set |= IMG_I2S_IN_CH_CTL_CLK_TRANS_MASK;
  256. break;
  257. case SND_SOC_DAIFMT_LEFT_J:
  258. break;
  259. default:
  260. return -EINVAL;
  261. }
  262. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  263. case SND_SOC_DAIFMT_CBM_CFM:
  264. break;
  265. default:
  266. return -EINVAL;
  267. }
  268. chan_control_mask = IMG_I2S_IN_CH_CTL_CLK_TRANS_MASK;
  269. for (i = 0; i < i2s->active_channels; i++)
  270. img_i2s_in_ch_disable(i2s, i);
  271. /*
  272. * BLKP and LRD must be set during separate register writes
  273. */
  274. for (i = 0; i < i2s->max_i2s_chan; i++) {
  275. reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
  276. reg = (reg & ~chan_control_mask) | chan_control_set;
  277. img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
  278. reg = (reg & ~IMG_I2S_IN_CH_CTL_BLKP_MASK) | blkp_set;
  279. img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
  280. reg = (reg & ~IMG_I2S_IN_CH_CTL_LRD_MASK) | lrd_set;
  281. img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
  282. }
  283. for (i = 0; i < i2s->active_channels; i++)
  284. img_i2s_in_ch_enable(i2s, i);
  285. return 0;
  286. }
  287. static const struct snd_soc_dai_ops img_i2s_in_dai_ops = {
  288. .trigger = img_i2s_in_trigger,
  289. .hw_params = img_i2s_in_hw_params,
  290. .set_fmt = img_i2s_in_set_fmt
  291. };
  292. static int img_i2s_in_dai_probe(struct snd_soc_dai *dai)
  293. {
  294. struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
  295. snd_soc_dai_init_dma_data(dai, NULL, &i2s->dma_data);
  296. return 0;
  297. }
  298. static const struct snd_soc_component_driver img_i2s_in_component = {
  299. .name = "img-i2s-in"
  300. };
  301. static int img_i2s_in_dma_prepare_slave_config(struct snd_pcm_substream *st,
  302. struct snd_pcm_hw_params *params, struct dma_slave_config *sc)
  303. {
  304. unsigned int i2s_channels = params_channels(params) / 2;
  305. struct snd_soc_pcm_runtime *rtd = st->private_data;
  306. struct snd_dmaengine_dai_dma_data *dma_data;
  307. int ret;
  308. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, st);
  309. ret = snd_hwparams_to_dma_slave_config(st, params, sc);
  310. if (ret)
  311. return ret;
  312. sc->src_addr = dma_data->addr;
  313. sc->src_addr_width = dma_data->addr_width;
  314. sc->src_maxburst = 4 * i2s_channels;
  315. return 0;
  316. }
  317. static const struct snd_dmaengine_pcm_config img_i2s_in_dma_config = {
  318. .prepare_slave_config = img_i2s_in_dma_prepare_slave_config
  319. };
  320. static int img_i2s_in_probe(struct platform_device *pdev)
  321. {
  322. struct img_i2s_in *i2s;
  323. struct resource *res;
  324. void __iomem *base;
  325. int ret, i;
  326. struct reset_control *rst;
  327. unsigned int max_i2s_chan_pow_2;
  328. struct device *dev = &pdev->dev;
  329. i2s = devm_kzalloc(dev, sizeof(*i2s), GFP_KERNEL);
  330. if (!i2s)
  331. return -ENOMEM;
  332. platform_set_drvdata(pdev, i2s);
  333. i2s->dev = dev;
  334. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  335. base = devm_ioremap_resource(dev, res);
  336. if (IS_ERR(base))
  337. return PTR_ERR(base);
  338. i2s->base = base;
  339. if (of_property_read_u32(pdev->dev.of_node, "img,i2s-channels",
  340. &i2s->max_i2s_chan)) {
  341. dev_err(dev, "No img,i2s-channels property\n");
  342. return -EINVAL;
  343. }
  344. max_i2s_chan_pow_2 = 1 << get_count_order(i2s->max_i2s_chan);
  345. i2s->channel_base = base + (max_i2s_chan_pow_2 * 0x20);
  346. i2s->clk_sys = devm_clk_get(dev, "sys");
  347. if (IS_ERR(i2s->clk_sys)) {
  348. if (PTR_ERR(i2s->clk_sys) != -EPROBE_DEFER)
  349. dev_err(dev, "Failed to acquire clock 'sys'\n");
  350. return PTR_ERR(i2s->clk_sys);
  351. }
  352. ret = clk_prepare_enable(i2s->clk_sys);
  353. if (ret)
  354. return ret;
  355. i2s->active_channels = 1;
  356. i2s->dma_data.addr = res->start + IMG_I2S_IN_RX_FIFO;
  357. i2s->dma_data.addr_width = 4;
  358. i2s->dai_driver.probe = img_i2s_in_dai_probe;
  359. i2s->dai_driver.capture.channels_min = 2;
  360. i2s->dai_driver.capture.channels_max = i2s->max_i2s_chan * 2;
  361. i2s->dai_driver.capture.rates = SNDRV_PCM_RATE_8000_192000;
  362. i2s->dai_driver.capture.formats = SNDRV_PCM_FMTBIT_S32_LE |
  363. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE;
  364. i2s->dai_driver.ops = &img_i2s_in_dai_ops;
  365. rst = devm_reset_control_get(dev, "rst");
  366. if (IS_ERR(rst)) {
  367. if (PTR_ERR(rst) == -EPROBE_DEFER) {
  368. ret = -EPROBE_DEFER;
  369. goto err_clk_disable;
  370. }
  371. dev_dbg(dev, "No top level reset found\n");
  372. img_i2s_in_disable(i2s);
  373. for (i = 0; i < i2s->max_i2s_chan; i++)
  374. img_i2s_in_ch_disable(i2s, i);
  375. } else {
  376. reset_control_assert(rst);
  377. reset_control_deassert(rst);
  378. }
  379. img_i2s_in_writel(i2s, 0, IMG_I2S_IN_CTL);
  380. for (i = 0; i < i2s->max_i2s_chan; i++)
  381. img_i2s_in_ch_writel(i2s, i,
  382. (4 << IMG_I2S_IN_CH_CTL_CCDEL_SHIFT) |
  383. IMG_I2S_IN_CH_CTL_JUST_MASK |
  384. IMG_I2S_IN_CH_CTL_FW_MASK, IMG_I2S_IN_CH_CTL);
  385. ret = devm_snd_soc_register_component(dev, &img_i2s_in_component,
  386. &i2s->dai_driver, 1);
  387. if (ret)
  388. goto err_clk_disable;
  389. ret = devm_snd_dmaengine_pcm_register(dev, &img_i2s_in_dma_config, 0);
  390. if (ret)
  391. goto err_clk_disable;
  392. return 0;
  393. err_clk_disable:
  394. clk_disable_unprepare(i2s->clk_sys);
  395. return ret;
  396. }
  397. static int img_i2s_in_dev_remove(struct platform_device *pdev)
  398. {
  399. struct img_i2s_in *i2s = platform_get_drvdata(pdev);
  400. clk_disable_unprepare(i2s->clk_sys);
  401. return 0;
  402. }
  403. static const struct of_device_id img_i2s_in_of_match[] = {
  404. { .compatible = "img,i2s-in" },
  405. {}
  406. };
  407. MODULE_DEVICE_TABLE(of, img_i2s_in_of_match);
  408. static struct platform_driver img_i2s_in_driver = {
  409. .driver = {
  410. .name = "img-i2s-in",
  411. .of_match_table = img_i2s_in_of_match
  412. },
  413. .probe = img_i2s_in_probe,
  414. .remove = img_i2s_in_dev_remove
  415. };
  416. module_platform_driver(img_i2s_in_driver);
  417. MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
  418. MODULE_DESCRIPTION("IMG I2S Input Driver");
  419. MODULE_LICENSE("GPL v2");