Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Helper routines for R-Car sound ADG. |
| 3 | * |
| 4 | * Copyright (C) 2013 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 10 | #include <linux/clk-provider.h> |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 11 | #include "rsnd.h" |
| 12 | |
| 13 | #define CLKA 0 |
| 14 | #define CLKB 1 |
| 15 | #define CLKC 2 |
| 16 | #define CLKI 3 |
| 17 | #define CLKMAX 4 |
| 18 | |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 19 | #define CLKOUT 0 |
| 20 | #define CLKOUT1 1 |
| 21 | #define CLKOUT2 2 |
| 22 | #define CLKOUT3 3 |
| 23 | #define CLKOUTMAX 4 |
| 24 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 25 | #define BRRx_MASK(x) (0x3FF & x) |
| 26 | |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 27 | static struct rsnd_mod_ops adg_ops = { |
| 28 | .name = "adg", |
| 29 | }; |
| 30 | |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 31 | struct rsnd_adg { |
| 32 | struct clk *clk[CLKMAX]; |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 33 | struct clk *clkout[CLKOUTMAX]; |
| 34 | struct clk_onecell_data onecell; |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 35 | struct rsnd_mod mod; |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 36 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 37 | int rbga_rate_for_441khz; /* RBGA */ |
| 38 | int rbgb_rate_for_48khz; /* RBGB */ |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define for_each_rsnd_clk(pos, adg, i) \ |
Kuninori Morimoto | 00463c11 | 2014-02-11 17:15:51 -0800 | [diff] [blame] | 42 | for (i = 0; \ |
| 43 | (i < CLKMAX) && \ |
| 44 | ((pos) = adg->clk[i]); \ |
| 45 | i++) |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 46 | #define for_each_rsnd_clkout(pos, adg, i) \ |
| 47 | for (i = 0; \ |
| 48 | (i < CLKOUTMAX) && \ |
| 49 | ((pos) = adg->clkout[i]); \ |
| 50 | i++) |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 51 | #define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg) |
| 52 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 53 | static u32 rsnd_adg_calculate_rbgx(unsigned long div) |
| 54 | { |
| 55 | int i, ratio; |
| 56 | |
| 57 | if (!div) |
| 58 | return 0; |
| 59 | |
| 60 | for (i = 3; i >= 0; i--) { |
| 61 | ratio = 2 << (i * 2); |
| 62 | if (0 == (div % ratio)) |
| 63 | return (u32)((i << 8) | ((div / ratio) - 1)); |
| 64 | } |
| 65 | |
| 66 | return ~0; |
| 67 | } |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 68 | |
Kuninori Morimoto | 8467ded | 2014-03-02 23:43:33 -0800 | [diff] [blame] | 69 | static u32 rsnd_adg_ssi_ws_timing_gen2(struct rsnd_dai_stream *io) |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 70 | { |
Kuninori Morimoto | 8467ded | 2014-03-02 23:43:33 -0800 | [diff] [blame] | 71 | struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 72 | int id = rsnd_mod_id(mod); |
| 73 | int ws = id; |
| 74 | |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 75 | if (rsnd_ssi_is_pin_sharing(io)) { |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 76 | switch (id) { |
| 77 | case 1: |
| 78 | case 2: |
| 79 | ws = 0; |
| 80 | break; |
| 81 | case 4: |
| 82 | ws = 3; |
| 83 | break; |
| 84 | case 8: |
| 85 | ws = 7; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return (0x6 + ws) << 8; |
| 91 | } |
| 92 | |
Kuninori Morimoto | f708d94 | 2015-01-15 08:07:19 +0000 | [diff] [blame] | 93 | int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *mod, |
Kuninori Morimoto | bff58ea | 2014-05-08 17:44:49 -0700 | [diff] [blame] | 94 | struct rsnd_dai_stream *io) |
| 95 | { |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 96 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 97 | struct rsnd_adg *adg = rsnd_priv_to_adg(priv); |
| 98 | struct rsnd_mod *adg_mod = rsnd_mod_get(adg); |
Kuninori Morimoto | bff58ea | 2014-05-08 17:44:49 -0700 | [diff] [blame] | 99 | int id = rsnd_mod_id(mod); |
| 100 | int shift = (id % 2) ? 16 : 0; |
| 101 | u32 mask, val; |
| 102 | |
| 103 | val = rsnd_adg_ssi_ws_timing_gen2(io); |
| 104 | |
| 105 | val = val << shift; |
| 106 | mask = 0xffff << shift; |
| 107 | |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 108 | rsnd_mod_bset(adg_mod, CMDOUT_TIMSEL, mask, val); |
Kuninori Morimoto | bff58ea | 2014-05-08 17:44:49 -0700 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 113 | static int rsnd_adg_set_src_timsel_gen2(struct rsnd_mod *src_mod, |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 114 | struct rsnd_dai_stream *io, |
| 115 | u32 timsel) |
| 116 | { |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 117 | struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod); |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 118 | struct rsnd_adg *adg = rsnd_priv_to_adg(priv); |
| 119 | struct rsnd_mod *adg_mod = rsnd_mod_get(adg); |
Kuninori Morimoto | 985a4f6 | 2015-01-15 08:06:49 +0000 | [diff] [blame] | 120 | int is_play = rsnd_io_is_play(io); |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 121 | int id = rsnd_mod_id(src_mod); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 122 | int shift = (id % 2) ? 16 : 0; |
| 123 | u32 mask, ws; |
| 124 | u32 in, out; |
| 125 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 126 | rsnd_mod_confirm_src(src_mod); |
| 127 | |
Kuninori Morimoto | 8467ded | 2014-03-02 23:43:33 -0800 | [diff] [blame] | 128 | ws = rsnd_adg_ssi_ws_timing_gen2(io); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 129 | |
| 130 | in = (is_play) ? timsel : ws; |
| 131 | out = (is_play) ? ws : timsel; |
| 132 | |
| 133 | in = in << shift; |
| 134 | out = out << shift; |
| 135 | mask = 0xffff << shift; |
| 136 | |
| 137 | switch (id / 2) { |
| 138 | case 0: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 139 | rsnd_mod_bset(adg_mod, SRCIN_TIMSEL0, mask, in); |
| 140 | rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL0, mask, out); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 141 | break; |
| 142 | case 1: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 143 | rsnd_mod_bset(adg_mod, SRCIN_TIMSEL1, mask, in); |
| 144 | rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL1, mask, out); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 145 | break; |
| 146 | case 2: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 147 | rsnd_mod_bset(adg_mod, SRCIN_TIMSEL2, mask, in); |
| 148 | rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL2, mask, out); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 149 | break; |
| 150 | case 3: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 151 | rsnd_mod_bset(adg_mod, SRCIN_TIMSEL3, mask, in); |
| 152 | rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL3, mask, out); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 153 | break; |
| 154 | case 4: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 155 | rsnd_mod_bset(adg_mod, SRCIN_TIMSEL4, mask, in); |
| 156 | rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL4, mask, out); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 163 | int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *src_mod, |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 164 | struct rsnd_dai_stream *io, |
| 165 | unsigned int src_rate, |
| 166 | unsigned int dst_rate) |
| 167 | { |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 168 | struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 169 | struct rsnd_adg *adg = rsnd_priv_to_adg(priv); |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 170 | struct rsnd_mod *adg_mod = rsnd_mod_get(adg); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 171 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | ee2c828 | 2014-02-11 21:04:12 -0800 | [diff] [blame] | 172 | int idx, sel, div, step, ret; |
| 173 | u32 val, en; |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 174 | unsigned int min, diff; |
| 175 | unsigned int sel_rate [] = { |
| 176 | clk_get_rate(adg->clk[CLKA]), /* 0000: CLKA */ |
| 177 | clk_get_rate(adg->clk[CLKB]), /* 0001: CLKB */ |
| 178 | clk_get_rate(adg->clk[CLKC]), /* 0010: CLKC */ |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 179 | adg->rbga_rate_for_441khz, /* 0011: RBGA */ |
| 180 | adg->rbgb_rate_for_48khz, /* 0100: RBGB */ |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 183 | rsnd_mod_confirm_src(src_mod); |
| 184 | |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 185 | min = ~0; |
| 186 | val = 0; |
Kuninori Morimoto | ee2c828 | 2014-02-11 21:04:12 -0800 | [diff] [blame] | 187 | en = 0; |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 188 | for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) { |
| 189 | idx = 0; |
| 190 | step = 2; |
| 191 | |
| 192 | if (!sel_rate[sel]) |
| 193 | continue; |
| 194 | |
| 195 | for (div = 2; div <= 98304; div += step) { |
| 196 | diff = abs(src_rate - sel_rate[sel] / div); |
| 197 | if (min > diff) { |
| 198 | val = (sel << 8) | idx; |
| 199 | min = diff; |
Kuninori Morimoto | ee2c828 | 2014-02-11 21:04:12 -0800 | [diff] [blame] | 200 | en = 1 << (sel + 1); /* fixme */ |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * step of 0_0000 / 0_0001 / 0_1101 |
| 205 | * are out of order |
| 206 | */ |
| 207 | if ((idx > 2) && (idx % 2)) |
| 208 | step *= 2; |
| 209 | if (idx == 0x1c) { |
| 210 | div += step; |
| 211 | step *= 2; |
| 212 | } |
| 213 | idx++; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (min == ~0) { |
| 218 | dev_err(dev, "no Input clock\n"); |
| 219 | return -EIO; |
| 220 | } |
| 221 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 222 | ret = rsnd_adg_set_src_timsel_gen2(src_mod, io, val); |
Kuninori Morimoto | ee2c828 | 2014-02-11 21:04:12 -0800 | [diff] [blame] | 223 | if (ret < 0) { |
| 224 | dev_err(dev, "timsel error\n"); |
| 225 | return ret; |
| 226 | } |
| 227 | |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 228 | rsnd_mod_bset(adg_mod, DIV_EN, en, en); |
Kuninori Morimoto | ee2c828 | 2014-02-11 21:04:12 -0800 | [diff] [blame] | 229 | |
Kuninori Morimoto | d2c4b80 | 2015-03-19 04:14:45 +0000 | [diff] [blame] | 230 | dev_dbg(dev, "convert rate %d <-> %d\n", src_rate, dst_rate); |
| 231 | |
Kuninori Morimoto | ee2c828 | 2014-02-11 21:04:12 -0800 | [diff] [blame] | 232 | return 0; |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 235 | int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *src_mod, |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 236 | struct rsnd_dai_stream *io) |
| 237 | { |
Kuninori Morimoto | 8467ded | 2014-03-02 23:43:33 -0800 | [diff] [blame] | 238 | u32 val = rsnd_adg_ssi_ws_timing_gen2(io); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 239 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 240 | rsnd_mod_confirm_src(src_mod); |
| 241 | |
| 242 | return rsnd_adg_set_src_timsel_gen2(src_mod, io, val); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 245 | int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv, |
| 246 | struct rsnd_mod *mod, |
| 247 | unsigned int src_rate, |
| 248 | unsigned int dst_rate) |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 249 | { |
| 250 | struct rsnd_adg *adg = rsnd_priv_to_adg(priv); |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 251 | struct rsnd_mod *adg_mod = rsnd_mod_get(adg); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 252 | struct device *dev = rsnd_priv_to_dev(priv); |
| 253 | int idx, sel, div, shift; |
| 254 | u32 mask, val; |
| 255 | int id = rsnd_mod_id(mod); |
| 256 | unsigned int sel_rate [] = { |
| 257 | clk_get_rate(adg->clk[CLKA]), /* 000: CLKA */ |
| 258 | clk_get_rate(adg->clk[CLKB]), /* 001: CLKB */ |
| 259 | clk_get_rate(adg->clk[CLKC]), /* 010: CLKC */ |
| 260 | 0, /* 011: MLBCLK (not used) */ |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 261 | adg->rbga_rate_for_441khz, /* 100: RBGA */ |
| 262 | adg->rbgb_rate_for_48khz, /* 101: RBGB */ |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | /* find div (= 1/128, 1/256, 1/512, 1/1024, 1/2048 */ |
| 266 | for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) { |
| 267 | for (div = 128, idx = 0; |
| 268 | div <= 2048; |
| 269 | div *= 2, idx++) { |
| 270 | if (src_rate == sel_rate[sel] / div) { |
| 271 | val = (idx << 4) | sel; |
| 272 | goto find_rate; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | dev_err(dev, "can't find convert src clk\n"); |
| 277 | return -EINVAL; |
| 278 | |
| 279 | find_rate: |
| 280 | shift = (id % 4) * 8; |
| 281 | mask = 0xFF << shift; |
| 282 | val = val << shift; |
| 283 | |
| 284 | dev_dbg(dev, "adg convert src clk = %02x\n", val); |
| 285 | |
| 286 | switch (id / 4) { |
| 287 | case 0: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 288 | rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL3, mask, val); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 289 | break; |
| 290 | case 1: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 291 | rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL4, mask, val); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 292 | break; |
| 293 | case 2: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 294 | rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL5, mask, val); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Gen1 doesn't need dst_rate settings, |
| 300 | * since it uses SSI WS pin. |
| 301 | * see also rsnd_src_set_route_if_gen1() |
| 302 | */ |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 307 | static void rsnd_adg_set_ssi_clk(struct rsnd_mod *ssi_mod, u32 val) |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 308 | { |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 309 | struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod); |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 310 | struct rsnd_adg *adg = rsnd_priv_to_adg(priv); |
| 311 | struct rsnd_mod *adg_mod = rsnd_mod_get(adg); |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 312 | int id = rsnd_mod_id(ssi_mod); |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 313 | int shift = (id % 4) * 8; |
| 314 | u32 mask = 0xFF << shift; |
| 315 | |
Kuninori Morimoto | f1df122 | 2015-09-10 07:03:08 +0000 | [diff] [blame] | 316 | rsnd_mod_confirm_ssi(ssi_mod); |
| 317 | |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 318 | val = val << shift; |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * SSI 8 is not connected to ADG. |
| 322 | * it works with SSI 7 |
| 323 | */ |
| 324 | if (id == 8) |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 325 | return; |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 326 | |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 327 | switch (id / 4) { |
| 328 | case 0: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 329 | rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL0, mask, val); |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 330 | break; |
| 331 | case 1: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 332 | rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL1, mask, val); |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 333 | break; |
| 334 | case 2: |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 335 | rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL2, mask, val); |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 336 | break; |
| 337 | } |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod) |
| 341 | { |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 342 | /* |
| 343 | * "mod" = "ssi" here. |
| 344 | * we can get "ssi id" from mod |
| 345 | */ |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 346 | rsnd_adg_set_ssi_clk(mod, 0); |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate) |
| 352 | { |
| 353 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 354 | struct rsnd_adg *adg = rsnd_priv_to_adg(priv); |
| 355 | struct device *dev = rsnd_priv_to_dev(priv); |
| 356 | struct clk *clk; |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 357 | int i; |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 358 | u32 data; |
| 359 | int sel_table[] = { |
| 360 | [CLKA] = 0x1, |
| 361 | [CLKB] = 0x2, |
| 362 | [CLKC] = 0x3, |
| 363 | [CLKI] = 0x0, |
| 364 | }; |
| 365 | |
| 366 | dev_dbg(dev, "request clock = %d\n", rate); |
| 367 | |
| 368 | /* |
| 369 | * find suitable clock from |
| 370 | * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI. |
| 371 | */ |
| 372 | data = 0; |
| 373 | for_each_rsnd_clk(clk, adg, i) { |
| 374 | if (rate == clk_get_rate(clk)) { |
| 375 | data = sel_table[i]; |
| 376 | goto found_clock; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 381 | * find divided clock from BRGA/BRGB |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 382 | */ |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 383 | if (rate == adg->rbga_rate_for_441khz) { |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 384 | data = 0x10; |
| 385 | goto found_clock; |
| 386 | } |
| 387 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 388 | if (rate == adg->rbgb_rate_for_48khz) { |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 389 | data = 0x20; |
| 390 | goto found_clock; |
| 391 | } |
| 392 | |
| 393 | return -EIO; |
| 394 | |
| 395 | found_clock: |
| 396 | |
| 397 | /* |
| 398 | * This "mod" = "ssi" here. |
| 399 | * we can get "ssi id" from mod |
| 400 | */ |
Kuninori Morimoto | e337853 | 2013-12-19 19:26:31 -0800 | [diff] [blame] | 401 | rsnd_adg_set_ssi_clk(mod, data); |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 402 | |
Kuninori Morimoto | 3af6c3a | 2015-09-10 07:04:06 +0000 | [diff] [blame] | 403 | dev_dbg(dev, "ADG: %s[%d] selects 0x%x for %d\n", |
| 404 | rsnd_mod_name(mod), rsnd_mod_id(mod), |
| 405 | data, rate); |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
Kuninori Morimoto | 248e88c | 2015-09-10 07:04:24 +0000 | [diff] [blame] | 410 | static void rsnd_adg_get_clkin(struct rsnd_priv *priv, |
| 411 | struct rsnd_adg *adg) |
| 412 | { |
| 413 | struct device *dev = rsnd_priv_to_dev(priv); |
| 414 | struct clk *clk; |
| 415 | static const char * const clk_name[] = { |
| 416 | [CLKA] = "clk_a", |
| 417 | [CLKB] = "clk_b", |
| 418 | [CLKC] = "clk_c", |
| 419 | [CLKI] = "clk_i", |
| 420 | }; |
Kuninori Morimoto | 68a5502 | 2015-11-05 08:51:15 +0000 | [diff] [blame^] | 421 | int i, ret; |
Kuninori Morimoto | 248e88c | 2015-09-10 07:04:24 +0000 | [diff] [blame] | 422 | |
| 423 | for (i = 0; i < CLKMAX; i++) { |
| 424 | clk = devm_clk_get(dev, clk_name[i]); |
| 425 | adg->clk[i] = IS_ERR(clk) ? NULL : clk; |
| 426 | } |
| 427 | |
Kuninori Morimoto | 68a5502 | 2015-11-05 08:51:15 +0000 | [diff] [blame^] | 428 | for_each_rsnd_clk(clk, adg, i) { |
| 429 | ret = clk_prepare_enable(clk); |
| 430 | if (ret < 0) |
| 431 | dev_warn(dev, "can't use clk %d\n", i); |
| 432 | |
Kuninori Morimoto | 248e88c | 2015-09-10 07:04:24 +0000 | [diff] [blame] | 433 | dev_dbg(dev, "clk %d : %p : %ld\n", i, clk, clk_get_rate(clk)); |
Kuninori Morimoto | 68a5502 | 2015-11-05 08:51:15 +0000 | [diff] [blame^] | 434 | } |
Kuninori Morimoto | 248e88c | 2015-09-10 07:04:24 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 437 | static void rsnd_adg_get_clkout(struct rsnd_priv *priv, |
| 438 | struct rsnd_adg *adg) |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 439 | { |
| 440 | struct clk *clk; |
Kuninori Morimoto | 5c6901d | 2015-09-10 07:03:25 +0000 | [diff] [blame] | 441 | struct rsnd_mod *adg_mod = rsnd_mod_get(adg); |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 442 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 443 | struct device_node *np = dev->of_node; |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 444 | u32 ckr, rbgx, rbga, rbgb; |
Geert Uytterhoeven | 8b27418 | 2015-10-28 16:03:48 +0100 | [diff] [blame] | 445 | u32 rate, req_rate = 0, div; |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 446 | uint32_t count = 0; |
| 447 | unsigned long req_48kHz_rate, req_441kHz_rate; |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 448 | int i; |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 449 | const char *parent_clk_name = NULL; |
| 450 | static const char * const clkout_name[] = { |
| 451 | [CLKOUT] = "audio_clkout", |
| 452 | [CLKOUT1] = "audio_clkout1", |
| 453 | [CLKOUT2] = "audio_clkout2", |
| 454 | [CLKOUT3] = "audio_clkout3", |
| 455 | }; |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 456 | int brg_table[] = { |
| 457 | [CLKA] = 0x0, |
| 458 | [CLKB] = 0x1, |
| 459 | [CLKC] = 0x4, |
| 460 | [CLKI] = 0x2, |
| 461 | }; |
| 462 | |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 463 | of_property_read_u32(np, "#clock-cells", &count); |
| 464 | |
| 465 | /* |
| 466 | * ADG supports BRRA/BRRB output only |
| 467 | * this means all clkout0/1/2/3 will be same rate |
| 468 | */ |
| 469 | of_property_read_u32(np, "clock-frequency", &req_rate); |
| 470 | req_48kHz_rate = 0; |
| 471 | req_441kHz_rate = 0; |
| 472 | if (0 == (req_rate % 44100)) |
| 473 | req_441kHz_rate = req_rate; |
| 474 | if (0 == (req_rate % 48000)) |
| 475 | req_48kHz_rate = req_rate; |
| 476 | |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 477 | /* |
| 478 | * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC |
| 479 | * have 44.1kHz or 48kHz base clocks for now. |
| 480 | * |
| 481 | * SSI itself can divide parent clock by 1/1 - 1/16 |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 482 | * see |
| 483 | * rsnd_adg_ssi_clk_try_start() |
Kuninori Morimoto | 5c6901d | 2015-09-10 07:03:25 +0000 | [diff] [blame] | 484 | * rsnd_ssi_master_clk_start() |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 485 | */ |
| 486 | ckr = 0; |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 487 | rbga = 2; /* default 1/6 */ |
| 488 | rbgb = 2; /* default 1/6 */ |
| 489 | adg->rbga_rate_for_441khz = 0; |
| 490 | adg->rbgb_rate_for_48khz = 0; |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 491 | for_each_rsnd_clk(clk, adg, i) { |
| 492 | rate = clk_get_rate(clk); |
| 493 | |
| 494 | if (0 == rate) /* not used */ |
| 495 | continue; |
| 496 | |
| 497 | /* RBGA */ |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 498 | if (!adg->rbga_rate_for_441khz && (0 == rate % 44100)) { |
| 499 | div = 6; |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 500 | if (req_441kHz_rate) |
| 501 | div = rate / req_441kHz_rate; |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 502 | rbgx = rsnd_adg_calculate_rbgx(div); |
| 503 | if (BRRx_MASK(rbgx) == rbgx) { |
| 504 | rbga = rbgx; |
| 505 | adg->rbga_rate_for_441khz = rate / div; |
| 506 | ckr |= brg_table[i] << 20; |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 507 | if (req_441kHz_rate) |
| 508 | parent_clk_name = __clk_get_name(clk); |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 509 | } |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* RBGB */ |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 513 | if (!adg->rbgb_rate_for_48khz && (0 == rate % 48000)) { |
| 514 | div = 6; |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 515 | if (req_48kHz_rate) |
| 516 | div = rate / req_48kHz_rate; |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 517 | rbgx = rsnd_adg_calculate_rbgx(div); |
| 518 | if (BRRx_MASK(rbgx) == rbgx) { |
| 519 | rbgb = rbgx; |
| 520 | adg->rbgb_rate_for_48khz = rate / div; |
| 521 | ckr |= brg_table[i] << 16; |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 522 | if (req_48kHz_rate) { |
| 523 | parent_clk_name = __clk_get_name(clk); |
| 524 | ckr |= 0x80000000; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * ADG supports BRRA/BRRB output only. |
| 532 | * this means all clkout0/1/2/3 will be * same rate |
| 533 | */ |
| 534 | |
| 535 | /* |
| 536 | * for clkout |
| 537 | */ |
| 538 | if (!count) { |
Kuninori Morimoto | 462c30b | 2015-09-15 02:44:37 +0000 | [diff] [blame] | 539 | clk = clk_register_fixed_rate(dev, clkout_name[CLKOUT], |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 540 | parent_clk_name, |
| 541 | (parent_clk_name) ? |
| 542 | 0 : CLK_IS_ROOT, req_rate); |
| 543 | if (!IS_ERR(clk)) { |
| 544 | adg->clkout[CLKOUT] = clk; |
| 545 | of_clk_add_provider(np, of_clk_src_simple_get, clk); |
| 546 | } |
| 547 | } |
| 548 | /* |
| 549 | * for clkout0/1/2/3 |
| 550 | */ |
| 551 | else { |
| 552 | for (i = 0; i < CLKOUTMAX; i++) { |
| 553 | clk = clk_register_fixed_rate(dev, clkout_name[i], |
| 554 | parent_clk_name, |
| 555 | (parent_clk_name) ? |
| 556 | 0 : CLK_IS_ROOT, |
| 557 | req_rate); |
| 558 | if (!IS_ERR(clk)) { |
| 559 | adg->onecell.clks = adg->clkout; |
| 560 | adg->onecell.clk_num = CLKOUTMAX; |
| 561 | |
| 562 | adg->clkout[i] = clk; |
| 563 | |
| 564 | of_clk_add_provider(np, of_clk_src_onecell_get, |
| 565 | &adg->onecell); |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 566 | } |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Kuninori Morimoto | 5c6901d | 2015-09-10 07:03:25 +0000 | [diff] [blame] | 570 | rsnd_mod_bset(adg_mod, SSICKR, 0x00FF0000, ckr); |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 571 | rsnd_mod_write(adg_mod, BRRA, rbga); |
| 572 | rsnd_mod_write(adg_mod, BRRB, rbgb); |
| 573 | |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 574 | for_each_rsnd_clkout(clk, adg, i) |
| 575 | dev_dbg(dev, "clkout %d : %p : %ld\n", i, clk, clk_get_rate(clk)); |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 576 | dev_dbg(dev, "SSICKR = 0x%08x, BRRA/BRRB = 0x%x/0x%x\n", |
| 577 | ckr, rbga, rbgb); |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | int rsnd_adg_probe(struct platform_device *pdev, |
Kuninori Morimoto | 90e8e50 | 2014-03-17 19:29:55 -0700 | [diff] [blame] | 581 | const struct rsnd_of_data *of_data, |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 582 | struct rsnd_priv *priv) |
| 583 | { |
| 584 | struct rsnd_adg *adg; |
| 585 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 586 | |
| 587 | adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL); |
| 588 | if (!adg) { |
| 589 | dev_err(dev, "ADG allocate failed\n"); |
| 590 | return -ENOMEM; |
| 591 | } |
| 592 | |
Kuninori Morimoto | 1665a9e | 2015-09-10 07:02:39 +0000 | [diff] [blame] | 593 | /* |
| 594 | * ADG is special module. |
| 595 | * Use ADG mod without rsnd_mod_init() to make debug easy |
| 596 | * for rsnd_write/rsnd_read |
| 597 | */ |
| 598 | adg->mod.ops = &adg_ops; |
| 599 | adg->mod.priv = priv; |
| 600 | |
Kuninori Morimoto | 248e88c | 2015-09-10 07:04:24 +0000 | [diff] [blame] | 601 | rsnd_adg_get_clkin(priv, adg); |
Kuninori Morimoto | 2a46db4 | 2015-09-10 07:04:45 +0000 | [diff] [blame] | 602 | rsnd_adg_get_clkout(priv, adg); |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 603 | |
| 604 | priv->adg = adg; |
| 605 | |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 606 | return 0; |
| 607 | } |
Kuninori Morimoto | 68a5502 | 2015-11-05 08:51:15 +0000 | [diff] [blame^] | 608 | |
| 609 | void rsnd_adg_remove(struct platform_device *pdev, |
| 610 | struct rsnd_priv *priv) |
| 611 | { |
| 612 | struct rsnd_adg *adg = rsnd_priv_to_adg(priv); |
| 613 | struct clk *clk; |
| 614 | int i; |
| 615 | |
| 616 | for_each_rsnd_clk(clk, adg, i) { |
| 617 | clk_disable_unprepare(clk); |
| 618 | } |
| 619 | } |