Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mix.c |
| 3 | * |
| 4 | * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include "rsnd.h" |
| 11 | |
| 12 | #define MIX_NAME_SIZE 16 |
| 13 | #define MIX_NAME "mix" |
| 14 | |
| 15 | struct rsnd_mix { |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 16 | struct rsnd_mod mod; |
| 17 | }; |
| 18 | |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 19 | #define rsnd_mix_get(priv, id) ((struct rsnd_mix *)(priv->mix) + id) |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 20 | #define rsnd_mix_nr(priv) ((priv)->mix_nr) |
| 21 | #define for_each_rsnd_mix(pos, priv, i) \ |
| 22 | for ((i) = 0; \ |
| 23 | ((i) < rsnd_mix_nr(priv)) && \ |
| 24 | ((pos) = (struct rsnd_mix *)(priv)->mix + i); \ |
| 25 | i++) |
| 26 | |
Kuninori Morimoto | 4fe3252 | 2015-11-30 08:50:51 +0000 | [diff] [blame] | 27 | static void rsnd_mix_activation(struct rsnd_mod *mod) |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 28 | { |
| 29 | rsnd_mod_write(mod, MIX_SWRSR, 0); |
| 30 | rsnd_mod_write(mod, MIX_SWRSR, 1); |
| 31 | } |
| 32 | |
Kuninori Morimoto | 95e6b0d | 2015-11-30 08:51:52 +0000 | [diff] [blame^] | 33 | static void rsnd_mix_halt(struct rsnd_mod *mod) |
| 34 | { |
| 35 | rsnd_mod_write(mod, MIX_MIXIR, 1); |
| 36 | rsnd_mod_write(mod, MIX_SWRSR, 0); |
| 37 | } |
| 38 | |
Kuninori Morimoto | 13e0d17 | 2015-11-04 08:44:32 +0000 | [diff] [blame] | 39 | static void rsnd_mix_volume_parameter(struct rsnd_dai_stream *io, |
| 40 | struct rsnd_mod *mod) |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 41 | { |
Kuninori Morimoto | 13e0d17 | 2015-11-04 08:44:32 +0000 | [diff] [blame] | 42 | rsnd_mod_write(mod, MIX_MDBAR, 0); |
| 43 | rsnd_mod_write(mod, MIX_MDBBR, 0); |
| 44 | rsnd_mod_write(mod, MIX_MDBCR, 0); |
| 45 | rsnd_mod_write(mod, MIX_MDBDR, 0); |
| 46 | } |
| 47 | |
| 48 | static void rsnd_mix_volume_init(struct rsnd_dai_stream *io, |
| 49 | struct rsnd_mod *mod) |
| 50 | { |
| 51 | rsnd_mod_write(mod, MIX_MIXIR, 1); |
| 52 | |
| 53 | /* General Information */ |
| 54 | rsnd_mod_write(mod, MIX_ADINR, rsnd_get_adinr_chan(mod, io)); |
| 55 | |
| 56 | /* volume step */ |
| 57 | rsnd_mod_write(mod, MIX_MIXMR, 0); |
| 58 | rsnd_mod_write(mod, MIX_MVPDR, 0); |
| 59 | |
| 60 | /* common volume parameter */ |
| 61 | rsnd_mix_volume_parameter(io, mod); |
| 62 | |
| 63 | rsnd_mod_write(mod, MIX_MIXIR, 0); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void rsnd_mix_volume_update(struct rsnd_dai_stream *io, |
| 67 | struct rsnd_mod *mod) |
| 68 | { |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 69 | /* Disable MIX dB setting */ |
| 70 | rsnd_mod_write(mod, MIX_MDBER, 0); |
| 71 | |
Kuninori Morimoto | 13e0d17 | 2015-11-04 08:44:32 +0000 | [diff] [blame] | 72 | /* common volume parameter */ |
| 73 | rsnd_mix_volume_parameter(io, mod); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 74 | |
| 75 | /* Enable MIX dB setting */ |
| 76 | rsnd_mod_write(mod, MIX_MDBER, 1); |
| 77 | } |
| 78 | |
Kuninori Morimoto | 1b2ca0a | 2015-10-26 08:43:21 +0000 | [diff] [blame] | 79 | static int rsnd_mix_probe_(struct rsnd_mod *mod, |
| 80 | struct rsnd_dai_stream *io, |
| 81 | struct rsnd_priv *priv) |
| 82 | { |
| 83 | return rsnd_cmd_attach(io, rsnd_mod_id(mod)); |
| 84 | } |
| 85 | |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 86 | static int rsnd_mix_init(struct rsnd_mod *mod, |
| 87 | struct rsnd_dai_stream *io, |
| 88 | struct rsnd_priv *priv) |
| 89 | { |
Kuninori Morimoto | c992934 | 2015-10-22 03:15:04 +0000 | [diff] [blame] | 90 | rsnd_mod_power_on(mod); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 91 | |
Kuninori Morimoto | 4fe3252 | 2015-11-30 08:50:51 +0000 | [diff] [blame] | 92 | rsnd_mix_activation(mod); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 93 | |
Kuninori Morimoto | 13e0d17 | 2015-11-04 08:44:32 +0000 | [diff] [blame] | 94 | rsnd_mix_volume_init(io, mod); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 95 | |
| 96 | rsnd_mix_volume_update(io, mod); |
| 97 | |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int rsnd_mix_quit(struct rsnd_mod *mod, |
| 102 | struct rsnd_dai_stream *io, |
| 103 | struct rsnd_priv *priv) |
| 104 | { |
Kuninori Morimoto | 95e6b0d | 2015-11-30 08:51:52 +0000 | [diff] [blame^] | 105 | rsnd_mix_halt(mod); |
| 106 | |
Kuninori Morimoto | c992934 | 2015-10-22 03:15:04 +0000 | [diff] [blame] | 107 | rsnd_mod_power_off(mod); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static struct rsnd_mod_ops rsnd_mix_ops = { |
| 113 | .name = MIX_NAME, |
Kuninori Morimoto | 1b2ca0a | 2015-10-26 08:43:21 +0000 | [diff] [blame] | 114 | .probe = rsnd_mix_probe_, |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 115 | .init = rsnd_mix_init, |
| 116 | .quit = rsnd_mix_quit, |
| 117 | }; |
| 118 | |
| 119 | struct rsnd_mod *rsnd_mix_mod_get(struct rsnd_priv *priv, int id) |
| 120 | { |
| 121 | if (WARN_ON(id < 0 || id >= rsnd_mix_nr(priv))) |
| 122 | id = 0; |
| 123 | |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 124 | return rsnd_mod_get(rsnd_mix_get(priv, id)); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame] | 127 | int rsnd_mix_probe(struct rsnd_priv *priv) |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 128 | { |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 129 | struct device_node *node; |
| 130 | struct device_node *np; |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 131 | struct device *dev = rsnd_priv_to_dev(priv); |
| 132 | struct rsnd_mix *mix; |
| 133 | struct clk *clk; |
| 134 | char name[MIX_NAME_SIZE]; |
| 135 | int i, nr, ret; |
| 136 | |
| 137 | /* This driver doesn't support Gen1 at this point */ |
Kuninori Morimoto | 8a98b42 | 2015-10-15 03:25:28 +0000 | [diff] [blame] | 138 | if (rsnd_is_gen1(priv)) |
| 139 | return 0; |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 140 | |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 141 | node = rsnd_mix_of_node(priv); |
| 142 | if (!node) |
| 143 | return 0; /* not used is not error */ |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 144 | |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 145 | nr = of_get_child_count(node); |
| 146 | if (!nr) { |
| 147 | ret = -EINVAL; |
| 148 | goto rsnd_mix_probe_done; |
| 149 | } |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 150 | |
| 151 | mix = devm_kzalloc(dev, sizeof(*mix) * nr, GFP_KERNEL); |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 152 | if (!mix) { |
| 153 | ret = -ENOMEM; |
| 154 | goto rsnd_mix_probe_done; |
| 155 | } |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 156 | |
| 157 | priv->mix_nr = nr; |
| 158 | priv->mix = mix; |
| 159 | |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 160 | i = 0; |
| 161 | for_each_child_of_node(node, np) { |
| 162 | mix = rsnd_mix_get(priv, i); |
| 163 | |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 164 | snprintf(name, MIX_NAME_SIZE, "%s.%d", |
| 165 | MIX_NAME, i); |
| 166 | |
| 167 | clk = devm_clk_get(dev, name); |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 168 | if (IS_ERR(clk)) { |
| 169 | ret = PTR_ERR(clk); |
| 170 | goto rsnd_mix_probe_done; |
| 171 | } |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 172 | |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 173 | ret = rsnd_mod_init(priv, rsnd_mod_get(mix), &rsnd_mix_ops, |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 174 | clk, RSND_MOD_MIX, i); |
| 175 | if (ret) |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 176 | goto rsnd_mix_probe_done; |
| 177 | |
| 178 | i++; |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Kuninori Morimoto | c7fe4be | 2015-11-10 05:12:32 +0000 | [diff] [blame] | 181 | rsnd_mix_probe_done: |
| 182 | of_node_put(node); |
| 183 | |
| 184 | return ret; |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame] | 187 | void rsnd_mix_remove(struct rsnd_priv *priv) |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 188 | { |
| 189 | struct rsnd_mix *mix; |
| 190 | int i; |
| 191 | |
| 192 | for_each_rsnd_mix(mix, priv, i) { |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 193 | rsnd_mod_quit(rsnd_mod_get(mix)); |
Kuninori Morimoto | 70fb105 | 2015-07-15 07:17:36 +0000 | [diff] [blame] | 194 | } |
| 195 | } |