aiu.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <[email protected]>
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/module.h>
  8. #include <linux/of_platform.h>
  9. #include <linux/regmap.h>
  10. #include <linux/reset.h>
  11. #include <sound/soc.h>
  12. #include <sound/soc-dai.h>
  13. #include <dt-bindings/sound/meson-aiu.h>
  14. #include "aiu.h"
  15. #include "aiu-fifo.h"
  16. #define AIU_I2S_MISC_958_SRC_SHIFT 3
  17. static const char * const aiu_spdif_encode_sel_texts[] = {
  18. "SPDIF", "I2S",
  19. };
  20. static SOC_ENUM_SINGLE_DECL(aiu_spdif_encode_sel_enum, AIU_I2S_MISC,
  21. AIU_I2S_MISC_958_SRC_SHIFT,
  22. aiu_spdif_encode_sel_texts);
  23. static const struct snd_kcontrol_new aiu_spdif_encode_mux =
  24. SOC_DAPM_ENUM("SPDIF Buffer Src", aiu_spdif_encode_sel_enum);
  25. static const struct snd_soc_dapm_widget aiu_cpu_dapm_widgets[] = {
  26. SND_SOC_DAPM_MUX("SPDIF SRC SEL", SND_SOC_NOPM, 0, 0,
  27. &aiu_spdif_encode_mux),
  28. };
  29. static const struct snd_soc_dapm_route aiu_cpu_dapm_routes[] = {
  30. { "I2S Encoder Playback", NULL, "I2S FIFO Playback" },
  31. { "SPDIF SRC SEL", "SPDIF", "SPDIF FIFO Playback" },
  32. { "SPDIF SRC SEL", "I2S", "I2S FIFO Playback" },
  33. { "SPDIF Encoder Playback", NULL, "SPDIF SRC SEL" },
  34. };
  35. int aiu_of_xlate_dai_name(struct snd_soc_component *component,
  36. const struct of_phandle_args *args,
  37. const char **dai_name,
  38. unsigned int component_id)
  39. {
  40. struct snd_soc_dai *dai;
  41. int id;
  42. if (args->args_count != 2)
  43. return -EINVAL;
  44. if (args->args[0] != component_id)
  45. return -EINVAL;
  46. id = args->args[1];
  47. if (id < 0 || id >= component->num_dai)
  48. return -EINVAL;
  49. for_each_component_dais(component, dai) {
  50. if (id == 0)
  51. break;
  52. id--;
  53. }
  54. *dai_name = dai->driver->name;
  55. return 0;
  56. }
  57. static int aiu_cpu_of_xlate_dai_name(struct snd_soc_component *component,
  58. const struct of_phandle_args *args,
  59. const char **dai_name)
  60. {
  61. return aiu_of_xlate_dai_name(component, args, dai_name, AIU_CPU);
  62. }
  63. static int aiu_cpu_component_probe(struct snd_soc_component *component)
  64. {
  65. struct aiu *aiu = snd_soc_component_get_drvdata(component);
  66. /* Required for the SPDIF Source control operation */
  67. return clk_prepare_enable(aiu->i2s.clks[PCLK].clk);
  68. }
  69. static void aiu_cpu_component_remove(struct snd_soc_component *component)
  70. {
  71. struct aiu *aiu = snd_soc_component_get_drvdata(component);
  72. clk_disable_unprepare(aiu->i2s.clks[PCLK].clk);
  73. }
  74. static const struct snd_soc_component_driver aiu_cpu_component = {
  75. .name = "AIU CPU",
  76. .dapm_widgets = aiu_cpu_dapm_widgets,
  77. .num_dapm_widgets = ARRAY_SIZE(aiu_cpu_dapm_widgets),
  78. .dapm_routes = aiu_cpu_dapm_routes,
  79. .num_dapm_routes = ARRAY_SIZE(aiu_cpu_dapm_routes),
  80. .of_xlate_dai_name = aiu_cpu_of_xlate_dai_name,
  81. .pointer = aiu_fifo_pointer,
  82. .probe = aiu_cpu_component_probe,
  83. .remove = aiu_cpu_component_remove,
  84. #ifdef CONFIG_DEBUG_FS
  85. .debugfs_prefix = "cpu",
  86. #endif
  87. };
  88. static struct snd_soc_dai_driver aiu_cpu_dai_drv[] = {
  89. [CPU_I2S_FIFO] = {
  90. .name = "I2S FIFO",
  91. .playback = {
  92. .stream_name = "I2S FIFO Playback",
  93. .channels_min = 2,
  94. .channels_max = 8,
  95. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  96. .rate_min = 5512,
  97. .rate_max = 192000,
  98. .formats = AIU_FORMATS,
  99. },
  100. .ops = &aiu_fifo_i2s_dai_ops,
  101. .pcm_new = aiu_fifo_pcm_new,
  102. .probe = aiu_fifo_i2s_dai_probe,
  103. .remove = aiu_fifo_dai_remove,
  104. },
  105. [CPU_SPDIF_FIFO] = {
  106. .name = "SPDIF FIFO",
  107. .playback = {
  108. .stream_name = "SPDIF FIFO Playback",
  109. .channels_min = 2,
  110. .channels_max = 2,
  111. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  112. .rate_min = 5512,
  113. .rate_max = 192000,
  114. .formats = AIU_FORMATS,
  115. },
  116. .ops = &aiu_fifo_spdif_dai_ops,
  117. .pcm_new = aiu_fifo_pcm_new,
  118. .probe = aiu_fifo_spdif_dai_probe,
  119. .remove = aiu_fifo_dai_remove,
  120. },
  121. [CPU_I2S_ENCODER] = {
  122. .name = "I2S Encoder",
  123. .playback = {
  124. .stream_name = "I2S Encoder Playback",
  125. .channels_min = 2,
  126. .channels_max = 8,
  127. .rates = SNDRV_PCM_RATE_8000_192000,
  128. .formats = AIU_FORMATS,
  129. },
  130. .ops = &aiu_encoder_i2s_dai_ops,
  131. },
  132. [CPU_SPDIF_ENCODER] = {
  133. .name = "SPDIF Encoder",
  134. .playback = {
  135. .stream_name = "SPDIF Encoder Playback",
  136. .channels_min = 2,
  137. .channels_max = 2,
  138. .rates = (SNDRV_PCM_RATE_32000 |
  139. SNDRV_PCM_RATE_44100 |
  140. SNDRV_PCM_RATE_48000 |
  141. SNDRV_PCM_RATE_88200 |
  142. SNDRV_PCM_RATE_96000 |
  143. SNDRV_PCM_RATE_176400 |
  144. SNDRV_PCM_RATE_192000),
  145. .formats = AIU_FORMATS,
  146. },
  147. .ops = &aiu_encoder_spdif_dai_ops,
  148. }
  149. };
  150. static const struct regmap_config aiu_regmap_cfg = {
  151. .reg_bits = 32,
  152. .val_bits = 32,
  153. .reg_stride = 4,
  154. .max_register = 0x2ac,
  155. };
  156. static int aiu_clk_bulk_get(struct device *dev,
  157. const char * const *ids,
  158. unsigned int num,
  159. struct aiu_interface *interface)
  160. {
  161. struct clk_bulk_data *clks;
  162. int i, ret;
  163. clks = devm_kcalloc(dev, num, sizeof(*clks), GFP_KERNEL);
  164. if (!clks)
  165. return -ENOMEM;
  166. for (i = 0; i < num; i++)
  167. clks[i].id = ids[i];
  168. ret = devm_clk_bulk_get(dev, num, clks);
  169. if (ret < 0)
  170. return ret;
  171. interface->clks = clks;
  172. interface->clk_num = num;
  173. return 0;
  174. }
  175. static const char * const aiu_i2s_ids[] = {
  176. [PCLK] = "i2s_pclk",
  177. [AOCLK] = "i2s_aoclk",
  178. [MCLK] = "i2s_mclk",
  179. [MIXER] = "i2s_mixer",
  180. };
  181. static const char * const aiu_spdif_ids[] = {
  182. [PCLK] = "spdif_pclk",
  183. [AOCLK] = "spdif_aoclk",
  184. [MCLK] = "spdif_mclk_sel"
  185. };
  186. static int aiu_clk_get(struct device *dev)
  187. {
  188. struct aiu *aiu = dev_get_drvdata(dev);
  189. int ret;
  190. aiu->pclk = devm_clk_get(dev, "pclk");
  191. if (IS_ERR(aiu->pclk))
  192. return dev_err_probe(dev, PTR_ERR(aiu->pclk), "Can't get the aiu pclk\n");
  193. aiu->spdif_mclk = devm_clk_get(dev, "spdif_mclk");
  194. if (IS_ERR(aiu->spdif_mclk))
  195. return dev_err_probe(dev, PTR_ERR(aiu->spdif_mclk),
  196. "Can't get the aiu spdif master clock\n");
  197. ret = aiu_clk_bulk_get(dev, aiu_i2s_ids, ARRAY_SIZE(aiu_i2s_ids),
  198. &aiu->i2s);
  199. if (ret)
  200. return dev_err_probe(dev, ret, "Can't get the i2s clocks\n");
  201. ret = aiu_clk_bulk_get(dev, aiu_spdif_ids, ARRAY_SIZE(aiu_spdif_ids),
  202. &aiu->spdif);
  203. if (ret)
  204. return dev_err_probe(dev, ret, "Can't get the spdif clocks\n");
  205. ret = clk_prepare_enable(aiu->pclk);
  206. if (ret) {
  207. dev_err(dev, "peripheral clock enable failed\n");
  208. return ret;
  209. }
  210. ret = devm_add_action_or_reset(dev,
  211. (void(*)(void *))clk_disable_unprepare,
  212. aiu->pclk);
  213. if (ret)
  214. dev_err(dev, "failed to add reset action on pclk");
  215. return ret;
  216. }
  217. static int aiu_probe(struct platform_device *pdev)
  218. {
  219. struct device *dev = &pdev->dev;
  220. void __iomem *regs;
  221. struct regmap *map;
  222. struct aiu *aiu;
  223. int ret;
  224. aiu = devm_kzalloc(dev, sizeof(*aiu), GFP_KERNEL);
  225. if (!aiu)
  226. return -ENOMEM;
  227. aiu->platform = device_get_match_data(dev);
  228. if (!aiu->platform)
  229. return -ENODEV;
  230. platform_set_drvdata(pdev, aiu);
  231. ret = device_reset(dev);
  232. if (ret)
  233. return dev_err_probe(dev, ret, "Failed to reset device\n");
  234. regs = devm_platform_ioremap_resource(pdev, 0);
  235. if (IS_ERR(regs))
  236. return PTR_ERR(regs);
  237. map = devm_regmap_init_mmio(dev, regs, &aiu_regmap_cfg);
  238. if (IS_ERR(map)) {
  239. dev_err(dev, "failed to init regmap: %ld\n",
  240. PTR_ERR(map));
  241. return PTR_ERR(map);
  242. }
  243. aiu->i2s.irq = platform_get_irq_byname(pdev, "i2s");
  244. if (aiu->i2s.irq < 0)
  245. return aiu->i2s.irq;
  246. aiu->spdif.irq = platform_get_irq_byname(pdev, "spdif");
  247. if (aiu->spdif.irq < 0)
  248. return aiu->spdif.irq;
  249. ret = aiu_clk_get(dev);
  250. if (ret)
  251. return ret;
  252. /* Register the cpu component of the aiu */
  253. ret = snd_soc_register_component(dev, &aiu_cpu_component,
  254. aiu_cpu_dai_drv,
  255. ARRAY_SIZE(aiu_cpu_dai_drv));
  256. if (ret) {
  257. dev_err(dev, "Failed to register cpu component\n");
  258. return ret;
  259. }
  260. /* Register the hdmi codec control component */
  261. ret = aiu_hdmi_ctrl_register_component(dev);
  262. if (ret) {
  263. dev_err(dev, "Failed to register hdmi control component\n");
  264. goto err;
  265. }
  266. /* Register the internal dac control component on gxl */
  267. if (aiu->platform->has_acodec) {
  268. ret = aiu_acodec_ctrl_register_component(dev);
  269. if (ret) {
  270. dev_err(dev,
  271. "Failed to register acodec control component\n");
  272. goto err;
  273. }
  274. }
  275. return 0;
  276. err:
  277. snd_soc_unregister_component(dev);
  278. return ret;
  279. }
  280. static int aiu_remove(struct platform_device *pdev)
  281. {
  282. snd_soc_unregister_component(&pdev->dev);
  283. return 0;
  284. }
  285. static const struct aiu_platform_data aiu_gxbb_pdata = {
  286. .has_acodec = false,
  287. .has_clk_ctrl_more_i2s_div = true,
  288. };
  289. static const struct aiu_platform_data aiu_gxl_pdata = {
  290. .has_acodec = true,
  291. .has_clk_ctrl_more_i2s_div = true,
  292. };
  293. static const struct aiu_platform_data aiu_meson8_pdata = {
  294. .has_acodec = false,
  295. .has_clk_ctrl_more_i2s_div = false,
  296. };
  297. static const struct of_device_id aiu_of_match[] = {
  298. { .compatible = "amlogic,aiu-gxbb", .data = &aiu_gxbb_pdata },
  299. { .compatible = "amlogic,aiu-gxl", .data = &aiu_gxl_pdata },
  300. { .compatible = "amlogic,aiu-meson8", .data = &aiu_meson8_pdata },
  301. { .compatible = "amlogic,aiu-meson8b", .data = &aiu_meson8_pdata },
  302. {}
  303. };
  304. MODULE_DEVICE_TABLE(of, aiu_of_match);
  305. static struct platform_driver aiu_pdrv = {
  306. .probe = aiu_probe,
  307. .remove = aiu_remove,
  308. .driver = {
  309. .name = "meson-aiu",
  310. .of_match_table = aiu_of_match,
  311. },
  312. };
  313. module_platform_driver(aiu_pdrv);
  314. MODULE_DESCRIPTION("Meson AIU Driver");
  315. MODULE_AUTHOR("Jerome Brunet <[email protected]>");
  316. MODULE_LICENSE("GPL v2");