1 /* 2 * Renesas R-Car CMD support 3 * 4 * Copyright (C) 2015 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 13 struct rsnd_cmd { 14 struct rsnd_mod mod; 15 }; 16 17 #define CMD_NAME "cmd" 18 19 #define rsnd_cmd_nr(priv) ((priv)->cmd_nr) 20 #define for_each_rsnd_cmd(pos, priv, i) \ 21 for ((i) = 0; \ 22 ((i) < rsnd_cmd_nr(priv)) && \ 23 ((pos) = (struct rsnd_cmd *)(priv)->cmd + i); \ 24 i++) 25 26 static int rsnd_cmd_init(struct rsnd_mod *mod, 27 struct rsnd_dai_stream *io, 28 struct rsnd_priv *priv) 29 { 30 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io); 31 struct rsnd_mod *mix = rsnd_io_to_mod_mix(io); 32 struct device *dev = rsnd_priv_to_dev(priv); 33 u32 data; 34 35 if (!mix && !dvc) 36 return 0; 37 38 if (mix) { 39 struct rsnd_dai *rdai; 40 struct rsnd_mod *src; 41 struct rsnd_dai_stream *tio; 42 int i; 43 u32 path[] = { 44 [0] = 0, 45 [1] = 1 << 0, 46 [2] = 0, 47 [3] = 0, 48 [4] = 0, 49 [5] = 1 << 8 50 }; 51 52 /* 53 * it is assuming that integrater is well understanding about 54 * data path. Here doesn't check impossible connection, 55 * like src2 + src5 56 */ 57 data = 0; 58 for_each_rsnd_dai(rdai, priv, i) { 59 tio = &rdai->playback; 60 src = rsnd_io_to_mod_src(tio); 61 if (mix == rsnd_io_to_mod_mix(tio)) 62 data |= path[rsnd_mod_id(src)]; 63 64 tio = &rdai->capture; 65 src = rsnd_io_to_mod_src(tio); 66 if (mix == rsnd_io_to_mod_mix(tio)) 67 data |= path[rsnd_mod_id(src)]; 68 } 69 70 } else { 71 struct rsnd_mod *src = rsnd_io_to_mod_src(io); 72 73 u32 path[] = { 74 [0] = 0x30000, 75 [1] = 0x30001, 76 [2] = 0x40000, 77 [3] = 0x10000, 78 [4] = 0x20000, 79 [5] = 0x40100 80 }; 81 82 data = path[rsnd_mod_id(src)]; 83 } 84 85 dev_dbg(dev, "ctu/mix path = 0x%08x", data); 86 87 rsnd_mod_write(mod, CMD_ROUTE_SLCT, data); 88 rsnd_mod_write(mod, CMD_BUSIF_DALIGN, rsnd_get_dalign(mod, io)); 89 90 rsnd_adg_set_cmd_timsel_gen2(mod, io); 91 92 return 0; 93 } 94 95 static int rsnd_cmd_start(struct rsnd_mod *mod, 96 struct rsnd_dai_stream *io, 97 struct rsnd_priv *priv) 98 { 99 rsnd_mod_write(mod, CMD_CTRL, 0x10); 100 101 return 0; 102 } 103 104 static int rsnd_cmd_stop(struct rsnd_mod *mod, 105 struct rsnd_dai_stream *io, 106 struct rsnd_priv *priv) 107 { 108 rsnd_mod_write(mod, CMD_CTRL, 0); 109 110 return 0; 111 } 112 113 static struct rsnd_mod_ops rsnd_cmd_ops = { 114 .name = CMD_NAME, 115 .init = rsnd_cmd_init, 116 .start = rsnd_cmd_start, 117 .stop = rsnd_cmd_stop, 118 }; 119 120 int rsnd_cmd_attach(struct rsnd_dai_stream *io, int id) 121 { 122 struct rsnd_priv *priv = rsnd_io_to_priv(io); 123 struct rsnd_mod *mod = rsnd_cmd_mod_get(priv, id); 124 125 return rsnd_dai_connect(mod, io, mod->type); 126 } 127 128 struct rsnd_mod *rsnd_cmd_mod_get(struct rsnd_priv *priv, int id) 129 { 130 if (WARN_ON(id < 0 || id >= rsnd_cmd_nr(priv))) 131 id = 0; 132 133 return rsnd_mod_get((struct rsnd_cmd *)(priv->cmd) + id); 134 } 135 136 int rsnd_cmd_probe(struct rsnd_priv *priv) 137 { 138 struct device *dev = rsnd_priv_to_dev(priv); 139 struct rsnd_cmd *cmd; 140 int i, nr, ret; 141 142 /* This driver doesn't support Gen1 at this point */ 143 if (rsnd_is_gen1(priv)) 144 return 0; 145 146 /* same number as DVC */ 147 nr = priv->dvc_nr; 148 if (!nr) 149 return 0; 150 151 cmd = devm_kzalloc(dev, sizeof(*cmd) * nr, GFP_KERNEL); 152 if (!cmd) 153 return -ENOMEM; 154 155 priv->cmd_nr = nr; 156 priv->cmd = cmd; 157 158 for_each_rsnd_cmd(cmd, priv, i) { 159 ret = rsnd_mod_init(priv, rsnd_mod_get(cmd), 160 &rsnd_cmd_ops, NULL, 161 rsnd_mod_get_status, RSND_MOD_CMD, i); 162 if (ret) 163 return ret; 164 } 165 166 return 0; 167 } 168 169 void rsnd_cmd_remove(struct rsnd_priv *priv) 170 { 171 struct rsnd_cmd *cmd; 172 int i; 173 174 for_each_rsnd_cmd(cmd, priv, i) { 175 rsnd_mod_quit(rsnd_mod_get(cmd)); 176 } 177 } 178