scu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Renesas R-Car SCU support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include "rsnd.h"
  12. struct rsnd_scu {
  13. struct rsnd_scu_platform_info *info; /* rcar_snd.h */
  14. struct rsnd_mod mod;
  15. };
  16. #define rsnd_scu_mode_flags(p) ((p)->info->flags)
  17. /*
  18. * ADINR
  19. */
  20. #define OTBL_24 (0 << 16)
  21. #define OTBL_22 (2 << 16)
  22. #define OTBL_20 (4 << 16)
  23. #define OTBL_18 (6 << 16)
  24. #define OTBL_16 (8 << 16)
  25. #define rsnd_mod_to_scu(_mod) \
  26. container_of((_mod), struct rsnd_scu, mod)
  27. #define for_each_rsnd_scu(pos, priv, i) \
  28. for ((i) = 0; \
  29. ((i) < rsnd_scu_nr(priv)) && \
  30. ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
  31. i++)
  32. static int rsnd_scu_set_route(struct rsnd_priv *priv,
  33. struct rsnd_mod *mod,
  34. struct rsnd_dai *rdai,
  35. struct rsnd_dai_stream *io)
  36. {
  37. struct scu_route_config {
  38. u32 mask;
  39. int shift;
  40. } routes[] = {
  41. { 0xF, 0, }, /* 0 */
  42. { 0xF, 4, }, /* 1 */
  43. { 0xF, 8, }, /* 2 */
  44. { 0x7, 12, }, /* 3 */
  45. { 0x7, 16, }, /* 4 */
  46. { 0x7, 20, }, /* 5 */
  47. { 0x7, 24, }, /* 6 */
  48. { 0x3, 28, }, /* 7 */
  49. { 0x3, 30, }, /* 8 */
  50. };
  51. u32 mask;
  52. u32 val;
  53. int shift;
  54. int id;
  55. /*
  56. * Gen1 only
  57. */
  58. if (!rsnd_is_gen1(priv))
  59. return 0;
  60. id = rsnd_mod_id(mod);
  61. if (id < 0 || id > ARRAY_SIZE(routes))
  62. return -EIO;
  63. /*
  64. * SRC_ROUTE_SELECT
  65. */
  66. val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
  67. val = val << routes[id].shift;
  68. mask = routes[id].mask << routes[id].shift;
  69. rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
  70. /*
  71. * SRC_TIMING_SELECT
  72. */
  73. shift = (id % 4) * 8;
  74. mask = 0x1F << shift;
  75. if (8 == id) /* SRU8 is very special */
  76. val = id << shift;
  77. else
  78. val = (id + 1) << shift;
  79. switch (id / 4) {
  80. case 0:
  81. rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
  82. break;
  83. case 1:
  84. rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
  85. break;
  86. case 2:
  87. rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
  88. break;
  89. }
  90. return 0;
  91. }
  92. static int rsnd_scu_set_mode(struct rsnd_priv *priv,
  93. struct rsnd_mod *mod,
  94. struct rsnd_dai *rdai,
  95. struct rsnd_dai_stream *io)
  96. {
  97. int id = rsnd_mod_id(mod);
  98. u32 val;
  99. if (rsnd_is_gen1(priv)) {
  100. val = (1 << id);
  101. rsnd_mod_bset(mod, SRC_CTRL, val, val);
  102. }
  103. return 0;
  104. }
  105. static int rsnd_scu_set_hpbif(struct rsnd_priv *priv,
  106. struct rsnd_mod *mod,
  107. struct rsnd_dai *rdai,
  108. struct rsnd_dai_stream *io)
  109. {
  110. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  111. u32 adinr = runtime->channels;
  112. switch (runtime->sample_bits) {
  113. case 16:
  114. adinr |= OTBL_16;
  115. break;
  116. case 32:
  117. adinr |= OTBL_24;
  118. break;
  119. default:
  120. return -EIO;
  121. }
  122. rsnd_mod_write(mod, BUSIF_MODE, 1);
  123. rsnd_mod_write(mod, BUSIF_ADINR, adinr);
  124. return 0;
  125. }
  126. bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod)
  127. {
  128. struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
  129. u32 flags = rsnd_scu_mode_flags(scu);
  130. return !!(flags & RSND_SCU_USE_HPBIF);
  131. }
  132. static int rsnd_scu_start(struct rsnd_mod *mod,
  133. struct rsnd_dai *rdai,
  134. struct rsnd_dai_stream *io)
  135. {
  136. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  137. struct device *dev = rsnd_priv_to_dev(priv);
  138. int ret;
  139. /*
  140. * SCU will be used if it has RSND_SCU_USE_HPBIF flags
  141. */
  142. if (!rsnd_scu_hpbif_is_enable(mod)) {
  143. /* it use PIO transter */
  144. dev_dbg(dev, "%s%d is not used\n",
  145. rsnd_mod_name(mod), rsnd_mod_id(mod));
  146. return 0;
  147. }
  148. /* it use DMA transter */
  149. ret = rsnd_scu_set_route(priv, mod, rdai, io);
  150. if (ret < 0)
  151. return ret;
  152. ret = rsnd_scu_set_mode(priv, mod, rdai, io);
  153. if (ret < 0)
  154. return ret;
  155. ret = rsnd_scu_set_hpbif(priv, mod, rdai, io);
  156. if (ret < 0)
  157. return ret;
  158. dev_dbg(dev, "%s%d start\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
  159. return 0;
  160. }
  161. static struct rsnd_mod_ops rsnd_scu_ops = {
  162. .name = "scu",
  163. .start = rsnd_scu_start,
  164. };
  165. struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
  166. {
  167. if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
  168. id = 0;
  169. return &((struct rsnd_scu *)(priv->scu) + id)->mod;
  170. }
  171. int rsnd_scu_probe(struct platform_device *pdev,
  172. struct rcar_snd_info *info,
  173. struct rsnd_priv *priv)
  174. {
  175. struct device *dev = rsnd_priv_to_dev(priv);
  176. struct rsnd_scu *scu;
  177. int i, nr;
  178. /*
  179. * init SCU
  180. */
  181. nr = info->scu_info_nr;
  182. scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
  183. if (!scu) {
  184. dev_err(dev, "SCU allocate failed\n");
  185. return -ENOMEM;
  186. }
  187. priv->scu_nr = nr;
  188. priv->scu = scu;
  189. for_each_rsnd_scu(scu, priv, i) {
  190. rsnd_mod_init(priv, &scu->mod,
  191. &rsnd_scu_ops, i);
  192. scu->info = &info->scu_info[i];
  193. dev_dbg(dev, "SCU%d probed\n", i);
  194. }
  195. dev_dbg(dev, "scu probed\n");
  196. return 0;
  197. }
  198. void rsnd_scu_remove(struct platform_device *pdev,
  199. struct rsnd_priv *priv)
  200. {
  201. }