scu.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. static int rsnd_scu_start(struct rsnd_mod *mod,
  127. struct rsnd_dai *rdai,
  128. struct rsnd_dai_stream *io)
  129. {
  130. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  131. struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
  132. struct device *dev = rsnd_priv_to_dev(priv);
  133. u32 flags = rsnd_scu_mode_flags(scu);
  134. int ret;
  135. /*
  136. * SCU will be used if it has RSND_SCU_USE_HPBIF flags
  137. */
  138. if (!(flags & RSND_SCU_USE_HPBIF)) {
  139. /* it use PIO transter */
  140. dev_dbg(dev, "%s%d is not used\n",
  141. rsnd_mod_name(mod), rsnd_mod_id(mod));
  142. return 0;
  143. }
  144. /* it use DMA transter */
  145. ret = rsnd_scu_set_route(priv, mod, rdai, io);
  146. if (ret < 0)
  147. return ret;
  148. ret = rsnd_scu_set_mode(priv, mod, rdai, io);
  149. if (ret < 0)
  150. return ret;
  151. ret = rsnd_scu_set_hpbif(priv, mod, rdai, io);
  152. if (ret < 0)
  153. return ret;
  154. dev_dbg(dev, "%s%d start\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
  155. return 0;
  156. }
  157. static struct rsnd_mod_ops rsnd_scu_ops = {
  158. .name = "scu",
  159. .start = rsnd_scu_start,
  160. };
  161. struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
  162. {
  163. BUG_ON(id < 0 || id >= rsnd_scu_nr(priv));
  164. return &((struct rsnd_scu *)(priv->scu) + id)->mod;
  165. }
  166. int rsnd_scu_probe(struct platform_device *pdev,
  167. struct rcar_snd_info *info,
  168. struct rsnd_priv *priv)
  169. {
  170. struct device *dev = rsnd_priv_to_dev(priv);
  171. struct rsnd_scu *scu;
  172. int i, nr;
  173. /*
  174. * init SCU
  175. */
  176. nr = info->scu_info_nr;
  177. scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
  178. if (!scu) {
  179. dev_err(dev, "SCU allocate failed\n");
  180. return -ENOMEM;
  181. }
  182. priv->scu_nr = nr;
  183. priv->scu = scu;
  184. for_each_rsnd_scu(scu, priv, i) {
  185. rsnd_mod_init(priv, &scu->mod,
  186. &rsnd_scu_ops, i);
  187. scu->info = &info->scu_info[i];
  188. dev_dbg(dev, "SCU%d probed\n", i);
  189. }
  190. dev_dbg(dev, "scu probed\n");
  191. return 0;
  192. }
  193. void rsnd_scu_remove(struct platform_device *pdev,
  194. struct rsnd_priv *priv)
  195. {
  196. }