xref: /openbmc/linux/sound/soc/sh/rcar/gen.c (revision 3821a065)
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_force_write(struct rsnd_priv *priv,
107 		      struct rsnd_mod *mod,
108 		      enum rsnd_reg reg, u32 data)
109 {
110 	struct device *dev = rsnd_priv_to_dev(priv);
111 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
112 
113 	if (!rsnd_is_accessible_reg(priv, gen, reg))
114 		return;
115 
116 	dev_dbg(dev, "w %s[%d] - %4d : %08x\n",
117 		rsnd_mod_name(mod), rsnd_mod_id(mod), reg, data);
118 
119 	regmap_fields_force_write(gen->regs[reg], rsnd_mod_id(mod), data);
120 }
121 
122 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod,
123 	       enum rsnd_reg reg, u32 mask, u32 data)
124 {
125 	struct device *dev = rsnd_priv_to_dev(priv);
126 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
127 
128 	if (!rsnd_is_accessible_reg(priv, gen, reg))
129 		return;
130 
131 	dev_dbg(dev, "b %s[%d] - %4d : %08x/%08x\n",
132 		rsnd_mod_name(mod), rsnd_mod_id(mod), reg, data, mask);
133 
134 	regmap_fields_update_bits(gen->regs[reg], rsnd_mod_id(mod),
135 				  mask, data);
136 }
137 
138 phys_addr_t rsnd_gen_get_phy_addr(struct rsnd_priv *priv, int reg_id)
139 {
140 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
141 
142 	return	gen->res[reg_id];
143 }
144 
145 #define rsnd_gen_regmap_init(priv, id_size, reg_id, name, conf)		\
146 	_rsnd_gen_regmap_init(priv, id_size, reg_id, name, conf, ARRAY_SIZE(conf))
147 static int _rsnd_gen_regmap_init(struct rsnd_priv *priv,
148 				 int id_size,
149 				 int reg_id,
150 				 const char *name,
151 				 struct rsnd_regmap_field_conf *conf,
152 				 int conf_size)
153 {
154 	struct platform_device *pdev = rsnd_priv_to_pdev(priv);
155 	struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
156 	struct device *dev = rsnd_priv_to_dev(priv);
157 	struct resource *res;
158 	struct regmap_config regc;
159 	struct regmap_field *regs;
160 	struct regmap *regmap;
161 	struct reg_field regf;
162 	void __iomem *base;
163 	int i;
164 
165 	memset(&regc, 0, sizeof(regc));
166 	regc.reg_bits = 32;
167 	regc.val_bits = 32;
168 	regc.reg_stride = 4;
169 	regc.name = name;
170 
171 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
172 	if (!res)
173 		res = platform_get_resource(pdev, IORESOURCE_MEM, reg_id);
174 	if (!res)
175 		return -ENODEV;
176 
177 	base = devm_ioremap_resource(dev, res);
178 	if (IS_ERR(base))
179 		return PTR_ERR(base);
180 
181 	regmap = devm_regmap_init_mmio(dev, base, &regc);
182 	if (IS_ERR(regmap))
183 		return PTR_ERR(regmap);
184 
185 	gen->base[reg_id] = base;
186 	gen->regmap[reg_id] = regmap;
187 	gen->res[reg_id] = res->start;
188 
189 	for (i = 0; i < conf_size; i++) {
190 
191 		regf.reg	= conf[i].reg_offset;
192 		regf.id_offset	= conf[i].id_offset;
193 		regf.lsb	= 0;
194 		regf.msb	= 31;
195 		regf.id_size	= id_size;
196 
197 		regs = devm_regmap_field_alloc(dev, regmap, regf);
198 		if (IS_ERR(regs))
199 			return PTR_ERR(regs);
200 
201 		gen->regs[conf[i].idx] = regs;
202 	}
203 
204 	return 0;
205 }
206 
207 /*
208  *		Gen2
209  */
210 static int rsnd_gen2_probe(struct platform_device *pdev,
211 			   struct rsnd_priv *priv)
212 {
213 	struct rsnd_regmap_field_conf conf_ssiu[] = {
214 		RSND_GEN_S_REG(SSI_MODE0,	0x800),
215 		RSND_GEN_S_REG(SSI_MODE1,	0x804),
216 		/* FIXME: it needs SSI_MODE2/3 in the future */
217 		RSND_GEN_M_REG(SSI_BUSIF_MODE,	0x0,	0x80),
218 		RSND_GEN_M_REG(SSI_BUSIF_ADINR,	0x4,	0x80),
219 		RSND_GEN_M_REG(SSI_BUSIF_DALIGN,0x8,	0x80),
220 		RSND_GEN_M_REG(SSI_CTRL,	0x10,	0x80),
221 		RSND_GEN_M_REG(SSI_INT_ENABLE,	0x18,	0x80),
222 	};
223 	struct rsnd_regmap_field_conf conf_scu[] = {
224 		RSND_GEN_M_REG(SRC_BUSIF_MODE,	0x0,	0x20),
225 		RSND_GEN_M_REG(SRC_BUSIF_DALIGN,0x8,	0x20),
226 		RSND_GEN_M_REG(SRC_ROUTE_MODE0,	0xc,	0x20),
227 		RSND_GEN_M_REG(SRC_CTRL,	0x10,	0x20),
228 		RSND_GEN_M_REG(SRC_INT_ENABLE0,	0x18,	0x20),
229 		RSND_GEN_M_REG(CMD_ROUTE_SLCT,	0x18c,	0x20),
230 		RSND_GEN_M_REG(CMD_CTRL,	0x190,	0x20),
231 		RSND_GEN_S_REG(SCU_SYS_STATUS0,	0x1c8),
232 		RSND_GEN_S_REG(SCU_SYS_INT_EN0,	0x1cc),
233 		RSND_GEN_S_REG(SCU_SYS_STATUS1,	0x1d0),
234 		RSND_GEN_S_REG(SCU_SYS_INT_EN1,	0x1c4),
235 		RSND_GEN_M_REG(SRC_SWRSR,	0x200,	0x40),
236 		RSND_GEN_M_REG(SRC_SRCIR,	0x204,	0x40),
237 		RSND_GEN_M_REG(SRC_ADINR,	0x214,	0x40),
238 		RSND_GEN_M_REG(SRC_IFSCR,	0x21c,	0x40),
239 		RSND_GEN_M_REG(SRC_IFSVR,	0x220,	0x40),
240 		RSND_GEN_M_REG(SRC_SRCCR,	0x224,	0x40),
241 		RSND_GEN_M_REG(SRC_BSDSR,	0x22c,	0x40),
242 		RSND_GEN_M_REG(SRC_BSISR,	0x238,	0x40),
243 		RSND_GEN_M_REG(CTU_CTUIR,	0x504,	0x100),
244 		RSND_GEN_M_REG(CTU_ADINR,	0x508,	0x100),
245 		RSND_GEN_M_REG(MIX_SWRSR,	0xd00,	0x40),
246 		RSND_GEN_M_REG(MIX_MIXIR,	0xd04,	0x40),
247 		RSND_GEN_M_REG(MIX_ADINR,	0xd08,	0x40),
248 		RSND_GEN_M_REG(MIX_MIXMR,	0xd10,	0x40),
249 		RSND_GEN_M_REG(MIX_MVPDR,	0xd14,	0x40),
250 		RSND_GEN_M_REG(MIX_MDBAR,	0xd18,	0x40),
251 		RSND_GEN_M_REG(MIX_MDBBR,	0xd1c,	0x40),
252 		RSND_GEN_M_REG(MIX_MDBCR,	0xd20,	0x40),
253 		RSND_GEN_M_REG(MIX_MDBDR,	0xd24,	0x40),
254 		RSND_GEN_M_REG(MIX_MDBER,	0xd28,	0x40),
255 		RSND_GEN_M_REG(DVC_SWRSR,	0xe00,	0x100),
256 		RSND_GEN_M_REG(DVC_DVUIR,	0xe04,	0x100),
257 		RSND_GEN_M_REG(DVC_ADINR,	0xe08,	0x100),
258 		RSND_GEN_M_REG(DVC_DVUCR,	0xe10,	0x100),
259 		RSND_GEN_M_REG(DVC_ZCMCR,	0xe14,	0x100),
260 		RSND_GEN_M_REG(DVC_VRCTR,	0xe18,	0x100),
261 		RSND_GEN_M_REG(DVC_VRPDR,	0xe1c,	0x100),
262 		RSND_GEN_M_REG(DVC_VRDBR,	0xe20,	0x100),
263 		RSND_GEN_M_REG(DVC_VOL0R,	0xe28,	0x100),
264 		RSND_GEN_M_REG(DVC_VOL1R,	0xe2c,	0x100),
265 		RSND_GEN_M_REG(DVC_DVUER,	0xe48,	0x100),
266 	};
267 	struct rsnd_regmap_field_conf conf_adg[] = {
268 		RSND_GEN_S_REG(BRRA,		0x00),
269 		RSND_GEN_S_REG(BRRB,		0x04),
270 		RSND_GEN_S_REG(SSICKR,		0x08),
271 		RSND_GEN_S_REG(AUDIO_CLK_SEL0,	0x0c),
272 		RSND_GEN_S_REG(AUDIO_CLK_SEL1,	0x10),
273 		RSND_GEN_S_REG(AUDIO_CLK_SEL2,	0x14),
274 		RSND_GEN_S_REG(DIV_EN,		0x30),
275 		RSND_GEN_S_REG(SRCIN_TIMSEL0,	0x34),
276 		RSND_GEN_S_REG(SRCIN_TIMSEL1,	0x38),
277 		RSND_GEN_S_REG(SRCIN_TIMSEL2,	0x3c),
278 		RSND_GEN_S_REG(SRCIN_TIMSEL3,	0x40),
279 		RSND_GEN_S_REG(SRCIN_TIMSEL4,	0x44),
280 		RSND_GEN_S_REG(SRCOUT_TIMSEL0,	0x48),
281 		RSND_GEN_S_REG(SRCOUT_TIMSEL1,	0x4c),
282 		RSND_GEN_S_REG(SRCOUT_TIMSEL2,	0x50),
283 		RSND_GEN_S_REG(SRCOUT_TIMSEL3,	0x54),
284 		RSND_GEN_S_REG(SRCOUT_TIMSEL4,	0x58),
285 		RSND_GEN_S_REG(CMDOUT_TIMSEL,	0x5c),
286 	};
287 	struct rsnd_regmap_field_conf conf_ssi[] = {
288 		RSND_GEN_M_REG(SSICR,		0x00,	0x40),
289 		RSND_GEN_M_REG(SSISR,		0x04,	0x40),
290 		RSND_GEN_M_REG(SSITDR,		0x08,	0x40),
291 		RSND_GEN_M_REG(SSIRDR,		0x0c,	0x40),
292 		RSND_GEN_M_REG(SSIWSR,		0x20,	0x40),
293 	};
294 	int ret_ssiu;
295 	int ret_scu;
296 	int ret_adg;
297 	int ret_ssi;
298 
299 	ret_ssiu = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SSIU, "ssiu", conf_ssiu);
300 	ret_scu  = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SCU,  "scu",  conf_scu);
301 	ret_adg  = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_ADG,  "adg",  conf_adg);
302 	ret_ssi  = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SSI,  "ssi",  conf_ssi);
303 	if (ret_ssiu < 0 ||
304 	    ret_scu  < 0 ||
305 	    ret_adg  < 0 ||
306 	    ret_ssi  < 0)
307 		return ret_ssiu | ret_scu | ret_adg | ret_ssi;
308 
309 	return 0;
310 }
311 
312 /*
313  *		Gen1
314  */
315 
316 static int rsnd_gen1_probe(struct platform_device *pdev,
317 			   struct rsnd_priv *priv)
318 {
319 	struct rsnd_regmap_field_conf conf_sru[] = {
320 		RSND_GEN_S_REG(SRC_ROUTE_SEL,	0x00),
321 		RSND_GEN_S_REG(SRC_TMG_SEL0,	0x08),
322 		RSND_GEN_S_REG(SRC_TMG_SEL1,	0x0c),
323 		RSND_GEN_S_REG(SRC_TMG_SEL2,	0x10),
324 		RSND_GEN_S_REG(SRC_ROUTE_CTRL,	0xc0),
325 		RSND_GEN_S_REG(SSI_MODE0,	0xD0),
326 		RSND_GEN_S_REG(SSI_MODE1,	0xD4),
327 		RSND_GEN_M_REG(SRC_BUSIF_MODE,	0x20,	0x4),
328 		RSND_GEN_M_REG(SRC_ROUTE_MODE0,	0x50,	0x8),
329 		RSND_GEN_M_REG(SRC_SWRSR,	0x200,	0x40),
330 		RSND_GEN_M_REG(SRC_SRCIR,	0x204,	0x40),
331 		RSND_GEN_M_REG(SRC_ADINR,	0x214,	0x40),
332 		RSND_GEN_M_REG(SRC_IFSCR,	0x21c,	0x40),
333 		RSND_GEN_M_REG(SRC_IFSVR,	0x220,	0x40),
334 		RSND_GEN_M_REG(SRC_SRCCR,	0x224,	0x40),
335 		RSND_GEN_M_REG(SRC_MNFSR,	0x228,	0x40),
336 		/*
337 		 * ADD US
338 		 *
339 		 * SRC_STATUS
340 		 * SRC_INT_EN
341 		 * SCU_SYS_STATUS0
342 		 * SCU_SYS_STATUS1
343 		 * SCU_SYS_INT_EN0
344 		 * SCU_SYS_INT_EN1
345 		 */
346 	};
347 	struct rsnd_regmap_field_conf conf_adg[] = {
348 		RSND_GEN_S_REG(BRRA,		0x00),
349 		RSND_GEN_S_REG(BRRB,		0x04),
350 		RSND_GEN_S_REG(SSICKR,		0x08),
351 		RSND_GEN_S_REG(AUDIO_CLK_SEL0,	0x0c),
352 		RSND_GEN_S_REG(AUDIO_CLK_SEL1,	0x10),
353 		RSND_GEN_S_REG(AUDIO_CLK_SEL3,	0x18),
354 		RSND_GEN_S_REG(AUDIO_CLK_SEL4,	0x1c),
355 		RSND_GEN_S_REG(AUDIO_CLK_SEL5,	0x20),
356 	};
357 	struct rsnd_regmap_field_conf conf_ssi[] = {
358 		RSND_GEN_M_REG(SSICR,		0x00,	0x40),
359 		RSND_GEN_M_REG(SSISR,		0x04,	0x40),
360 		RSND_GEN_M_REG(SSITDR,		0x08,	0x40),
361 		RSND_GEN_M_REG(SSIRDR,		0x0c,	0x40),
362 		RSND_GEN_M_REG(SSIWSR,		0x20,	0x40),
363 	};
364 	int ret_sru;
365 	int ret_adg;
366 	int ret_ssi;
367 
368 	ret_sru  = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_SRU, "sru", conf_sru);
369 	ret_adg  = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_ADG, "adg", conf_adg);
370 	ret_ssi  = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_SSI, "ssi", conf_ssi);
371 	if (ret_sru  < 0 ||
372 	    ret_adg  < 0 ||
373 	    ret_ssi  < 0)
374 		return ret_sru | ret_adg | ret_ssi;
375 
376 	return 0;
377 }
378 
379 /*
380  *		Gen
381  */
382 static void rsnd_of_parse_gen(struct platform_device *pdev,
383 			      const struct rsnd_of_data *of_data,
384 			      struct rsnd_priv *priv)
385 {
386 	struct rcar_snd_info *info = priv->info;
387 
388 	if (!of_data)
389 		return;
390 
391 	info->flags = of_data->flags;
392 }
393 
394 int rsnd_gen_probe(struct platform_device *pdev,
395 		   const struct rsnd_of_data *of_data,
396 		   struct rsnd_priv *priv)
397 {
398 	struct device *dev = rsnd_priv_to_dev(priv);
399 	struct rsnd_gen *gen;
400 	int ret;
401 
402 	rsnd_of_parse_gen(pdev, of_data, priv);
403 
404 	gen = devm_kzalloc(dev, sizeof(*gen), GFP_KERNEL);
405 	if (!gen) {
406 		dev_err(dev, "GEN allocate failed\n");
407 		return -ENOMEM;
408 	}
409 
410 	priv->gen = gen;
411 
412 	ret = -ENODEV;
413 	if (rsnd_is_gen1(priv))
414 		ret = rsnd_gen1_probe(pdev, priv);
415 	else if (rsnd_is_gen2(priv))
416 		ret = rsnd_gen2_probe(pdev, priv);
417 
418 	if (ret < 0)
419 		dev_err(dev, "unknown generation R-Car sound device\n");
420 
421 	return ret;
422 }
423