blob: b34957ab75b933e309239b61fc280853f5bfaa32 [file] [log] [blame]
Kuninori Morimoto70fb1052015-07-15 07:17:36 +00001/*
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
15struct rsnd_mix {
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000016 struct rsnd_mod mod;
17};
18
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +000019#define rsnd_mix_get(priv, id) ((struct rsnd_mix *)(priv->mix) + id)
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000020#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 Morimoto4fe32522015-11-30 08:50:51 +000027static void rsnd_mix_activation(struct rsnd_mod *mod)
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000028{
29 rsnd_mod_write(mod, MIX_SWRSR, 0);
30 rsnd_mod_write(mod, MIX_SWRSR, 1);
31}
32
Kuninori Morimoto95e6b0d2015-11-30 08:51:52 +000033static 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 Morimoto13e0d172015-11-04 08:44:32 +000039static void rsnd_mix_volume_parameter(struct rsnd_dai_stream *io,
40 struct rsnd_mod *mod)
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000041{
Kuninori Morimoto13e0d172015-11-04 08:44:32 +000042 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
48static 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 Morimoto70fb1052015-07-15 07:17:36 +000064}
65
66static void rsnd_mix_volume_update(struct rsnd_dai_stream *io,
67 struct rsnd_mod *mod)
68{
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000069 /* Disable MIX dB setting */
70 rsnd_mod_write(mod, MIX_MDBER, 0);
71
Kuninori Morimoto13e0d172015-11-04 08:44:32 +000072 /* common volume parameter */
73 rsnd_mix_volume_parameter(io, mod);
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000074
75 /* Enable MIX dB setting */
76 rsnd_mod_write(mod, MIX_MDBER, 1);
77}
78
Kuninori Morimoto1b2ca0a2015-10-26 08:43:21 +000079static 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 Morimoto70fb1052015-07-15 07:17:36 +000086static int rsnd_mix_init(struct rsnd_mod *mod,
87 struct rsnd_dai_stream *io,
88 struct rsnd_priv *priv)
89{
Kuninori Morimotoc9929342015-10-22 03:15:04 +000090 rsnd_mod_power_on(mod);
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000091
Kuninori Morimoto4fe32522015-11-30 08:50:51 +000092 rsnd_mix_activation(mod);
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000093
Kuninori Morimoto13e0d172015-11-04 08:44:32 +000094 rsnd_mix_volume_init(io, mod);
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000095
96 rsnd_mix_volume_update(io, mod);
97
Kuninori Morimoto70fb1052015-07-15 07:17:36 +000098 return 0;
99}
100
101static int rsnd_mix_quit(struct rsnd_mod *mod,
102 struct rsnd_dai_stream *io,
103 struct rsnd_priv *priv)
104{
Kuninori Morimoto95e6b0d2015-11-30 08:51:52 +0000105 rsnd_mix_halt(mod);
106
Kuninori Morimotoc9929342015-10-22 03:15:04 +0000107 rsnd_mod_power_off(mod);
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000108
109 return 0;
110}
111
112static struct rsnd_mod_ops rsnd_mix_ops = {
113 .name = MIX_NAME,
Kuninori Morimoto1b2ca0a2015-10-26 08:43:21 +0000114 .probe = rsnd_mix_probe_,
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000115 .init = rsnd_mix_init,
116 .quit = rsnd_mix_quit,
117};
118
119struct 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 Morimotoc7fe4be2015-11-10 05:12:32 +0000124 return rsnd_mod_get(rsnd_mix_get(priv, id));
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000125}
126
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000127int rsnd_mix_probe(struct rsnd_priv *priv)
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000128{
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000129 struct device_node *node;
130 struct device_node *np;
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000131 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 Morimoto8a98b422015-10-15 03:25:28 +0000138 if (rsnd_is_gen1(priv))
139 return 0;
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000140
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000141 node = rsnd_mix_of_node(priv);
142 if (!node)
143 return 0; /* not used is not error */
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000144
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000145 nr = of_get_child_count(node);
146 if (!nr) {
147 ret = -EINVAL;
148 goto rsnd_mix_probe_done;
149 }
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000150
151 mix = devm_kzalloc(dev, sizeof(*mix) * nr, GFP_KERNEL);
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000152 if (!mix) {
153 ret = -ENOMEM;
154 goto rsnd_mix_probe_done;
155 }
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000156
157 priv->mix_nr = nr;
158 priv->mix = mix;
159
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000160 i = 0;
161 for_each_child_of_node(node, np) {
162 mix = rsnd_mix_get(priv, i);
163
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000164 snprintf(name, MIX_NAME_SIZE, "%s.%d",
165 MIX_NAME, i);
166
167 clk = devm_clk_get(dev, name);
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000168 if (IS_ERR(clk)) {
169 ret = PTR_ERR(clk);
170 goto rsnd_mix_probe_done;
171 }
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000172
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000173 ret = rsnd_mod_init(priv, rsnd_mod_get(mix), &rsnd_mix_ops,
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000174 clk, RSND_MOD_MIX, i);
175 if (ret)
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000176 goto rsnd_mix_probe_done;
177
178 i++;
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000179 }
180
Kuninori Morimotoc7fe4be2015-11-10 05:12:32 +0000181rsnd_mix_probe_done:
182 of_node_put(node);
183
184 return ret;
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000185}
186
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000187void rsnd_mix_remove(struct rsnd_priv *priv)
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000188{
189 struct rsnd_mix *mix;
190 int i;
191
192 for_each_rsnd_mix(mix, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000193 rsnd_mod_quit(rsnd_mod_get(mix));
Kuninori Morimoto70fb1052015-07-15 07:17:36 +0000194 }
195}