ctu.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * ctu.c
 *
 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include "rsnd.h"

#define CTU_NAME_SIZE	16
#define CTU_NAME "ctu"

K
Kuninori Morimoto 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/*
 * User needs to setup CTU by amixer, and its settings are
 * based on below registers
 *
 * CTUn_CPMDR : amixser set "CTU Pass"
 * CTUn_SV0xR : amixser set "CTU SV0"
 * CTUn_SV1xR : amixser set "CTU SV1"
 * CTUn_SV2xR : amixser set "CTU SV2"
 * CTUn_SV3xR : amixser set "CTU SV3"
 *
 * [CTU Pass]
 * 0000: default
 * 0001: Connect input data of channel 0
 * 0010: Connect input data of channel 1
 * 0011: Connect input data of channel 2
 * 0100: Connect input data of channel 3
 * 0101: Connect input data of channel 4
 * 0110: Connect input data of channel 5
 * 0111: Connect input data of channel 6
 * 1000: Connect input data of channel 7
 * 1001: Connect calculated data by scale values of matrix row 0
 * 1010: Connect calculated data by scale values of matrix row 1
 * 1011: Connect calculated data by scale values of matrix row 2
 * 1100: Connect calculated data by scale values of matrix row 3
 *
 * [CTU SVx]
 * [Output0] = [SV00, SV01, SV02, SV03, SV04, SV05, SV06, SV07]
 * [Output1] = [SV10, SV11, SV12, SV13, SV14, SV15, SV16, SV17]
 * [Output2] = [SV20, SV21, SV22, SV23, SV24, SV25, SV26, SV27]
 * [Output3] = [SV30, SV31, SV32, SV33, SV34, SV35, SV36, SV37]
 * [Output4] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
 * [Output5] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
 * [Output6] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
 * [Output7] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
 *
 * [SVxx]
 * Plus					Minus
 * value	time		dB	value		time		dB
 * -----------------------------------------------------------------------
 * H'7F_FFFF	2		6	H'80_0000	2		6
 * ...
 * H'40_0000	1		0	H'C0_0000	1		0
 * ...
 * H'00_0001	2.38 x 10^-7	-132
 * H'00_0000	0		Mute	H'FF_FFFF	2.38 x 10^-7	-132
 *
 *
 * Ex) Input ch -> Output ch
 *	1ch     ->  0ch
 *	0ch     ->  1ch
 *
 *	amixer set "CTU Reset" on
 *	amixer set "CTU Pass" 9,10
 *	amixer set "CTU SV0" 0,4194304
 *	amixer set "CTU SV1" 4194304,0
 * or
 *	amixer set "CTU Reset" on
 *	amixer set "CTU Pass" 2,1
 */

75 76
struct rsnd_ctu {
	struct rsnd_mod mod;
K
Kuninori Morimoto 已提交
77 78 79 80 81 82
	struct rsnd_kctrl_cfg_m pass;
	struct rsnd_kctrl_cfg_m sv0;
	struct rsnd_kctrl_cfg_m sv1;
	struct rsnd_kctrl_cfg_m sv2;
	struct rsnd_kctrl_cfg_m sv3;
	struct rsnd_kctrl_cfg_s reset;
83
	int channels;
84
	u32 flags;
85 86
};

87 88
#define KCTRL_INITIALIZED	(1 << 0)

89 90 91 92 93 94 95
#define rsnd_ctu_nr(priv) ((priv)->ctu_nr)
#define for_each_rsnd_ctu(pos, priv, i)					\
	for ((i) = 0;							\
	     ((i) < rsnd_ctu_nr(priv)) &&				\
		     ((pos) = (struct rsnd_ctu *)(priv)->ctu + i);	\
	     i++)

96 97 98
#define rsnd_mod_to_ctu(_mod)	\
	container_of((_mod), struct rsnd_ctu, mod)

99
#define rsnd_ctu_get(priv, id) ((struct rsnd_ctu *)(priv->ctu) + id)
100 101 102 103 104 105 106

static void rsnd_ctu_activation(struct rsnd_mod *mod)
{
	rsnd_mod_write(mod, CTU_SWRSR, 0);
	rsnd_mod_write(mod, CTU_SWRSR, 1);
}

107 108 109 110 111 112
static void rsnd_ctu_halt(struct rsnd_mod *mod)
{
	rsnd_mod_write(mod, CTU_CTUIR, 1);
	rsnd_mod_write(mod, CTU_SWRSR, 0);
}

113 114 115 116 117 118 119
int rsnd_ctu_converted_channel(struct rsnd_mod *mod)
{
	struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);

	return ctu->channels;
}

120 121 122 123 124 125 126
static int rsnd_ctu_probe_(struct rsnd_mod *mod,
			   struct rsnd_dai_stream *io,
			   struct rsnd_priv *priv)
{
	return rsnd_cmd_attach(io, rsnd_mod_id(mod) / 4);
}

127 128 129
static void rsnd_ctu_value_init(struct rsnd_dai_stream *io,
			       struct rsnd_mod *mod)
{
K
Kuninori Morimoto 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
	struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);
	u32 cpmdr = 0;
	u32 scmdr = 0;
	int i;

	for (i = 0; i < RSND_MAX_CHANNELS; i++) {
		u32 val = ctu->pass.val[i];

		cpmdr |= val << (28 - (i * 4));

		if ((val > 0x8) && (scmdr < (val - 0x8)))
			scmdr = val - 0x8;
	}

144 145
	rsnd_mod_write(mod, CTU_CTUIR, 1);

146
	rsnd_mod_write(mod, CTU_ADINR, rsnd_runtime_channel_original(io));
147

K
Kuninori Morimoto 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	rsnd_mod_write(mod, CTU_CPMDR, cpmdr);

	rsnd_mod_write(mod, CTU_SCMDR, scmdr);

	if (scmdr > 0) {
		rsnd_mod_write(mod, CTU_SV00R, ctu->sv0.val[0]);
		rsnd_mod_write(mod, CTU_SV01R, ctu->sv0.val[1]);
		rsnd_mod_write(mod, CTU_SV02R, ctu->sv0.val[2]);
		rsnd_mod_write(mod, CTU_SV03R, ctu->sv0.val[3]);
		rsnd_mod_write(mod, CTU_SV04R, ctu->sv0.val[4]);
		rsnd_mod_write(mod, CTU_SV05R, ctu->sv0.val[5]);
		rsnd_mod_write(mod, CTU_SV06R, ctu->sv0.val[6]);
		rsnd_mod_write(mod, CTU_SV07R, ctu->sv0.val[7]);
	}
	if (scmdr > 1) {
		rsnd_mod_write(mod, CTU_SV10R, ctu->sv1.val[0]);
		rsnd_mod_write(mod, CTU_SV11R, ctu->sv1.val[1]);
		rsnd_mod_write(mod, CTU_SV12R, ctu->sv1.val[2]);
		rsnd_mod_write(mod, CTU_SV13R, ctu->sv1.val[3]);
		rsnd_mod_write(mod, CTU_SV14R, ctu->sv1.val[4]);
		rsnd_mod_write(mod, CTU_SV15R, ctu->sv1.val[5]);
		rsnd_mod_write(mod, CTU_SV16R, ctu->sv1.val[6]);
		rsnd_mod_write(mod, CTU_SV17R, ctu->sv1.val[7]);
	}
	if (scmdr > 2) {
		rsnd_mod_write(mod, CTU_SV20R, ctu->sv2.val[0]);
		rsnd_mod_write(mod, CTU_SV21R, ctu->sv2.val[1]);
		rsnd_mod_write(mod, CTU_SV22R, ctu->sv2.val[2]);
		rsnd_mod_write(mod, CTU_SV23R, ctu->sv2.val[3]);
		rsnd_mod_write(mod, CTU_SV24R, ctu->sv2.val[4]);
		rsnd_mod_write(mod, CTU_SV25R, ctu->sv2.val[5]);
		rsnd_mod_write(mod, CTU_SV26R, ctu->sv2.val[6]);
		rsnd_mod_write(mod, CTU_SV27R, ctu->sv2.val[7]);
	}
	if (scmdr > 3) {
		rsnd_mod_write(mod, CTU_SV30R, ctu->sv3.val[0]);
		rsnd_mod_write(mod, CTU_SV31R, ctu->sv3.val[1]);
		rsnd_mod_write(mod, CTU_SV32R, ctu->sv3.val[2]);
		rsnd_mod_write(mod, CTU_SV33R, ctu->sv3.val[3]);
		rsnd_mod_write(mod, CTU_SV34R, ctu->sv3.val[4]);
		rsnd_mod_write(mod, CTU_SV35R, ctu->sv3.val[5]);
		rsnd_mod_write(mod, CTU_SV36R, ctu->sv3.val[6]);
		rsnd_mod_write(mod, CTU_SV37R, ctu->sv3.val[7]);
	}
192 193 194 195

	rsnd_mod_write(mod, CTU_CTUIR, 0);
}

K
Kuninori Morimoto 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
static void rsnd_ctu_value_reset(struct rsnd_dai_stream *io,
				 struct rsnd_mod *mod)
{
	struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);
	int i;

	if (!ctu->reset.val)
		return;

	for (i = 0; i < RSND_MAX_CHANNELS; i++) {
		ctu->pass.val[i] = 0;
		ctu->sv0.val[i] = 0;
		ctu->sv1.val[i] = 0;
		ctu->sv2.val[i] = 0;
		ctu->sv3.val[i] = 0;
	}
	ctu->reset.val = 0;
}

215 216 217 218
static int rsnd_ctu_init(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
{
219
	rsnd_mod_power_on(mod);
220

221 222
	rsnd_ctu_activation(mod);

223
	rsnd_ctu_value_init(io, mod);
224 225 226 227 228 229 230 231

	return 0;
}

static int rsnd_ctu_quit(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
{
232 233
	rsnd_ctu_halt(mod);

234
	rsnd_mod_power_off(mod);
235 236 237 238

	return 0;
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static int rsnd_ctu_hw_params(struct rsnd_mod *mod,
			      struct rsnd_dai_stream *io,
			      struct snd_pcm_substream *substream,
			      struct snd_pcm_hw_params *fe_params)
{
	struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);
	struct snd_soc_pcm_runtime *fe = substream->private_data;

	/*
	 * CTU assumes that it is used under DPCM if user want to use
	 * channel transfer. Then, CTU should be FE.
	 * And then, this function will be called *after* BE settings.
	 * this means, each BE already has fixuped hw_params.
	 * see
	 *	dpcm_fe_dai_hw_params()
	 *	dpcm_be_dai_hw_params()
	 */
	ctu->channels = 0;
	if (fe->dai_link->dynamic) {
		struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
		struct device *dev = rsnd_priv_to_dev(priv);
		struct snd_soc_dpcm *dpcm;
		struct snd_pcm_hw_params *be_params;
		int stream = substream->stream;

		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
			be_params = &dpcm->hw_params;
			if (params_channels(fe_params) != params_channels(be_params))
				ctu->channels = params_channels(be_params);
		}

		dev_dbg(dev, "CTU convert channels %d\n", ctu->channels);
	}

	return 0;
}

K
Kuninori Morimoto 已提交
276 277 278 279 280 281 282
static int rsnd_ctu_pcm_new(struct rsnd_mod *mod,
			    struct rsnd_dai_stream *io,
			    struct snd_soc_pcm_runtime *rtd)
{
	struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);
	int ret;

283 284 285
	if (rsnd_flags_has(ctu, KCTRL_INITIALIZED))
		return 0;

K
Kuninori Morimoto 已提交
286 287
	/* CTU Pass */
	ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU Pass",
288
			       rsnd_kctrl_accept_anytime,
K
Kuninori Morimoto 已提交
289 290 291 292 293 294
			       NULL,
			       &ctu->pass, RSND_MAX_CHANNELS,
			       0xC);

	/* ROW0 */
	ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV0",
295
			       rsnd_kctrl_accept_anytime,
K
Kuninori Morimoto 已提交
296 297 298 299 300 301 302 303
			       NULL,
			       &ctu->sv0, RSND_MAX_CHANNELS,
			       0x00FFFFFF);
	if (ret < 0)
		return ret;

	/* ROW1 */
	ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV1",
304
			       rsnd_kctrl_accept_anytime,
K
Kuninori Morimoto 已提交
305 306 307 308 309 310 311 312
			       NULL,
			       &ctu->sv1, RSND_MAX_CHANNELS,
			       0x00FFFFFF);
	if (ret < 0)
		return ret;

	/* ROW2 */
	ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV2",
313
			       rsnd_kctrl_accept_anytime,
K
Kuninori Morimoto 已提交
314 315 316 317 318 319 320 321
			       NULL,
			       &ctu->sv2, RSND_MAX_CHANNELS,
			       0x00FFFFFF);
	if (ret < 0)
		return ret;

	/* ROW3 */
	ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV3",
322
			       rsnd_kctrl_accept_anytime,
K
Kuninori Morimoto 已提交
323 324 325 326 327 328 329 330
			       NULL,
			       &ctu->sv3, RSND_MAX_CHANNELS,
			       0x00FFFFFF);
	if (ret < 0)
		return ret;

	/* Reset */
	ret = rsnd_kctrl_new_s(mod, io, rtd, "CTU Reset",
331
			       rsnd_kctrl_accept_anytime,
K
Kuninori Morimoto 已提交
332 333 334
			       rsnd_ctu_value_reset,
			       &ctu->reset, 1);

335 336
	rsnd_flags_set(ctu, KCTRL_INITIALIZED);

K
Kuninori Morimoto 已提交
337 338 339
	return ret;
}

340 341
static struct rsnd_mod_ops rsnd_ctu_ops = {
	.name		= CTU_NAME,
342
	.probe		= rsnd_ctu_probe_,
343 344
	.init		= rsnd_ctu_init,
	.quit		= rsnd_ctu_quit,
345
	.hw_params	= rsnd_ctu_hw_params,
K
Kuninori Morimoto 已提交
346
	.pcm_new	= rsnd_ctu_pcm_new,
347 348 349 350 351 352 353
};

struct rsnd_mod *rsnd_ctu_mod_get(struct rsnd_priv *priv, int id)
{
	if (WARN_ON(id < 0 || id >= rsnd_ctu_nr(priv)))
		id = 0;

354
	return rsnd_mod_get(rsnd_ctu_get(priv, id));
355 356
}

357
int rsnd_ctu_probe(struct rsnd_priv *priv)
358
{
359 360
	struct device_node *node;
	struct device_node *np;
361 362 363 364 365 366 367
	struct device *dev = rsnd_priv_to_dev(priv);
	struct rsnd_ctu *ctu;
	struct clk *clk;
	char name[CTU_NAME_SIZE];
	int i, nr, ret;

	/* This driver doesn't support Gen1 at this point */
368 369
	if (rsnd_is_gen1(priv))
		return 0;
370

371 372 373
	node = rsnd_ctu_of_node(priv);
	if (!node)
		return 0; /* not used is not error */
374

375 376 377 378 379
	nr = of_get_child_count(node);
	if (!nr) {
		ret = -EINVAL;
		goto rsnd_ctu_probe_done;
	}
380 381

	ctu = devm_kzalloc(dev, sizeof(*ctu) * nr, GFP_KERNEL);
382 383 384 385
	if (!ctu) {
		ret = -ENOMEM;
		goto rsnd_ctu_probe_done;
	}
386 387 388 389

	priv->ctu_nr	= nr;
	priv->ctu	= ctu;

390
	i = 0;
391
	ret = 0;
392 393 394
	for_each_child_of_node(node, np) {
		ctu = rsnd_ctu_get(priv, i);

395 396 397 398 399 400 401 402
		/*
		 * CTU00, CTU01, CTU02, CTU03 => CTU0
		 * CTU10, CTU11, CTU12, CTU13 => CTU1
		 */
		snprintf(name, CTU_NAME_SIZE, "%s.%d",
			 CTU_NAME, i / 4);

		clk = devm_clk_get(dev, name);
403 404
		if (IS_ERR(clk)) {
			ret = PTR_ERR(clk);
405
			of_node_put(np);
406 407
			goto rsnd_ctu_probe_done;
		}
408

409
		ret = rsnd_mod_init(priv, rsnd_mod_get(ctu), &rsnd_ctu_ops,
410
				    clk, rsnd_mod_get_status, RSND_MOD_CTU, i);
411 412
		if (ret) {
			of_node_put(np);
413
			goto rsnd_ctu_probe_done;
414
		}
415 416

		i++;
417 418
	}

419 420 421 422 423

rsnd_ctu_probe_done:
	of_node_put(node);

	return ret;
424 425
}

426
void rsnd_ctu_remove(struct rsnd_priv *priv)
427 428 429 430 431
{
	struct rsnd_ctu *ctu;
	int i;

	for_each_rsnd_ctu(ctu, priv, i) {
432
		rsnd_mod_quit(rsnd_mod_get(ctu));
433 434
	}
}