ctu.c 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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"

struct rsnd_ctu {
	struct rsnd_mod mod;
};

#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++)

26
#define rsnd_ctu_get(priv, id) ((struct rsnd_ctu *)(priv->ctu) + id)
27 28 29 30 31 32 33

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

34 35 36 37 38 39 40
#define rsnd_ctu_initialize_lock(mod)	__rsnd_ctu_initialize_lock(mod, 1)
#define rsnd_ctu_initialize_unlock(mod)	__rsnd_ctu_initialize_lock(mod, 0)
static void __rsnd_ctu_initialize_lock(struct rsnd_mod *mod, u32 enable)
{
	rsnd_mod_write(mod, CTU_CTUIR, enable);
}

41 42 43 44 45 46 47
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);
}

48 49 50 51
static int rsnd_ctu_init(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
{
52
	rsnd_mod_power_on(mod);
53

54 55
	rsnd_ctu_activation(mod);

56 57 58 59 60 61 62 63 64 65 66 67 68
	rsnd_ctu_initialize_lock(mod);

	rsnd_mod_write(mod, CTU_ADINR, rsnd_get_adinr_chan(mod, io));

	rsnd_ctu_initialize_unlock(mod);

	return 0;
}

static int rsnd_ctu_quit(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
{
69
	rsnd_mod_power_off(mod);
70 71 72 73 74 75

	return 0;
}

static struct rsnd_mod_ops rsnd_ctu_ops = {
	.name		= CTU_NAME,
76
	.probe		= rsnd_ctu_probe_,
77 78 79 80 81 82 83 84 85
	.init		= rsnd_ctu_init,
	.quit		= rsnd_ctu_quit,
};

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;

86
	return rsnd_mod_get(rsnd_ctu_get(priv, id));
87 88
}

89
int rsnd_ctu_probe(struct rsnd_priv *priv)
90
{
91 92
	struct device_node *node;
	struct device_node *np;
93 94 95 96 97 98 99
	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 */
100 101
	if (rsnd_is_gen1(priv))
		return 0;
102

103 104 105
	node = rsnd_ctu_of_node(priv);
	if (!node)
		return 0; /* not used is not error */
106

107 108 109 110 111
	nr = of_get_child_count(node);
	if (!nr) {
		ret = -EINVAL;
		goto rsnd_ctu_probe_done;
	}
112 113

	ctu = devm_kzalloc(dev, sizeof(*ctu) * nr, GFP_KERNEL);
114 115 116 117
	if (!ctu) {
		ret = -ENOMEM;
		goto rsnd_ctu_probe_done;
	}
118 119 120 121

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

122
	i = 0;
123
	ret = 0;
124 125 126
	for_each_child_of_node(node, np) {
		ctu = rsnd_ctu_get(priv, i);

127 128 129 130 131 132 133 134
		/*
		 * 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);
135 136 137 138
		if (IS_ERR(clk)) {
			ret = PTR_ERR(clk);
			goto rsnd_ctu_probe_done;
		}
139

140
		ret = rsnd_mod_init(priv, rsnd_mod_get(ctu), &rsnd_ctu_ops,
141
				    clk, rsnd_mod_get_status, RSND_MOD_CTU, i);
142
		if (ret)
143 144 145
			goto rsnd_ctu_probe_done;

		i++;
146 147
	}

148 149 150 151 152

rsnd_ctu_probe_done:
	of_node_put(node);

	return ret;
153 154
}

155
void rsnd_ctu_remove(struct rsnd_priv *priv)
156 157 158 159 160
{
	struct rsnd_ctu *ctu;
	int i;

	for_each_rsnd_ctu(ctu, priv, i) {
161
		rsnd_mod_quit(rsnd_mod_get(ctu));
162 163
	}
}