img-parallel-out.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IMG parallel output controller driver
  4. *
  5. * Copyright (C) 2015 Imagination Technologies Ltd.
  6. *
  7. * Author: Damien Horsley <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/reset.h>
  17. #include <sound/core.h>
  18. #include <sound/dmaengine_pcm.h>
  19. #include <sound/initval.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #define IMG_PRL_OUT_TX_FIFO 0
  24. #define IMG_PRL_OUT_CTL 0x4
  25. #define IMG_PRL_OUT_CTL_CH_MASK BIT(4)
  26. #define IMG_PRL_OUT_CTL_PACKH_MASK BIT(3)
  27. #define IMG_PRL_OUT_CTL_EDGE_MASK BIT(2)
  28. #define IMG_PRL_OUT_CTL_ME_MASK BIT(1)
  29. #define IMG_PRL_OUT_CTL_SRST_MASK BIT(0)
  30. struct img_prl_out {
  31. void __iomem *base;
  32. struct clk *clk_sys;
  33. struct clk *clk_ref;
  34. struct snd_dmaengine_dai_dma_data dma_data;
  35. struct device *dev;
  36. struct reset_control *rst;
  37. };
  38. static int img_prl_out_suspend(struct device *dev)
  39. {
  40. struct img_prl_out *prl = dev_get_drvdata(dev);
  41. clk_disable_unprepare(prl->clk_ref);
  42. return 0;
  43. }
  44. static int img_prl_out_resume(struct device *dev)
  45. {
  46. struct img_prl_out *prl = dev_get_drvdata(dev);
  47. int ret;
  48. ret = clk_prepare_enable(prl->clk_ref);
  49. if (ret) {
  50. dev_err(dev, "clk_enable failed: %d\n", ret);
  51. return ret;
  52. }
  53. return 0;
  54. }
  55. static inline void img_prl_out_writel(struct img_prl_out *prl,
  56. u32 val, u32 reg)
  57. {
  58. writel(val, prl->base + reg);
  59. }
  60. static inline u32 img_prl_out_readl(struct img_prl_out *prl, u32 reg)
  61. {
  62. return readl(prl->base + reg);
  63. }
  64. static void img_prl_out_reset(struct img_prl_out *prl)
  65. {
  66. u32 ctl;
  67. ctl = img_prl_out_readl(prl, IMG_PRL_OUT_CTL) &
  68. ~IMG_PRL_OUT_CTL_ME_MASK;
  69. reset_control_assert(prl->rst);
  70. reset_control_deassert(prl->rst);
  71. img_prl_out_writel(prl, ctl, IMG_PRL_OUT_CTL);
  72. }
  73. static int img_prl_out_trigger(struct snd_pcm_substream *substream, int cmd,
  74. struct snd_soc_dai *dai)
  75. {
  76. struct img_prl_out *prl = snd_soc_dai_get_drvdata(dai);
  77. u32 reg;
  78. switch (cmd) {
  79. case SNDRV_PCM_TRIGGER_START:
  80. case SNDRV_PCM_TRIGGER_RESUME:
  81. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  82. reg = img_prl_out_readl(prl, IMG_PRL_OUT_CTL);
  83. reg |= IMG_PRL_OUT_CTL_ME_MASK;
  84. img_prl_out_writel(prl, reg, IMG_PRL_OUT_CTL);
  85. break;
  86. case SNDRV_PCM_TRIGGER_STOP:
  87. case SNDRV_PCM_TRIGGER_SUSPEND:
  88. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  89. img_prl_out_reset(prl);
  90. break;
  91. default:
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. static int img_prl_out_hw_params(struct snd_pcm_substream *substream,
  97. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  98. {
  99. struct img_prl_out *prl = snd_soc_dai_get_drvdata(dai);
  100. unsigned int rate, channels;
  101. u32 reg, control_set = 0;
  102. rate = params_rate(params);
  103. channels = params_channels(params);
  104. switch (params_format(params)) {
  105. case SNDRV_PCM_FORMAT_S32_LE:
  106. control_set |= IMG_PRL_OUT_CTL_PACKH_MASK;
  107. break;
  108. case SNDRV_PCM_FORMAT_S24_LE:
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. if (channels != 2)
  114. return -EINVAL;
  115. clk_set_rate(prl->clk_ref, rate * 256);
  116. reg = img_prl_out_readl(prl, IMG_PRL_OUT_CTL);
  117. reg = (reg & ~IMG_PRL_OUT_CTL_PACKH_MASK) | control_set;
  118. img_prl_out_writel(prl, reg, IMG_PRL_OUT_CTL);
  119. return 0;
  120. }
  121. static int img_prl_out_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  122. {
  123. struct img_prl_out *prl = snd_soc_dai_get_drvdata(dai);
  124. u32 reg, control_set = 0;
  125. int ret;
  126. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  127. case SND_SOC_DAIFMT_NB_NF:
  128. break;
  129. case SND_SOC_DAIFMT_NB_IF:
  130. control_set |= IMG_PRL_OUT_CTL_EDGE_MASK;
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. ret = pm_runtime_resume_and_get(prl->dev);
  136. if (ret < 0)
  137. return ret;
  138. reg = img_prl_out_readl(prl, IMG_PRL_OUT_CTL);
  139. reg = (reg & ~IMG_PRL_OUT_CTL_EDGE_MASK) | control_set;
  140. img_prl_out_writel(prl, reg, IMG_PRL_OUT_CTL);
  141. pm_runtime_put(prl->dev);
  142. return 0;
  143. }
  144. static const struct snd_soc_dai_ops img_prl_out_dai_ops = {
  145. .trigger = img_prl_out_trigger,
  146. .hw_params = img_prl_out_hw_params,
  147. .set_fmt = img_prl_out_set_fmt
  148. };
  149. static int img_prl_out_dai_probe(struct snd_soc_dai *dai)
  150. {
  151. struct img_prl_out *prl = snd_soc_dai_get_drvdata(dai);
  152. snd_soc_dai_init_dma_data(dai, &prl->dma_data, NULL);
  153. return 0;
  154. }
  155. static struct snd_soc_dai_driver img_prl_out_dai = {
  156. .probe = img_prl_out_dai_probe,
  157. .playback = {
  158. .channels_min = 2,
  159. .channels_max = 2,
  160. .rates = SNDRV_PCM_RATE_8000_192000,
  161. .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S24_LE
  162. },
  163. .ops = &img_prl_out_dai_ops
  164. };
  165. static const struct snd_soc_component_driver img_prl_out_component = {
  166. .name = "img-prl-out",
  167. .legacy_dai_naming = 1,
  168. };
  169. static int img_prl_out_probe(struct platform_device *pdev)
  170. {
  171. struct img_prl_out *prl;
  172. struct resource *res;
  173. void __iomem *base;
  174. int ret;
  175. struct device *dev = &pdev->dev;
  176. prl = devm_kzalloc(&pdev->dev, sizeof(*prl), GFP_KERNEL);
  177. if (!prl)
  178. return -ENOMEM;
  179. platform_set_drvdata(pdev, prl);
  180. prl->dev = &pdev->dev;
  181. base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  182. if (IS_ERR(base))
  183. return PTR_ERR(base);
  184. prl->base = base;
  185. prl->rst = devm_reset_control_get_exclusive(&pdev->dev, "rst");
  186. if (IS_ERR(prl->rst))
  187. return dev_err_probe(&pdev->dev, PTR_ERR(prl->rst),
  188. "No top level reset found\n");
  189. prl->clk_sys = devm_clk_get(&pdev->dev, "sys");
  190. if (IS_ERR(prl->clk_sys))
  191. return dev_err_probe(dev, PTR_ERR(prl->clk_sys),
  192. "Failed to acquire clock 'sys'\n");
  193. prl->clk_ref = devm_clk_get(&pdev->dev, "ref");
  194. if (IS_ERR(prl->clk_ref))
  195. return dev_err_probe(dev, PTR_ERR(prl->clk_ref),
  196. "Failed to acquire clock 'ref'\n");
  197. ret = clk_prepare_enable(prl->clk_sys);
  198. if (ret)
  199. return ret;
  200. img_prl_out_writel(prl, IMG_PRL_OUT_CTL_EDGE_MASK, IMG_PRL_OUT_CTL);
  201. img_prl_out_reset(prl);
  202. pm_runtime_enable(&pdev->dev);
  203. if (!pm_runtime_enabled(&pdev->dev)) {
  204. ret = img_prl_out_resume(&pdev->dev);
  205. if (ret)
  206. goto err_pm_disable;
  207. }
  208. prl->dma_data.addr = res->start + IMG_PRL_OUT_TX_FIFO;
  209. prl->dma_data.addr_width = 4;
  210. prl->dma_data.maxburst = 4;
  211. ret = devm_snd_soc_register_component(&pdev->dev,
  212. &img_prl_out_component,
  213. &img_prl_out_dai, 1);
  214. if (ret)
  215. goto err_suspend;
  216. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  217. if (ret)
  218. goto err_suspend;
  219. return 0;
  220. err_suspend:
  221. if (!pm_runtime_status_suspended(&pdev->dev))
  222. img_prl_out_suspend(&pdev->dev);
  223. err_pm_disable:
  224. pm_runtime_disable(&pdev->dev);
  225. clk_disable_unprepare(prl->clk_sys);
  226. return ret;
  227. }
  228. static int img_prl_out_dev_remove(struct platform_device *pdev)
  229. {
  230. struct img_prl_out *prl = platform_get_drvdata(pdev);
  231. pm_runtime_disable(&pdev->dev);
  232. if (!pm_runtime_status_suspended(&pdev->dev))
  233. img_prl_out_suspend(&pdev->dev);
  234. clk_disable_unprepare(prl->clk_sys);
  235. return 0;
  236. }
  237. static const struct of_device_id img_prl_out_of_match[] = {
  238. { .compatible = "img,parallel-out" },
  239. {}
  240. };
  241. MODULE_DEVICE_TABLE(of, img_prl_out_of_match);
  242. static const struct dev_pm_ops img_prl_out_pm_ops = {
  243. SET_RUNTIME_PM_OPS(img_prl_out_suspend,
  244. img_prl_out_resume, NULL)
  245. };
  246. static struct platform_driver img_prl_out_driver = {
  247. .driver = {
  248. .name = "img-parallel-out",
  249. .of_match_table = img_prl_out_of_match,
  250. .pm = &img_prl_out_pm_ops
  251. },
  252. .probe = img_prl_out_probe,
  253. .remove = img_prl_out_dev_remove
  254. };
  255. module_platform_driver(img_prl_out_driver);
  256. MODULE_AUTHOR("Damien Horsley <[email protected]>");
  257. MODULE_DESCRIPTION("IMG Parallel Output Driver");
  258. MODULE_LICENSE("GPL v2");