]> Pileus Git - ~andy/linux/blob - sound/soc/sh/rcar/scu.c
5f4f57206faff98bd5dd20cf98c315df9561add7
[~andy/linux] / sound / soc / sh / rcar / scu.c
1 /*
2  * Renesas R-Car SCU support
3  *
4  * Copyright (C) 2013 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_scu {
14         struct rsnd_scu_platform_info *info; /* rcar_snd.h */
15         struct rsnd_mod mod;
16 };
17
18 #define rsnd_scu_mode_flags(p) ((p)->info->flags)
19
20 /*
21  * ADINR
22  */
23 #define OTBL_24         (0 << 16)
24 #define OTBL_22         (2 << 16)
25 #define OTBL_20         (4 << 16)
26 #define OTBL_18         (6 << 16)
27 #define OTBL_16         (8 << 16)
28
29
30 #define rsnd_mod_to_scu(_mod)   \
31         container_of((_mod), struct rsnd_scu, mod)
32
33 #define for_each_rsnd_scu(pos, priv, i)                                 \
34         for ((i) = 0;                                                   \
35              ((i) < rsnd_scu_nr(priv)) &&                               \
36                      ((pos) = (struct rsnd_scu *)(priv)->scu + i);      \
37              i++)
38
39 /* Gen1 only */
40 static int rsnd_src_set_route_if_gen1(struct rsnd_priv *priv,
41                               struct rsnd_mod *mod,
42                               struct rsnd_dai *rdai,
43                               struct rsnd_dai_stream *io)
44 {
45         struct scu_route_config {
46                 u32 mask;
47                 int shift;
48         } routes[] = {
49                 { 0xF,  0, }, /* 0 */
50                 { 0xF,  4, }, /* 1 */
51                 { 0xF,  8, }, /* 2 */
52                 { 0x7, 12, }, /* 3 */
53                 { 0x7, 16, }, /* 4 */
54                 { 0x7, 20, }, /* 5 */
55                 { 0x7, 24, }, /* 6 */
56                 { 0x3, 28, }, /* 7 */
57                 { 0x3, 30, }, /* 8 */
58         };
59
60         u32 mask;
61         u32 val;
62         int shift;
63         int id;
64
65         /*
66          * Gen1 only
67          */
68         if (!rsnd_is_gen1(priv))
69                 return 0;
70
71         id = rsnd_mod_id(mod);
72         if (id < 0 || id > ARRAY_SIZE(routes))
73                 return -EIO;
74
75         /*
76          * SRC_ROUTE_SELECT
77          */
78         val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
79         val = val               << routes[id].shift;
80         mask = routes[id].mask  << routes[id].shift;
81
82         rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
83
84         /*
85          * SRC_TIMING_SELECT
86          */
87         shift   = (id % 4) * 8;
88         mask    = 0x1F << shift;
89         if (8 == id) /* SRU8 is very special */
90                 val = id << shift;
91         else
92                 val = (id + 1) << shift;
93
94         switch (id / 4) {
95         case 0:
96                 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
97                 break;
98         case 1:
99                 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
100                 break;
101         case 2:
102                 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
103                 break;
104         }
105
106         return 0;
107 }
108
109 static int rsnd_scu_rate_ctrl(struct rsnd_priv *priv,
110                               struct rsnd_mod *mod,
111                               struct rsnd_dai *rdai,
112                               struct rsnd_dai_stream *io)
113 {
114         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
115         u32 adinr = runtime->channels;
116
117         switch (runtime->sample_bits) {
118         case 16:
119                 adinr |= OTBL_16;
120                 break;
121         case 32:
122                 adinr |= OTBL_24;
123                 break;
124         default:
125                 return -EIO;
126         }
127
128         rsnd_mod_write(mod, SRC_ADINR, adinr);
129
130         return 0;
131 }
132
133 static int rsnd_scu_transfer_start(struct rsnd_priv *priv,
134                                    struct rsnd_mod *mod,
135                                    struct rsnd_dai *rdai,
136                                    struct rsnd_dai_stream *io)
137 {
138         int id = rsnd_mod_id(mod);
139         u32 val;
140
141         if (rsnd_is_gen1(priv)) {
142                 val = (1 << id);
143                 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, val, val);
144         }
145
146         rsnd_mod_write(mod, BUSIF_MODE, 1);
147
148         return 0;
149 }
150
151 bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod)
152 {
153         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
154         u32 flags = rsnd_scu_mode_flags(scu);
155
156         return !!(flags & RSND_SCU_USE_HPBIF);
157 }
158
159 static int rsnd_scu_start(struct rsnd_mod *mod,
160                           struct rsnd_dai *rdai,
161                           struct rsnd_dai_stream *io)
162 {
163         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
164         struct device *dev = rsnd_priv_to_dev(priv);
165         int ret;
166
167         /*
168          * SCU will be used if it has RSND_SCU_USE_HPBIF flags
169          */
170         if (!rsnd_scu_hpbif_is_enable(mod)) {
171                 /* it use PIO transter */
172                 dev_dbg(dev, "%s%d is not used\n",
173                         rsnd_mod_name(mod), rsnd_mod_id(mod));
174
175                 return 0;
176         }
177
178         /* it use DMA transter */
179
180         ret = rsnd_src_set_route_if_gen1(priv, mod, rdai, io);
181         if (ret < 0)
182                 return ret;
183
184         ret = rsnd_scu_rate_ctrl(priv, mod, rdai, io);
185         if (ret < 0)
186                 return ret;
187
188         ret = rsnd_scu_transfer_start(priv, mod, rdai, io);
189         if (ret < 0)
190                 return ret;
191
192         dev_dbg(dev, "%s%d start\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
193
194         return 0;
195 }
196
197 static struct rsnd_mod_ops rsnd_scu_ops = {
198         .name   = "scu",
199         .start  = rsnd_scu_start,
200 };
201
202 struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
203 {
204         BUG_ON(id < 0 || id >= rsnd_scu_nr(priv));
205
206         return &((struct rsnd_scu *)(priv->scu) + id)->mod;
207 }
208
209 int rsnd_scu_probe(struct platform_device *pdev,
210                    struct rcar_snd_info *info,
211                    struct rsnd_priv *priv)
212 {
213         struct device *dev = rsnd_priv_to_dev(priv);
214         struct rsnd_scu *scu;
215         int i, nr;
216
217         /*
218          * init SCU
219          */
220         nr      = info->scu_info_nr;
221         scu     = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
222         if (!scu) {
223                 dev_err(dev, "SCU allocate failed\n");
224                 return -ENOMEM;
225         }
226
227         priv->scu_nr    = nr;
228         priv->scu       = scu;
229
230         for_each_rsnd_scu(scu, priv, i) {
231                 rsnd_mod_init(priv, &scu->mod,
232                               &rsnd_scu_ops, i);
233                 scu->info = &info->scu_info[i];
234
235                 dev_dbg(dev, "SCU%d probed\n", i);
236         }
237         dev_dbg(dev, "scu probed\n");
238
239         return 0;
240 }
241
242 void rsnd_scu_remove(struct platform_device *pdev,
243                      struct rsnd_priv *priv)
244 {
245 }