img-parallel-out.c 7.6 KB

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