rockchip_sound.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 Google, LLC
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #define LOG_CATEGORY UCLASS_SOUND
  7. #include <common.h>
  8. #include <audio_codec.h>
  9. #include <clk.h>
  10. #include <dm.h>
  11. #include <i2s.h>
  12. #include <log.h>
  13. #include <misc.h>
  14. #include <sound.h>
  15. #include <asm/arch-rockchip/periph.h>
  16. #include <dm/pinctrl.h>
  17. static int rockchip_sound_setup(struct udevice *dev)
  18. {
  19. struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  20. struct i2s_uc_priv *i2c_priv = dev_get_uclass_priv(uc_priv->i2s);
  21. int ret;
  22. if (uc_priv->setup_done)
  23. return -EALREADY;
  24. ret = audio_codec_set_params(uc_priv->codec, i2c_priv->id,
  25. i2c_priv->samplingrate,
  26. i2c_priv->samplingrate * i2c_priv->rfs,
  27. i2c_priv->bitspersample,
  28. i2c_priv->channels);
  29. if (ret)
  30. return ret;
  31. uc_priv->setup_done = true;
  32. return 0;
  33. }
  34. static int rockchip_sound_play(struct udevice *dev, void *data, uint data_size)
  35. {
  36. struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  37. return i2s_tx_data(uc_priv->i2s, data, data_size);
  38. }
  39. static int rockchip_sound_probe(struct udevice *dev)
  40. {
  41. struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  42. struct ofnode_phandle_args args;
  43. struct udevice *pinctrl;
  44. struct clk clk;
  45. ofnode node;
  46. int ret;
  47. node = ofnode_find_subnode(dev_ofnode(dev), "cpu");
  48. if (!ofnode_valid(node)) {
  49. log_debug("Failed to find /cpu subnode\n");
  50. return -EINVAL;
  51. }
  52. ret = ofnode_parse_phandle_with_args(node, "sound-dai",
  53. "#sound-dai-cells", 0, 0, &args);
  54. if (ret) {
  55. log_debug("Cannot find i2s phandle: %d\n", ret);
  56. return ret;
  57. }
  58. ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s);
  59. if (ret) {
  60. log_debug("Cannot find i2s: %d\n", ret);
  61. return ret;
  62. }
  63. node = ofnode_find_subnode(dev_ofnode(dev), "codec");
  64. if (!ofnode_valid(node)) {
  65. log_debug("Failed to find /codec subnode\n");
  66. return -EINVAL;
  67. }
  68. ret = ofnode_parse_phandle_with_args(node, "sound-dai",
  69. "#sound-dai-cells", 0, 0, &args);
  70. if (ret) {
  71. log_debug("Cannot find codec phandle: %d\n", ret);
  72. return ret;
  73. }
  74. ret = uclass_get_device_by_ofnode(UCLASS_AUDIO_CODEC, args.node,
  75. &uc_priv->codec);
  76. if (ret) {
  77. log_debug("Cannot find audio codec: %d\n", ret);
  78. return ret;
  79. }
  80. ret = clk_get_by_index(uc_priv->i2s, 1, &clk);
  81. if (ret) {
  82. log_debug("Cannot find clock: %d\n", ret);
  83. return ret;
  84. }
  85. ret = clk_set_rate(&clk, 12288000);
  86. if (ret < 0) {
  87. log_debug("Cannot find clock: %d\n", ret);
  88. return ret;
  89. }
  90. ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
  91. if (ret) {
  92. debug("%s: Cannot find pinctrl device\n", __func__);
  93. return ret;
  94. }
  95. ret = pinctrl_request(pinctrl, PERIPH_ID_I2S, 0);
  96. if (ret) {
  97. debug("%s: Cannot select I2C pinctrl\n", __func__);
  98. return ret;
  99. }
  100. log_debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name,
  101. uc_priv->codec->name, uc_priv->i2s->name);
  102. return 0;
  103. }
  104. static const struct sound_ops rockchip_sound_ops = {
  105. .setup = rockchip_sound_setup,
  106. .play = rockchip_sound_play,
  107. };
  108. static const struct udevice_id rockchip_sound_ids[] = {
  109. { .compatible = "rockchip,audio-max98090-jerry" },
  110. { }
  111. };
  112. U_BOOT_DRIVER(rockchip_sound) = {
  113. .name = "rockchip_sound",
  114. .id = UCLASS_SOUND,
  115. .of_match = rockchip_sound_ids,
  116. .probe = rockchip_sound_probe,
  117. .ops = &rockchip_sound_ops,
  118. };