xref: /openbmc/linux/sound/soc/sh/rcar/cmd.c (revision cd4d09ec)
1 /*
2  * Renesas R-Car CMD support
3  *
4  * Copyright (C) 2015 Renesas Solutions Corp.
5  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include "rsnd.h"
12 
13 struct rsnd_cmd {
14 	struct rsnd_mod mod;
15 };
16 
17 #define CMD_NAME "cmd"
18 
19 #define rsnd_cmd_nr(priv) ((priv)->cmd_nr)
20 #define for_each_rsnd_cmd(pos, priv, i)					\
21 	for ((i) = 0;							\
22 	     ((i) < rsnd_cmd_nr(priv)) &&				\
23 		     ((pos) = (struct rsnd_cmd *)(priv)->cmd + i);	\
24 	     i++)
25 
26 static int rsnd_cmd_init(struct rsnd_mod *mod,
27 			 struct rsnd_dai_stream *io,
28 			 struct rsnd_priv *priv)
29 {
30 	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
31 	struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
32 	struct rsnd_mod *src = rsnd_io_to_mod_src(io);
33 	struct device *dev = rsnd_priv_to_dev(priv);
34 	u32 data;
35 
36 	if (!mix && !dvc)
37 		return 0;
38 
39 	if (mix) {
40 		struct rsnd_dai *rdai;
41 		int i;
42 		u32 path[] = {
43 			[0] = 0,
44 			[1] = 1 << 0,
45 			[2] = 0,
46 			[3] = 0,
47 			[4] = 0,
48 			[5] = 1 << 8
49 		};
50 
51 		/*
52 		 * it is assuming that integrater is well understanding about
53 		 * data path. Here doesn't check impossible connection,
54 		 * like src2 + src5
55 		 */
56 		data = 0;
57 		for_each_rsnd_dai(rdai, priv, i) {
58 			io = &rdai->playback;
59 			if (mix == rsnd_io_to_mod_mix(io))
60 				data |= path[rsnd_mod_id(src)];
61 
62 			io = &rdai->capture;
63 			if (mix == rsnd_io_to_mod_mix(io))
64 				data |= path[rsnd_mod_id(src)];
65 		}
66 
67 	} else {
68 		u32 path[] = {
69 			[0] = 0x30000,
70 			[1] = 0x30001,
71 			[2] = 0x40000,
72 			[3] = 0x10000,
73 			[4] = 0x20000,
74 			[5] = 0x40100
75 		};
76 
77 		data = path[rsnd_mod_id(src)];
78 	}
79 
80 	dev_dbg(dev, "ctu/mix path = 0x%08x", data);
81 
82 	rsnd_mod_write(mod, CMD_ROUTE_SLCT, data);
83 	rsnd_mod_write(mod, CMD_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
84 
85 	rsnd_adg_set_cmd_timsel_gen2(mod, io);
86 
87 	return 0;
88 }
89 
90 static int rsnd_cmd_start(struct rsnd_mod *mod,
91 			  struct rsnd_dai_stream *io,
92 			  struct rsnd_priv *priv)
93 {
94 	rsnd_mod_write(mod, CMD_CTRL, 0x10);
95 
96 	return 0;
97 }
98 
99 static int rsnd_cmd_stop(struct rsnd_mod *mod,
100 			 struct rsnd_dai_stream *io,
101 			 struct rsnd_priv *priv)
102 {
103 	rsnd_mod_write(mod, CMD_CTRL, 0);
104 
105 	return 0;
106 }
107 
108 static struct rsnd_mod_ops rsnd_cmd_ops = {
109 	.name	= CMD_NAME,
110 	.init	= rsnd_cmd_init,
111 	.start	= rsnd_cmd_start,
112 	.stop	= rsnd_cmd_stop,
113 };
114 
115 int rsnd_cmd_attach(struct rsnd_dai_stream *io, int id)
116 {
117 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
118 	struct rsnd_mod *mod = rsnd_cmd_mod_get(priv, id);
119 
120 	return rsnd_dai_connect(mod, io, mod->type);
121 }
122 
123 struct rsnd_mod *rsnd_cmd_mod_get(struct rsnd_priv *priv, int id)
124 {
125 	if (WARN_ON(id < 0 || id >= rsnd_cmd_nr(priv)))
126 		id = 0;
127 
128 	return rsnd_mod_get((struct rsnd_cmd *)(priv->cmd) + id);
129 }
130 
131 int rsnd_cmd_probe(struct rsnd_priv *priv)
132 {
133 	struct device *dev = rsnd_priv_to_dev(priv);
134 	struct rsnd_cmd *cmd;
135 	int i, nr, ret;
136 
137 	/* This driver doesn't support Gen1 at this point */
138 	if (rsnd_is_gen1(priv))
139 		return 0;
140 
141 	/* same number as DVC */
142 	nr = priv->dvc_nr;
143 	if (!nr)
144 		return 0;
145 
146 	cmd = devm_kzalloc(dev, sizeof(*cmd) * nr, GFP_KERNEL);
147 	if (!cmd)
148 		return -ENOMEM;
149 
150 	priv->cmd_nr	= nr;
151 	priv->cmd	= cmd;
152 
153 	for_each_rsnd_cmd(cmd, priv, i) {
154 		ret = rsnd_mod_init(priv, rsnd_mod_get(cmd),
155 				    &rsnd_cmd_ops, NULL, RSND_MOD_CMD, i);
156 		if (ret)
157 			return ret;
158 	}
159 
160 	return 0;
161 }
162 
163 void rsnd_cmd_remove(struct rsnd_priv *priv)
164 {
165 	struct rsnd_cmd *cmd;
166 	int i;
167 
168 	for_each_rsnd_cmd(cmd, priv, i) {
169 		rsnd_mod_quit(rsnd_mod_get(cmd));
170 	}
171 }
172