img-parallel-out.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. pm_runtime_get_sync(prl->dev);
  140. reg = img_prl_out_readl(prl, IMG_PRL_OUT_CTL);
  141. reg = (reg & ~IMG_PRL_OUT_CTL_EDGE_MASK) | control_set;
  142. img_prl_out_writel(prl, reg, IMG_PRL_OUT_CTL);
  143. pm_runtime_put(prl->dev);
  144. return 0;
  145. }
  146. static const struct snd_soc_dai_ops img_prl_out_dai_ops = {
  147. .trigger = img_prl_out_trigger,
  148. .hw_params = img_prl_out_hw_params,
  149. .set_fmt = img_prl_out_set_fmt
  150. };
  151. static int img_prl_out_dai_probe(struct snd_soc_dai *dai)
  152. {
  153. struct img_prl_out *prl = snd_soc_dai_get_drvdata(dai);
  154. snd_soc_dai_init_dma_data(dai, &prl->dma_data, NULL);
  155. return 0;
  156. }
  157. static struct snd_soc_dai_driver img_prl_out_dai = {
  158. .probe = img_prl_out_dai_probe,
  159. .playback = {
  160. .channels_min = 2,
  161. .channels_max = 2,
  162. .rates = SNDRV_PCM_RATE_8000_192000,
  163. .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S24_LE
  164. },
  165. .ops = &img_prl_out_dai_ops
  166. };
  167. static const struct snd_soc_component_driver img_prl_out_component = {
  168. .name = "img-prl-out"
  169. };
  170. static int img_prl_out_probe(struct platform_device *pdev)
  171. {
  172. struct img_prl_out *prl;
  173. struct resource *res;
  174. void __iomem *base;
  175. int ret;
  176. struct device *dev = &pdev->dev;
  177. prl = devm_kzalloc(&pdev->dev, sizeof(*prl), GFP_KERNEL);
  178. if (!prl)
  179. return -ENOMEM;
  180. platform_set_drvdata(pdev, prl);
  181. prl->dev = &pdev->dev;
  182. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  183. base = devm_ioremap_resource(&pdev->dev, res);
  184. if (IS_ERR(base))
  185. return PTR_ERR(base);
  186. prl->base = base;
  187. prl->rst = devm_reset_control_get(&pdev->dev, "rst");
  188. if (IS_ERR(prl->rst)) {
  189. if (PTR_ERR(prl->rst) != -EPROBE_DEFER)
  190. dev_err(&pdev->dev, "No top level reset found\n");
  191. return PTR_ERR(prl->rst);
  192. }
  193. prl->clk_sys = devm_clk_get(&pdev->dev, "sys");
  194. if (IS_ERR(prl->clk_sys)) {
  195. if (PTR_ERR(prl->clk_sys) != -EPROBE_DEFER)
  196. dev_err(dev, "Failed to acquire clock 'sys'\n");
  197. return PTR_ERR(prl->clk_sys);
  198. }
  199. prl->clk_ref = devm_clk_get(&pdev->dev, "ref");
  200. if (IS_ERR(prl->clk_ref)) {
  201. if (PTR_ERR(prl->clk_ref) != -EPROBE_DEFER)
  202. dev_err(dev, "Failed to acquire clock 'ref'\n");
  203. return PTR_ERR(prl->clk_ref);
  204. }
  205. ret = clk_prepare_enable(prl->clk_sys);
  206. if (ret)
  207. return ret;
  208. img_prl_out_writel(prl, IMG_PRL_OUT_CTL_EDGE_MASK, IMG_PRL_OUT_CTL);
  209. img_prl_out_reset(prl);
  210. pm_runtime_enable(&pdev->dev);
  211. if (!pm_runtime_enabled(&pdev->dev)) {
  212. ret = img_prl_out_resume(&pdev->dev);
  213. if (ret)
  214. goto err_pm_disable;
  215. }
  216. prl->dma_data.addr = res->start + IMG_PRL_OUT_TX_FIFO;
  217. prl->dma_data.addr_width = 4;
  218. prl->dma_data.maxburst = 4;
  219. ret = devm_snd_soc_register_component(&pdev->dev,
  220. &img_prl_out_component,
  221. &img_prl_out_dai, 1);
  222. if (ret)
  223. goto err_suspend;
  224. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  225. if (ret)
  226. goto err_suspend;
  227. return 0;
  228. err_suspend:
  229. if (!pm_runtime_status_suspended(&pdev->dev))
  230. img_prl_out_suspend(&pdev->dev);
  231. err_pm_disable:
  232. pm_runtime_disable(&pdev->dev);
  233. clk_disable_unprepare(prl->clk_sys);
  234. return ret;
  235. }
  236. static int img_prl_out_dev_remove(struct platform_device *pdev)
  237. {
  238. struct img_prl_out *prl = platform_get_drvdata(pdev);
  239. pm_runtime_disable(&pdev->dev);
  240. if (!pm_runtime_status_suspended(&pdev->dev))
  241. img_prl_out_suspend(&pdev->dev);
  242. clk_disable_unprepare(prl->clk_sys);
  243. return 0;
  244. }
  245. static const struct of_device_id img_prl_out_of_match[] = {
  246. { .compatible = "img,parallel-out" },
  247. {}
  248. };
  249. MODULE_DEVICE_TABLE(of, img_prl_out_of_match);
  250. static const struct dev_pm_ops img_prl_out_pm_ops = {
  251. SET_RUNTIME_PM_OPS(img_prl_out_suspend,
  252. img_prl_out_resume, NULL)
  253. };
  254. static struct platform_driver img_prl_out_driver = {
  255. .driver = {
  256. .name = "img-parallel-out",
  257. .of_match_table = img_prl_out_of_match,
  258. .pm = &img_prl_out_pm_ops
  259. },
  260. .probe = img_prl_out_probe,
  261. .remove = img_prl_out_dev_remove
  262. };
  263. module_platform_driver(img_prl_out_driver);
  264. MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
  265. MODULE_DESCRIPTION("IMG Parallel Output Driver");
  266. MODULE_LICENSE("GPL v2");