gen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. * #define DEBUG
  13. *
  14. * you can also add below in
  15. * ${LINUX}/drivers/base/regmap/regmap.c
  16. * for regmap debug
  17. *
  18. * #define LOG_DEVICE "xxxx.rcar_sound"
  19. */
  20. #include "rsnd.h"
  21. struct rsnd_gen {
  22. struct rsnd_gen_ops *ops;
  23. /* RSND_BASE_MAX base */
  24. void __iomem *base[RSND_BASE_MAX];
  25. phys_addr_t res[RSND_BASE_MAX];
  26. struct regmap *regmap[RSND_BASE_MAX];
  27. /* RSND_REG_MAX base */
  28. struct regmap_field *regs[RSND_REG_MAX];
  29. const char *reg_name[RSND_REG_MAX];
  30. };
  31. #define rsnd_priv_to_gen(p) ((struct rsnd_gen *)(p)->gen)
  32. #define rsnd_reg_name(gen, id) ((gen)->reg_name[id])
  33. struct rsnd_regmap_field_conf {
  34. int idx;
  35. unsigned int reg_offset;
  36. unsigned int id_offset;
  37. const char *reg_name;
  38. };
  39. #define RSND_REG_SET(id, offset, _id_offset, n) \
  40. { \
  41. .idx = id, \
  42. .reg_offset = offset, \
  43. .id_offset = _id_offset, \
  44. .reg_name = n, \
  45. }
  46. /* single address mapping */
  47. #define RSND_GEN_S_REG(id, offset) \
  48. RSND_REG_SET(RSND_REG_##id, offset, 0, #id)
  49. /* multi address mapping */
  50. #define RSND_GEN_M_REG(id, offset, _id_offset) \
  51. RSND_REG_SET(RSND_REG_##id, offset, _id_offset, #id)
  52. /*
  53. * basic function
  54. */
  55. static int rsnd_is_accessible_reg(struct rsnd_priv *priv,
  56. struct rsnd_gen *gen, enum rsnd_reg reg)
  57. {
  58. if (!gen->regs[reg]) {
  59. struct device *dev = rsnd_priv_to_dev(priv);
  60. dev_err(dev, "unsupported register access %x\n", reg);
  61. return 0;
  62. }
  63. return 1;
  64. }
  65. u32 rsnd_read(struct rsnd_priv *priv,
  66. struct rsnd_mod *mod, enum rsnd_reg reg)
  67. {
  68. struct device *dev = rsnd_priv_to_dev(priv);
  69. struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
  70. u32 val;
  71. if (!rsnd_is_accessible_reg(priv, gen, reg))
  72. return 0;
  73. regmap_fields_read(gen->regs[reg], rsnd_mod_id(mod), &val);
  74. dev_dbg(dev, "r %s[%d] - %-18s (%4d) : %08x\n",
  75. rsnd_mod_name(mod), rsnd_mod_id(mod),
  76. rsnd_reg_name(gen, reg), reg, val);
  77. return val;
  78. }
  79. void rsnd_write(struct rsnd_priv *priv,
  80. struct rsnd_mod *mod,
  81. enum rsnd_reg reg, u32 data)
  82. {
  83. struct device *dev = rsnd_priv_to_dev(priv);
  84. struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
  85. if (!rsnd_is_accessible_reg(priv, gen, reg))
  86. return;
  87. regmap_fields_force_write(gen->regs[reg], rsnd_mod_id(mod), data);
  88. dev_dbg(dev, "w %s[%d] - %-18s (%4d) : %08x\n",
  89. rsnd_mod_name(mod), rsnd_mod_id(mod),
  90. rsnd_reg_name(gen, reg), reg, data);
  91. }
  92. void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod,
  93. enum rsnd_reg reg, u32 mask, u32 data)
  94. {
  95. struct device *dev = rsnd_priv_to_dev(priv);
  96. struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
  97. if (!rsnd_is_accessible_reg(priv, gen, reg))
  98. return;
  99. regmap_fields_force_update_bits(gen->regs[reg],
  100. rsnd_mod_id(mod), mask, data);
  101. dev_dbg(dev, "b %s[%d] - %-18s (%4d) : %08x/%08x\n",
  102. rsnd_mod_name(mod), rsnd_mod_id(mod),
  103. rsnd_reg_name(gen, reg), reg, data, mask);
  104. }
  105. phys_addr_t rsnd_gen_get_phy_addr(struct rsnd_priv *priv, int reg_id)
  106. {
  107. struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
  108. return gen->res[reg_id];
  109. }
  110. #define rsnd_gen_regmap_init(priv, id_size, reg_id, name, conf) \
  111. _rsnd_gen_regmap_init(priv, id_size, reg_id, name, conf, ARRAY_SIZE(conf))
  112. static int _rsnd_gen_regmap_init(struct rsnd_priv *priv,
  113. int id_size,
  114. int reg_id,
  115. const char *name,
  116. const struct rsnd_regmap_field_conf *conf,
  117. int conf_size)
  118. {
  119. struct platform_device *pdev = rsnd_priv_to_pdev(priv);
  120. struct rsnd_gen *gen = rsnd_priv_to_gen(priv);
  121. struct device *dev = rsnd_priv_to_dev(priv);
  122. struct resource *res;
  123. struct regmap_config regc;
  124. struct regmap_field *regs;
  125. struct regmap *regmap;
  126. struct reg_field regf;
  127. void __iomem *base;
  128. int i;
  129. memset(&regc, 0, sizeof(regc));
  130. regc.reg_bits = 32;
  131. regc.val_bits = 32;
  132. regc.reg_stride = 4;
  133. regc.name = name;
  134. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  135. if (!res)
  136. res = platform_get_resource(pdev, IORESOURCE_MEM, reg_id);
  137. if (!res)
  138. return -ENODEV;
  139. base = devm_ioremap_resource(dev, res);
  140. if (IS_ERR(base))
  141. return PTR_ERR(base);
  142. regmap = devm_regmap_init_mmio(dev, base, &regc);
  143. if (IS_ERR(regmap))
  144. return PTR_ERR(regmap);
  145. /* RSND_BASE_MAX base */
  146. gen->base[reg_id] = base;
  147. gen->regmap[reg_id] = regmap;
  148. gen->res[reg_id] = res->start;
  149. for (i = 0; i < conf_size; i++) {
  150. regf.reg = conf[i].reg_offset;
  151. regf.id_offset = conf[i].id_offset;
  152. regf.lsb = 0;
  153. regf.msb = 31;
  154. regf.id_size = id_size;
  155. regs = devm_regmap_field_alloc(dev, regmap, regf);
  156. if (IS_ERR(regs))
  157. return PTR_ERR(regs);
  158. /* RSND_REG_MAX base */
  159. gen->regs[conf[i].idx] = regs;
  160. gen->reg_name[conf[i].idx] = conf[i].reg_name;
  161. }
  162. return 0;
  163. }
  164. /*
  165. * Gen2
  166. */
  167. static int rsnd_gen2_probe(struct rsnd_priv *priv)
  168. {
  169. static const struct rsnd_regmap_field_conf conf_ssiu[] = {
  170. RSND_GEN_S_REG(SSI_MODE0, 0x800),
  171. RSND_GEN_S_REG(SSI_MODE1, 0x804),
  172. RSND_GEN_S_REG(SSI_MODE2, 0x808),
  173. RSND_GEN_S_REG(SSI_CONTROL, 0x810),
  174. /* FIXME: it needs SSI_MODE2/3 in the future */
  175. RSND_GEN_M_REG(SSI_BUSIF_MODE, 0x0, 0x80),
  176. RSND_GEN_M_REG(SSI_BUSIF_ADINR, 0x4, 0x80),
  177. RSND_GEN_M_REG(SSI_BUSIF_DALIGN,0x8, 0x80),
  178. RSND_GEN_M_REG(SSI_MODE, 0xc, 0x80),
  179. RSND_GEN_M_REG(SSI_CTRL, 0x10, 0x80),
  180. RSND_GEN_M_REG(SSI_INT_ENABLE, 0x18, 0x80),
  181. };
  182. static const struct rsnd_regmap_field_conf conf_scu[] = {
  183. RSND_GEN_M_REG(SRC_I_BUSIF_MODE,0x0, 0x20),
  184. RSND_GEN_M_REG(SRC_O_BUSIF_MODE,0x4, 0x20),
  185. RSND_GEN_M_REG(SRC_BUSIF_DALIGN,0x8, 0x20),
  186. RSND_GEN_M_REG(SRC_ROUTE_MODE0, 0xc, 0x20),
  187. RSND_GEN_M_REG(SRC_CTRL, 0x10, 0x20),
  188. RSND_GEN_M_REG(SRC_INT_ENABLE0, 0x18, 0x20),
  189. RSND_GEN_M_REG(CMD_BUSIF_DALIGN,0x188, 0x20),
  190. RSND_GEN_M_REG(CMD_ROUTE_SLCT, 0x18c, 0x20),
  191. RSND_GEN_M_REG(CMD_CTRL, 0x190, 0x20),
  192. RSND_GEN_S_REG(SCU_SYS_STATUS0, 0x1c8),
  193. RSND_GEN_S_REG(SCU_SYS_INT_EN0, 0x1cc),
  194. RSND_GEN_S_REG(SCU_SYS_STATUS1, 0x1d0),
  195. RSND_GEN_S_REG(SCU_SYS_INT_EN1, 0x1d4),
  196. RSND_GEN_M_REG(SRC_SWRSR, 0x200, 0x40),
  197. RSND_GEN_M_REG(SRC_SRCIR, 0x204, 0x40),
  198. RSND_GEN_M_REG(SRC_ADINR, 0x214, 0x40),
  199. RSND_GEN_M_REG(SRC_IFSCR, 0x21c, 0x40),
  200. RSND_GEN_M_REG(SRC_IFSVR, 0x220, 0x40),
  201. RSND_GEN_M_REG(SRC_SRCCR, 0x224, 0x40),
  202. RSND_GEN_M_REG(SRC_BSDSR, 0x22c, 0x40),
  203. RSND_GEN_M_REG(SRC_BSISR, 0x238, 0x40),
  204. RSND_GEN_M_REG(CTU_SWRSR, 0x500, 0x100),
  205. RSND_GEN_M_REG(CTU_CTUIR, 0x504, 0x100),
  206. RSND_GEN_M_REG(CTU_ADINR, 0x508, 0x100),
  207. RSND_GEN_M_REG(CTU_CPMDR, 0x510, 0x100),
  208. RSND_GEN_M_REG(CTU_SCMDR, 0x514, 0x100),
  209. RSND_GEN_M_REG(CTU_SV00R, 0x518, 0x100),
  210. RSND_GEN_M_REG(CTU_SV01R, 0x51c, 0x100),
  211. RSND_GEN_M_REG(CTU_SV02R, 0x520, 0x100),
  212. RSND_GEN_M_REG(CTU_SV03R, 0x524, 0x100),
  213. RSND_GEN_M_REG(CTU_SV04R, 0x528, 0x100),
  214. RSND_GEN_M_REG(CTU_SV05R, 0x52c, 0x100),
  215. RSND_GEN_M_REG(CTU_SV06R, 0x530, 0x100),
  216. RSND_GEN_M_REG(CTU_SV07R, 0x534, 0x100),
  217. RSND_GEN_M_REG(CTU_SV10R, 0x538, 0x100),
  218. RSND_GEN_M_REG(CTU_SV11R, 0x53c, 0x100),
  219. RSND_GEN_M_REG(CTU_SV12R, 0x540, 0x100),
  220. RSND_GEN_M_REG(CTU_SV13R, 0x544, 0x100),
  221. RSND_GEN_M_REG(CTU_SV14R, 0x548, 0x100),
  222. RSND_GEN_M_REG(CTU_SV15R, 0x54c, 0x100),
  223. RSND_GEN_M_REG(CTU_SV16R, 0x550, 0x100),
  224. RSND_GEN_M_REG(CTU_SV17R, 0x554, 0x100),
  225. RSND_GEN_M_REG(CTU_SV20R, 0x558, 0x100),
  226. RSND_GEN_M_REG(CTU_SV21R, 0x55c, 0x100),
  227. RSND_GEN_M_REG(CTU_SV22R, 0x560, 0x100),
  228. RSND_GEN_M_REG(CTU_SV23R, 0x564, 0x100),
  229. RSND_GEN_M_REG(CTU_SV24R, 0x568, 0x100),
  230. RSND_GEN_M_REG(CTU_SV25R, 0x56c, 0x100),
  231. RSND_GEN_M_REG(CTU_SV26R, 0x570, 0x100),
  232. RSND_GEN_M_REG(CTU_SV27R, 0x574, 0x100),
  233. RSND_GEN_M_REG(CTU_SV30R, 0x578, 0x100),
  234. RSND_GEN_M_REG(CTU_SV31R, 0x57c, 0x100),
  235. RSND_GEN_M_REG(CTU_SV32R, 0x580, 0x100),
  236. RSND_GEN_M_REG(CTU_SV33R, 0x584, 0x100),
  237. RSND_GEN_M_REG(CTU_SV34R, 0x588, 0x100),
  238. RSND_GEN_M_REG(CTU_SV35R, 0x58c, 0x100),
  239. RSND_GEN_M_REG(CTU_SV36R, 0x590, 0x100),
  240. RSND_GEN_M_REG(CTU_SV37R, 0x594, 0x100),
  241. RSND_GEN_M_REG(MIX_SWRSR, 0xd00, 0x40),
  242. RSND_GEN_M_REG(MIX_MIXIR, 0xd04, 0x40),
  243. RSND_GEN_M_REG(MIX_ADINR, 0xd08, 0x40),
  244. RSND_GEN_M_REG(MIX_MIXMR, 0xd10, 0x40),
  245. RSND_GEN_M_REG(MIX_MVPDR, 0xd14, 0x40),
  246. RSND_GEN_M_REG(MIX_MDBAR, 0xd18, 0x40),
  247. RSND_GEN_M_REG(MIX_MDBBR, 0xd1c, 0x40),
  248. RSND_GEN_M_REG(MIX_MDBCR, 0xd20, 0x40),
  249. RSND_GEN_M_REG(MIX_MDBDR, 0xd24, 0x40),
  250. RSND_GEN_M_REG(MIX_MDBER, 0xd28, 0x40),
  251. RSND_GEN_M_REG(DVC_SWRSR, 0xe00, 0x100),
  252. RSND_GEN_M_REG(DVC_DVUIR, 0xe04, 0x100),
  253. RSND_GEN_M_REG(DVC_ADINR, 0xe08, 0x100),
  254. RSND_GEN_M_REG(DVC_DVUCR, 0xe10, 0x100),
  255. RSND_GEN_M_REG(DVC_ZCMCR, 0xe14, 0x100),
  256. RSND_GEN_M_REG(DVC_VRCTR, 0xe18, 0x100),
  257. RSND_GEN_M_REG(DVC_VRPDR, 0xe1c, 0x100),
  258. RSND_GEN_M_REG(DVC_VRDBR, 0xe20, 0x100),
  259. RSND_GEN_M_REG(DVC_VOL0R, 0xe28, 0x100),
  260. RSND_GEN_M_REG(DVC_VOL1R, 0xe2c, 0x100),
  261. RSND_GEN_M_REG(DVC_VOL2R, 0xe30, 0x100),
  262. RSND_GEN_M_REG(DVC_VOL3R, 0xe34, 0x100),
  263. RSND_GEN_M_REG(DVC_VOL4R, 0xe38, 0x100),
  264. RSND_GEN_M_REG(DVC_VOL5R, 0xe3c, 0x100),
  265. RSND_GEN_M_REG(DVC_VOL6R, 0xe40, 0x100),
  266. RSND_GEN_M_REG(DVC_VOL7R, 0xe44, 0x100),
  267. RSND_GEN_M_REG(DVC_DVUER, 0xe48, 0x100),
  268. };
  269. static const struct rsnd_regmap_field_conf conf_adg[] = {
  270. RSND_GEN_S_REG(BRRA, 0x00),
  271. RSND_GEN_S_REG(BRRB, 0x04),
  272. RSND_GEN_S_REG(SSICKR, 0x08),
  273. RSND_GEN_S_REG(AUDIO_CLK_SEL0, 0x0c),
  274. RSND_GEN_S_REG(AUDIO_CLK_SEL1, 0x10),
  275. RSND_GEN_S_REG(AUDIO_CLK_SEL2, 0x14),
  276. RSND_GEN_S_REG(DIV_EN, 0x30),
  277. RSND_GEN_S_REG(SRCIN_TIMSEL0, 0x34),
  278. RSND_GEN_S_REG(SRCIN_TIMSEL1, 0x38),
  279. RSND_GEN_S_REG(SRCIN_TIMSEL2, 0x3c),
  280. RSND_GEN_S_REG(SRCIN_TIMSEL3, 0x40),
  281. RSND_GEN_S_REG(SRCIN_TIMSEL4, 0x44),
  282. RSND_GEN_S_REG(SRCOUT_TIMSEL0, 0x48),
  283. RSND_GEN_S_REG(SRCOUT_TIMSEL1, 0x4c),
  284. RSND_GEN_S_REG(SRCOUT_TIMSEL2, 0x50),
  285. RSND_GEN_S_REG(SRCOUT_TIMSEL3, 0x54),
  286. RSND_GEN_S_REG(SRCOUT_TIMSEL4, 0x58),
  287. RSND_GEN_S_REG(CMDOUT_TIMSEL, 0x5c),
  288. };
  289. static const struct rsnd_regmap_field_conf conf_ssi[] = {
  290. RSND_GEN_M_REG(SSICR, 0x00, 0x40),
  291. RSND_GEN_M_REG(SSISR, 0x04, 0x40),
  292. RSND_GEN_M_REG(SSITDR, 0x08, 0x40),
  293. RSND_GEN_M_REG(SSIRDR, 0x0c, 0x40),
  294. RSND_GEN_M_REG(SSIWSR, 0x20, 0x40),
  295. };
  296. int ret_ssiu;
  297. int ret_scu;
  298. int ret_adg;
  299. int ret_ssi;
  300. ret_ssiu = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SSIU, "ssiu", conf_ssiu);
  301. ret_scu = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SCU, "scu", conf_scu);
  302. ret_adg = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_ADG, "adg", conf_adg);
  303. ret_ssi = rsnd_gen_regmap_init(priv, 10, RSND_GEN2_SSI, "ssi", conf_ssi);
  304. if (ret_ssiu < 0 ||
  305. ret_scu < 0 ||
  306. ret_adg < 0 ||
  307. ret_ssi < 0)
  308. return ret_ssiu | ret_scu | ret_adg | ret_ssi;
  309. return 0;
  310. }
  311. /*
  312. * Gen1
  313. */
  314. static int rsnd_gen1_probe(struct rsnd_priv *priv)
  315. {
  316. static const struct rsnd_regmap_field_conf conf_adg[] = {
  317. RSND_GEN_S_REG(BRRA, 0x00),
  318. RSND_GEN_S_REG(BRRB, 0x04),
  319. RSND_GEN_S_REG(SSICKR, 0x08),
  320. RSND_GEN_S_REG(AUDIO_CLK_SEL0, 0x0c),
  321. RSND_GEN_S_REG(AUDIO_CLK_SEL1, 0x10),
  322. };
  323. static const struct rsnd_regmap_field_conf conf_ssi[] = {
  324. RSND_GEN_M_REG(SSICR, 0x00, 0x40),
  325. RSND_GEN_M_REG(SSISR, 0x04, 0x40),
  326. RSND_GEN_M_REG(SSITDR, 0x08, 0x40),
  327. RSND_GEN_M_REG(SSIRDR, 0x0c, 0x40),
  328. RSND_GEN_M_REG(SSIWSR, 0x20, 0x40),
  329. };
  330. int ret_adg;
  331. int ret_ssi;
  332. ret_adg = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_ADG, "adg", conf_adg);
  333. ret_ssi = rsnd_gen_regmap_init(priv, 9, RSND_GEN1_SSI, "ssi", conf_ssi);
  334. if (ret_adg < 0 ||
  335. ret_ssi < 0)
  336. return ret_adg | ret_ssi;
  337. return 0;
  338. }
  339. /*
  340. * Gen
  341. */
  342. int rsnd_gen_probe(struct rsnd_priv *priv)
  343. {
  344. struct device *dev = rsnd_priv_to_dev(priv);
  345. struct rsnd_gen *gen;
  346. int ret;
  347. gen = devm_kzalloc(dev, sizeof(*gen), GFP_KERNEL);
  348. if (!gen) {
  349. dev_err(dev, "GEN allocate failed\n");
  350. return -ENOMEM;
  351. }
  352. priv->gen = gen;
  353. ret = -ENODEV;
  354. if (rsnd_is_gen1(priv))
  355. ret = rsnd_gen1_probe(priv);
  356. else if (rsnd_is_gen2(priv))
  357. ret = rsnd_gen2_probe(priv);
  358. if (ret < 0)
  359. dev_err(dev, "unknown generation R-Car sound device\n");
  360. return ret;
  361. }