xref: /openbmc/linux/sound/soc/sh/rcar/gen.c (revision e2f1cf25)
1 /*
2  * Renesas R-Car Gen1 SRU/SSI 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 
12 /*
13  * #define DEBUG
14  *
15  * you can also add below in
16  * ${LINUX}/drivers/base/regmap/regmap.c
17  * for regmap debug
18  *
19  * #define LOG_DEVICE "xxxx.rcar_sound"
20  */
21 
22 #include "rsnd.h"
23 
24 struct rsnd_gen {
25 	void __iomem *base[RSND_BASE_MAX];
26 
27 	struct rsnd_gen_ops *ops;
28 
29 	struct regmap *regmap[RSND_BASE_MAX];
30 	struct regmap_field *regs[RSND_REG_MAX];
31 	phys_addr_t res[RSND_REG_MAX];
32 };
33 
34 #define rsnd_priv_to_gen(p)	((struct rsnd_gen *)(p)->gen)
35 
36 struct rsnd_regmap_field_conf {
37 	int idx;
38 	unsigned int reg_offset;
39 	unsigned int id_offset;
40 };
41 
42 #define RSND_REG_SET(id, offset, _id_offset)	\
43 {						\
44 	.idx = id,				\
45 	.reg_offset = offset,			\
46 	.id_offset = _id_offset,		\
47 }
48 /* single address mapping */
49 #define RSND_GEN_S_REG(id, offset)	\
50 	RSND_REG_SET(RSND_REG_##id, offset, 0)
51 
52 /* multi address mapping */
53 #define RSND_GEN_M_REG(id, offset, _id_offset)	\
54 	RSND_REG_SET(RSND_REG_##id, offset, _id_offset)
55 
56 /*
57  *		basic function
58  */
59 static int rsnd_is_accessible_reg(struct rsnd_priv *priv,
60 				  struct rsnd_gen *gen, enum rsnd_reg reg)
61 {
62 	if (!gen->regs[reg]) {
63 		struct device *dev = rsnd_priv_to_dev(priv);
64 
65 		dev_err(dev, "unsupported register access %x\n", reg);
66 		return 0;
67 	}
68 
69 	return 1;
70 }
71 
72 u32 rsnd_read(struct rsnd_priv *priv,
73 	      struct rsnd_mod *mod, enum rsnd_reg reg)
74 {
75 	struct device *dev = rsnd_priv_to_dev(priv);
76 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
77 	u32 val;
78 
79 	if (!rsnd_is_accessible_reg(priv, gen, reg))
80 		return 0;
81 
82 	dev_dbg(dev, "r %s[%d] - %4d : %08x\n",
83 		rsnd_mod_name(mod), rsnd_mod_id(mod), reg, val);
84 
85 	regmap_fields_read(gen->regs[reg], rsnd_mod_id(mod), &val);
86 
87 	return val;
88 }
89 
90 void rsnd_write(struct rsnd_priv *priv,
91 		struct rsnd_mod *mod,
92 		enum rsnd_reg reg, u32 data)
93 {
94 	struct device *dev = rsnd_priv_to_dev(priv);
95 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
96 
97 	if (!rsnd_is_accessible_reg(priv, gen, reg))
98 		return;
99 
100 	dev_dbg(dev, "w %s[%d] - %4d : %08x\n",
101 		rsnd_mod_name(mod), rsnd_mod_id(mod), reg, data);
102 
103 	regmap_fields_write(gen->regs[reg], rsnd_mod_id(mod), data);
104 }
105 
106 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod,
107 	       enum rsnd_reg reg, u32 mask, u32 data)
108 {
109 	struct device *dev = rsnd_priv_to_dev(priv);
110 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
111 
112 	if (!rsnd_is_accessible_reg(priv, gen, reg))
113 		return;
114 
115 	dev_dbg(dev, "b %s[%d] - %4d : %08x/%08x\n",
116 		rsnd_mod_name(mod), rsnd_mod_id(mod), reg, data, mask);
117 
118 	regmap_fields_update_bits(gen->regs[reg], rsnd_mod_id(mod),
119 				  mask, data);
120 }
121 
122 phys_addr_t rsnd_gen_get_phy_addr(struct rsnd_priv *priv, int reg_id)
123 {
124 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
125 
126 	return	gen->res[reg_id];
127 }
128 
129 #define rsnd_gen_regmap_init(priv, id_size, reg_id, name, conf)		\
130 	_rsnd_gen_regmap_init(priv, id_size, reg_id, name, conf, ARRAY_SIZE(conf))
131 static int _rsnd_gen_regmap_init(struct rsnd_priv *priv,
132 				 int id_size,
133 				 int reg_id,
134 				 const char *name,
135 				 struct rsnd_regmap_field_conf *conf,
136 				 int conf_size)
137 {
138 	struct platform_device *pdev = rsnd_priv_to_pdev(priv);
139 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
140 	struct device *dev = rsnd_priv_to_dev(priv);
141 	struct resource *res;
142 	struct regmap_config regc;
143 	struct regmap_field *regs;
144 	struct regmap *regmap;
145 	struct reg_field regf;
146 	void __iomem *base;
147 	int i;
148 
149 	memset(&regc, 0, sizeof(regc));
150 	regc.reg_bits = 32;
151 	regc.val_bits = 32;
152 	regc.reg_stride = 4;
153 	regc.name = name;
154 
155 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
156 	if (!res)
157 		res = platform_get_resource(pdev, IORESOURCE_MEM, reg_id);
158 	if (!res)
159 		return -ENODEV;
160 
161 	base = devm_ioremap_resource(dev, res);
162 	if (IS_ERR(base))
163 		return PTR_ERR(base);
164 
165 	regmap = devm_regmap_init_mmio(dev, base, &regc);
166 	if (IS_ERR(regmap))
167 		return PTR_ERR(regmap);
168 
169 	gen->base[reg_id] = base;
170 	gen->regmap[reg_id] = regmap;
171 	gen->res[reg_id] = res->start;
172 
173 	for (i = 0; i < conf_size; i++) {
174 
175 		regf.reg	= conf[i].reg_offset;
176 		regf.id_offset	= conf[i].id_offset;
177 		regf.lsb	= 0;
178 		regf.msb	= 31;
179 		regf.id_size	= id_size;
180 
181 		regs = devm_regmap_field_alloc(dev, regmap, regf);
182 		if (IS_ERR(regs))
183 			return PTR_ERR(regs);
184 
185 		gen->regs[conf[i].idx] = regs;
186 	}
187 
188 	return 0;
189 }
190 
191 /*
192  *		Gen2
193  */
194 static int rsnd_gen2_probe(struct platform_device *pdev,
195 			   struct rsnd_priv *priv)
196 {
197 	struct rsnd_regmap_field_conf conf_ssiu[] = {
198 		RSND_GEN_S_REG(SSI_MODE0,	0x800),
199 		RSND_GEN_S_REG(SSI_MODE1,	0x804),
200 		/* FIXME: it needs SSI_MODE2/3 in the future */
201 		RSND_GEN_M_REG(SSI_BUSIF_MODE,	0x0,	0x80),
202 		RSND_GEN_M_REG(SSI_BUSIF_ADINR,	0x4,	0x80),
203 		RSND_GEN_M_REG(BUSIF_DALIGN,	0x8,	0x80),
204 		RSND_GEN_M_REG(SSI_CTRL,	0x10,	0x80),
205 		RSND_GEN_M_REG(INT_ENABLE,	0x18,	0x80),
206 	};
207 	struct rsnd_regmap_field_conf conf_scu[] = {
208 		RSND_GEN_M_REG(SRC_BUSIF_MODE,	0x0,	0x20),
209 		RSND_GEN_M_REG(SRC_ROUTE_MODE0,	0xc,	0x20),
210 		RSND_GEN_M_REG(SRC_CTRL,	0x10,	0x20),
211 		RSND_GEN_M_REG(SRC_INT_ENABLE0,	0x18,	0x20),
212 		RSND_GEN_M_REG(CMD_ROUTE_SLCT,	0x18c,	0x20),
213 		RSND_GEN_M_REG(CMD_CTRL,	0x190,	0x20),
214 		RSND_GEN_S_REG(SCU_SYS_STATUS0,	0x1c8),
215 		RSND_GEN_S_REG(SCU_SYS_INT_EN0,	0x1cc),
216 		RSND_GEN_S_REG(SCU_SYS_STATUS1,	0x1d0),
217 		RSND_GEN_S_REG(SCU_SYS_INT_EN1,	0x1c4),
218 		RSND_GEN_M_REG(SRC_SWRSR,	0x200,	0x40),
219 		RSND_GEN_M_REG(SRC_SRCIR,	0x204,	0x40),
220 		RSND_GEN_M_REG(SRC_ADINR,	0x214,	0x40),
221 		RSND_GEN_M_REG(SRC_IFSCR,	0x21c,	0x40),
222 		RSND_GEN_M_REG(SRC_IFSVR,	0x220,	0x40),
223 		RSND_GEN_M_REG(SRC_SRCCR,	0x224,	0x40),
224 		RSND_GEN_M_REG(SRC_BSDSR,	0x22c,	0x40),
225 		RSND_GEN_M_REG(SRC_BSISR,	0x238,	0x40),
226 		RSND_GEN_M_REG(DVC_SWRSR,	0xe00,	0x100),
227 		RSND_GEN_M_REG(DVC_DVUIR,	0xe04,	0x100),
228 		RSND_GEN_M_REG(DVC_ADINR,	0xe08,	0x100),
229 		RSND_GEN_M_REG(DVC_DVUCR,	0xe10,	0x100),
230 		RSND_GEN_M_REG(DVC_ZCMCR,	0xe14,	0x100),
231 		RSND_GEN_M_REG(DVC_VRCTR,	0xe18,	0x100),
232 		RSND_GEN_M_REG(DVC_VRPDR,	0xe1c,	0x100),
233 		RSND_GEN_M_REG(DVC_VRDBR,	0xe20,	0x100),
234 		RSND_GEN_M_REG(DVC_VOL0R,	0xe28,	0x100),
235 		RSND_GEN_M_REG(DVC_VOL1R,	0xe2c,	0x100),
236 		RSND_GEN_M_REG(DVC_DVUER,	0xe48,	0x100),
237 	};
238 	struct rsnd_regmap_field_conf conf_adg[] = {
239 		RSND_GEN_S_REG(BRRA,		0x00),
240 		RSND_GEN_S_REG(BRRB,		0x04),
241 		RSND_GEN_S_REG(SSICKR,		0x08),
242 		RSND_GEN_S_REG(AUDIO_CLK_SEL0,	0x0c),
243 		RSND_GEN_S_REG(AUDIO_CLK_SEL1,	0x10),
244 		RSND_GEN_S_REG(AUDIO_CLK_SEL2,	0x14),
245 		RSND_GEN_S_REG(DIV_EN,		0x30),
246 		RSND_GEN_S_REG(SRCIN_TIMSEL0,	0x34),
247 		RSND_GEN_S_REG(SRCIN_TIMSEL1,	0x38),
248 		RSND_GEN_S_REG(SRCIN_TIMSEL2,	0x3c),
249 		RSND_GEN_S_REG(SRCIN_TIMSEL3,	0x40),
250 		RSND_GEN_S_REG(SRCIN_TIMSEL4,	0x44),
251 		RSND_GEN_S_REG(SRCOUT_TIMSEL0,	0x48),
252 		RSND_GEN_S_REG(SRCOUT_TIMSEL1,	0x4c),
253 		RSND_GEN_S_REG(SRCOUT_TIMSEL2,	0x50),
254 		RSND_GEN_S_REG(SRCOUT_TIMSEL3,	0x54),
255 		RSND_GEN_S_REG(SRCOUT_TIMSEL4,	0x58),
256 		RSND_GEN_S_REG(CMDOUT_TIMSEL,	0x5c),
257 	};
258 	struct rsnd_regmap_field_conf conf_ssi[] = {
259 		RSND_GEN_M_REG(SSICR,		0x00,	0x40),
260 		RSND_GEN_M_REG(SSISR,		0x04,	0x40),
261 		RSND_GEN_M_REG(SSITDR,		0x08,	0x40),
262 		RSND_GEN_M_REG(SSIRDR,		0x0c,	0x40),
263 		RSND_GEN_M_REG(SSIWSR,		0x20,	0x40),
264 	};
265 	int ret_ssiu;
266 	int ret_scu;
267 	int ret_adg;
268 	int ret_ssi;
269 
270 	ret_ssiu = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SSIU, "ssiu", conf_ssiu);
271 	ret_scu  = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SCU,  "scu",  conf_scu);
272 	ret_adg  = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_ADG,  "adg",  conf_adg);
273 	ret_ssi  = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SSI,  "ssi",  conf_ssi);
274 	if (ret_ssiu < 0 ||
275 	    ret_scu  < 0 ||
276 	    ret_adg  < 0 ||
277 	    ret_ssi  < 0)
278 		return ret_ssiu | ret_scu | ret_adg | ret_ssi;
279 
280 	return 0;
281 }
282 
283 /*
284  *		Gen1
285  */
286 
287 static int rsnd_gen1_probe(struct platform_device *pdev,
288 			   struct rsnd_priv *priv)
289 {
290 	struct rsnd_regmap_field_conf conf_sru[] = {
291 		RSND_GEN_S_REG(SRC_ROUTE_SEL,	0x00),
292 		RSND_GEN_S_REG(SRC_TMG_SEL0,	0x08),
293 		RSND_GEN_S_REG(SRC_TMG_SEL1,	0x0c),
294 		RSND_GEN_S_REG(SRC_TMG_SEL2,	0x10),
295 		RSND_GEN_S_REG(SRC_ROUTE_CTRL,	0xc0),
296 		RSND_GEN_S_REG(SSI_MODE0,	0xD0),
297 		RSND_GEN_S_REG(SSI_MODE1,	0xD4),
298 		RSND_GEN_M_REG(SRC_BUSIF_MODE,	0x20,	0x4),
299 		RSND_GEN_M_REG(SRC_ROUTE_MODE0,	0x50,	0x8),
300 		RSND_GEN_M_REG(SRC_SWRSR,	0x200,	0x40),
301 		RSND_GEN_M_REG(SRC_SRCIR,	0x204,	0x40),
302 		RSND_GEN_M_REG(SRC_ADINR,	0x214,	0x40),
303 		RSND_GEN_M_REG(SRC_IFSCR,	0x21c,	0x40),
304 		RSND_GEN_M_REG(SRC_IFSVR,	0x220,	0x40),
305 		RSND_GEN_M_REG(SRC_SRCCR,	0x224,	0x40),
306 		RSND_GEN_M_REG(SRC_MNFSR,	0x228,	0x40),
307 		/*
308 		 * ADD US
309 		 *
310 		 * SRC_STATUS
311 		 * SRC_INT_EN
312 		 * SCU_SYS_STATUS0
313 		 * SCU_SYS_STATUS1
314 		 * SCU_SYS_INT_EN0
315 		 * SCU_SYS_INT_EN1
316 		 */
317 	};
318 	struct rsnd_regmap_field_conf conf_adg[] = {
319 		RSND_GEN_S_REG(BRRA,		0x00),
320 		RSND_GEN_S_REG(BRRB,		0x04),
321 		RSND_GEN_S_REG(SSICKR,		0x08),
322 		RSND_GEN_S_REG(AUDIO_CLK_SEL0,	0x0c),
323 		RSND_GEN_S_REG(AUDIO_CLK_SEL1,	0x10),
324 		RSND_GEN_S_REG(AUDIO_CLK_SEL3,	0x18),
325 		RSND_GEN_S_REG(AUDIO_CLK_SEL4,	0x1c),
326 		RSND_GEN_S_REG(AUDIO_CLK_SEL5,	0x20),
327 	};
328 	struct rsnd_regmap_field_conf conf_ssi[] = {
329 		RSND_GEN_M_REG(SSICR,		0x00,	0x40),
330 		RSND_GEN_M_REG(SSISR,		0x04,	0x40),
331 		RSND_GEN_M_REG(SSITDR,		0x08,	0x40),
332 		RSND_GEN_M_REG(SSIRDR,		0x0c,	0x40),
333 		RSND_GEN_M_REG(SSIWSR,		0x20,	0x40),
334 	};
335 	int ret_sru;
336 	int ret_adg;
337 	int ret_ssi;
338 
339 	ret_sru  = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_SRU, "sru", conf_sru);
340 	ret_adg  = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_ADG, "adg", conf_adg);
341 	ret_ssi  = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_SSI, "ssi", conf_ssi);
342 	if (ret_sru  < 0 ||
343 	    ret_adg  < 0 ||
344 	    ret_ssi  < 0)
345 		return ret_sru | ret_adg | ret_ssi;
346 
347 	return 0;
348 }
349 
350 /*
351  *		Gen
352  */
353 static void rsnd_of_parse_gen(struct platform_device *pdev,
354 			      const struct rsnd_of_data *of_data,
355 			      struct rsnd_priv *priv)
356 {
357 	struct rcar_snd_info *info = priv->info;
358 
359 	if (!of_data)
360 		return;
361 
362 	info->flags = of_data->flags;
363 }
364 
365 int rsnd_gen_probe(struct platform_device *pdev,
366 		   const struct rsnd_of_data *of_data,
367 		   struct rsnd_priv *priv)
368 {
369 	struct device *dev = rsnd_priv_to_dev(priv);
370 	struct rsnd_gen *gen;
371 	int ret;
372 
373 	rsnd_of_parse_gen(pdev, of_data, priv);
374 
375 	gen = devm_kzalloc(dev, sizeof(*gen), GFP_KERNEL);
376 	if (!gen) {
377 		dev_err(dev, "GEN allocate failed\n");
378 		return -ENOMEM;
379 	}
380 
381 	priv->gen = gen;
382 
383 	ret = -ENODEV;
384 	if (rsnd_is_gen1(priv))
385 		ret = rsnd_gen1_probe(pdev, priv);
386 	else if (rsnd_is_gen2(priv))
387 		ret = rsnd_gen2_probe(pdev, priv);
388 
389 	if (ret < 0)
390 		dev_err(dev, "unknown generation R-Car sound device\n");
391 
392 	return ret;
393 }
394