ASoC: rsnd: use mod base common method on CMD
[deliverable/linux.git] / sound / soc / sh / rcar / ctu.c
1 /*
2 * ctu.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 CTU_NAME_SIZE 16
13 #define CTU_NAME "ctu"
14
15 struct rsnd_ctu {
16 struct rsnd_ctu_platform_info *info; /* rcar_snd.h */
17 struct rsnd_mod mod;
18 };
19
20 #define rsnd_ctu_nr(priv) ((priv)->ctu_nr)
21 #define for_each_rsnd_ctu(pos, priv, i) \
22 for ((i) = 0; \
23 ((i) < rsnd_ctu_nr(priv)) && \
24 ((pos) = (struct rsnd_ctu *)(priv)->ctu + i); \
25 i++)
26
27 #define rsnd_ctu_initialize_lock(mod) __rsnd_ctu_initialize_lock(mod, 1)
28 #define rsnd_ctu_initialize_unlock(mod) __rsnd_ctu_initialize_lock(mod, 0)
29 static void __rsnd_ctu_initialize_lock(struct rsnd_mod *mod, u32 enable)
30 {
31 rsnd_mod_write(mod, CTU_CTUIR, enable);
32 }
33
34 static int rsnd_ctu_probe_(struct rsnd_mod *mod,
35 struct rsnd_dai_stream *io,
36 struct rsnd_priv *priv)
37 {
38 return rsnd_cmd_attach(io, rsnd_mod_id(mod) / 4);
39 }
40
41 static int rsnd_ctu_init(struct rsnd_mod *mod,
42 struct rsnd_dai_stream *io,
43 struct rsnd_priv *priv)
44 {
45 rsnd_mod_power_on(mod);
46
47 rsnd_ctu_initialize_lock(mod);
48
49 rsnd_mod_write(mod, CTU_ADINR, rsnd_get_adinr_chan(mod, io));
50
51 rsnd_ctu_initialize_unlock(mod);
52
53 return 0;
54 }
55
56 static int rsnd_ctu_quit(struct rsnd_mod *mod,
57 struct rsnd_dai_stream *io,
58 struct rsnd_priv *priv)
59 {
60 rsnd_mod_power_off(mod);
61
62 return 0;
63 }
64
65 static struct rsnd_mod_ops rsnd_ctu_ops = {
66 .name = CTU_NAME,
67 .probe = rsnd_ctu_probe_,
68 .init = rsnd_ctu_init,
69 .quit = rsnd_ctu_quit,
70 };
71
72 struct rsnd_mod *rsnd_ctu_mod_get(struct rsnd_priv *priv, int id)
73 {
74 if (WARN_ON(id < 0 || id >= rsnd_ctu_nr(priv)))
75 id = 0;
76
77 return rsnd_mod_get((struct rsnd_ctu *)(priv->ctu) + id);
78 }
79
80 static void rsnd_of_parse_ctu(struct platform_device *pdev,
81 const struct rsnd_of_data *of_data,
82 struct rsnd_priv *priv)
83 {
84 struct device_node *node;
85 struct rsnd_ctu_platform_info *ctu_info;
86 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
87 struct device *dev = &pdev->dev;
88 int nr;
89
90 if (!of_data)
91 return;
92
93 node = of_get_child_by_name(dev->of_node, "rcar_sound,ctu");
94 if (!node)
95 return;
96
97 nr = of_get_child_count(node);
98 if (!nr)
99 goto rsnd_of_parse_ctu_end;
100
101 ctu_info = devm_kzalloc(dev,
102 sizeof(struct rsnd_ctu_platform_info) * nr,
103 GFP_KERNEL);
104 if (!ctu_info) {
105 dev_err(dev, "ctu info allocation error\n");
106 goto rsnd_of_parse_ctu_end;
107 }
108
109 info->ctu_info = ctu_info;
110 info->ctu_info_nr = nr;
111
112 rsnd_of_parse_ctu_end:
113 of_node_put(node);
114
115 }
116
117 int rsnd_ctu_probe(struct platform_device *pdev,
118 const struct rsnd_of_data *of_data,
119 struct rsnd_priv *priv)
120 {
121 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
122 struct device *dev = rsnd_priv_to_dev(priv);
123 struct rsnd_ctu *ctu;
124 struct clk *clk;
125 char name[CTU_NAME_SIZE];
126 int i, nr, ret;
127
128 /* This driver doesn't support Gen1 at this point */
129 if (rsnd_is_gen1(priv))
130 return 0;
131
132 rsnd_of_parse_ctu(pdev, of_data, priv);
133
134 nr = info->ctu_info_nr;
135 if (!nr)
136 return 0;
137
138 ctu = devm_kzalloc(dev, sizeof(*ctu) * nr, GFP_KERNEL);
139 if (!ctu)
140 return -ENOMEM;
141
142 priv->ctu_nr = nr;
143 priv->ctu = ctu;
144
145 for_each_rsnd_ctu(ctu, priv, i) {
146 /*
147 * CTU00, CTU01, CTU02, CTU03 => CTU0
148 * CTU10, CTU11, CTU12, CTU13 => CTU1
149 */
150 snprintf(name, CTU_NAME_SIZE, "%s.%d",
151 CTU_NAME, i / 4);
152
153 clk = devm_clk_get(dev, name);
154 if (IS_ERR(clk))
155 return PTR_ERR(clk);
156
157 ctu->info = &info->ctu_info[i];
158
159 ret = rsnd_mod_init(priv, rsnd_mod_get(ctu), &rsnd_ctu_ops,
160 clk, RSND_MOD_CTU, i);
161 if (ret)
162 return ret;
163 }
164
165 return 0;
166 }
167
168 void rsnd_ctu_remove(struct platform_device *pdev,
169 struct rsnd_priv *priv)
170 {
171 struct rsnd_ctu *ctu;
172 int i;
173
174 for_each_rsnd_ctu(ctu, priv, i) {
175 rsnd_mod_quit(rsnd_mod_get(ctu));
176 }
177 }
This page took 0.036836 seconds and 5 git commands to generate.