scu.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_init(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 device *dev = rsnd_priv_to_dev(priv);
  132. dev_dbg(dev, "%s.%d init\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
  133. return 0;
  134. }
  135. static int rsnd_scu_quit(struct rsnd_mod *mod,
  136. struct rsnd_dai *rdai,
  137. struct rsnd_dai_stream *io)
  138. {
  139. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  140. struct device *dev = rsnd_priv_to_dev(priv);
  141. dev_dbg(dev, "%s.%d quit\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
  142. return 0;
  143. }
  144. static int rsnd_scu_start(struct rsnd_mod *mod,
  145. struct rsnd_dai *rdai,
  146. struct rsnd_dai_stream *io)
  147. {
  148. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  149. struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
  150. struct device *dev = rsnd_priv_to_dev(priv);
  151. u32 flags = rsnd_scu_mode_flags(scu);
  152. int ret;
  153. /*
  154. * SCU will be used if it has RSND_SCU_USB_HPBIF flags
  155. */
  156. if (!(flags & RSND_SCU_USB_HPBIF)) {
  157. /* it use PIO transter */
  158. dev_dbg(dev, "%s%d is not used\n",
  159. rsnd_mod_name(mod), rsnd_mod_id(mod));
  160. return 0;
  161. }
  162. /* it use DMA transter */
  163. ret = rsnd_scu_set_route(priv, mod, rdai, io);
  164. if (ret < 0)
  165. return ret;
  166. ret = rsnd_scu_set_mode(priv, mod, rdai, io);
  167. if (ret < 0)
  168. return ret;
  169. ret = rsnd_scu_set_hpbif(priv, mod, rdai, io);
  170. if (ret < 0)
  171. return ret;
  172. dev_dbg(dev, "%s%d start\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
  173. return 0;
  174. }
  175. static int rsnd_scu_stop(struct rsnd_mod *mod,
  176. struct rsnd_dai *rdai,
  177. struct rsnd_dai_stream *io)
  178. {
  179. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  180. struct device *dev = rsnd_priv_to_dev(priv);
  181. dev_dbg(dev, "%s.%d stop\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
  182. return 0;
  183. }
  184. static struct rsnd_mod_ops rsnd_scu_ops = {
  185. .name = "scu",
  186. .init = rsnd_scu_init,
  187. .quit = rsnd_scu_quit,
  188. .start = rsnd_scu_start,
  189. .stop = rsnd_scu_stop,
  190. };
  191. struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
  192. {
  193. BUG_ON(id < 0 || id >= rsnd_scu_nr(priv));
  194. return &((struct rsnd_scu *)(priv->scu) + id)->mod;
  195. }
  196. int rsnd_scu_probe(struct platform_device *pdev,
  197. struct rcar_snd_info *info,
  198. struct rsnd_priv *priv)
  199. {
  200. struct device *dev = rsnd_priv_to_dev(priv);
  201. struct rsnd_scu *scu;
  202. int i, nr;
  203. /*
  204. * init SCU
  205. */
  206. nr = info->scu_info_nr;
  207. scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
  208. if (!scu) {
  209. dev_err(dev, "SCU allocate failed\n");
  210. return -ENOMEM;
  211. }
  212. priv->scu_nr = nr;
  213. priv->scu = scu;
  214. for_each_rsnd_scu(scu, priv, i) {
  215. rsnd_mod_init(priv, &scu->mod,
  216. &rsnd_scu_ops, i);
  217. scu->info = &info->scu_info[i];
  218. dev_dbg(dev, "SCU%d probed\n", i);
  219. }
  220. dev_dbg(dev, "scu probed\n");
  221. return 0;
  222. }
  223. void rsnd_scu_remove(struct platform_device *pdev,
  224. struct rsnd_priv *priv)
  225. {
  226. }