rsnd.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Renesas R-Car
  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. #ifndef RSND_H
  12. #define RSND_H
  13. #include <linux/clk.h>
  14. #include <linux/device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/io.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/sh_dma.h>
  22. #include <linux/workqueue.h>
  23. #include <sound/rcar_snd.h>
  24. #include <sound/soc.h>
  25. #include <sound/pcm_params.h>
  26. /*
  27. * pseudo register
  28. *
  29. * The register address offsets SRU/SCU/SSIU on Gen1/Gen2 are very different.
  30. * This driver uses pseudo register in order to hide it.
  31. * see gen1/gen2 for detail
  32. */
  33. enum rsnd_reg {
  34. /* SRU/SCU/SSIU */
  35. RSND_REG_SSI_MODE0,
  36. RSND_REG_SSI_MODE1,
  37. RSND_REG_SRC_BUSIF_MODE,
  38. RSND_REG_SRC_ROUTE_MODE0,
  39. RSND_REG_SRC_SWRSR,
  40. RSND_REG_SRC_SRCIR,
  41. RSND_REG_SRC_ADINR,
  42. RSND_REG_SRC_IFSCR,
  43. RSND_REG_SRC_IFSVR,
  44. RSND_REG_SRC_SRCCR,
  45. RSND_REG_CMD_ROUTE_SLCT,
  46. RSND_REG_DVC_SWRSR,
  47. RSND_REG_DVC_DVUIR,
  48. RSND_REG_DVC_ADINR,
  49. RSND_REG_DVC_DVUCR,
  50. RSND_REG_DVC_ZCMCR,
  51. RSND_REG_DVC_VOL0R,
  52. RSND_REG_DVC_VOL1R,
  53. RSND_REG_DVC_DVUER,
  54. /* ADG */
  55. RSND_REG_BRRA,
  56. RSND_REG_BRRB,
  57. RSND_REG_SSICKR,
  58. RSND_REG_AUDIO_CLK_SEL0,
  59. RSND_REG_AUDIO_CLK_SEL1,
  60. /* SSI */
  61. RSND_REG_SSICR,
  62. RSND_REG_SSISR,
  63. RSND_REG_SSITDR,
  64. RSND_REG_SSIRDR,
  65. RSND_REG_SSIWSR,
  66. /* SHARE see below */
  67. RSND_REG_SHARE01,
  68. RSND_REG_SHARE02,
  69. RSND_REG_SHARE03,
  70. RSND_REG_SHARE04,
  71. RSND_REG_SHARE05,
  72. RSND_REG_SHARE06,
  73. RSND_REG_SHARE07,
  74. RSND_REG_SHARE08,
  75. RSND_REG_SHARE09,
  76. RSND_REG_SHARE10,
  77. RSND_REG_SHARE11,
  78. RSND_REG_SHARE12,
  79. RSND_REG_SHARE13,
  80. RSND_REG_SHARE14,
  81. RSND_REG_SHARE15,
  82. RSND_REG_SHARE16,
  83. RSND_REG_SHARE17,
  84. RSND_REG_SHARE18,
  85. RSND_REG_SHARE19,
  86. RSND_REG_SHARE20,
  87. RSND_REG_SHARE21,
  88. RSND_REG_SHARE22,
  89. RSND_REG_MAX,
  90. };
  91. /* Gen1 only */
  92. #define RSND_REG_SRC_ROUTE_SEL RSND_REG_SHARE01
  93. #define RSND_REG_SRC_TMG_SEL0 RSND_REG_SHARE02
  94. #define RSND_REG_SRC_TMG_SEL1 RSND_REG_SHARE03
  95. #define RSND_REG_SRC_TMG_SEL2 RSND_REG_SHARE04
  96. #define RSND_REG_SRC_ROUTE_CTRL RSND_REG_SHARE05
  97. #define RSND_REG_SRC_MNFSR RSND_REG_SHARE06
  98. #define RSND_REG_AUDIO_CLK_SEL3 RSND_REG_SHARE07
  99. #define RSND_REG_AUDIO_CLK_SEL4 RSND_REG_SHARE08
  100. #define RSND_REG_AUDIO_CLK_SEL5 RSND_REG_SHARE09
  101. /* Gen2 only */
  102. #define RSND_REG_SRC_CTRL RSND_REG_SHARE01
  103. #define RSND_REG_SSI_CTRL RSND_REG_SHARE02
  104. #define RSND_REG_SSI_BUSIF_MODE RSND_REG_SHARE03
  105. #define RSND_REG_SSI_BUSIF_ADINR RSND_REG_SHARE04
  106. #define RSND_REG_INT_ENABLE RSND_REG_SHARE05
  107. #define RSND_REG_SRC_BSDSR RSND_REG_SHARE06
  108. #define RSND_REG_SRC_BSISR RSND_REG_SHARE07
  109. #define RSND_REG_DIV_EN RSND_REG_SHARE08
  110. #define RSND_REG_SRCIN_TIMSEL0 RSND_REG_SHARE09
  111. #define RSND_REG_SRCIN_TIMSEL1 RSND_REG_SHARE10
  112. #define RSND_REG_SRCIN_TIMSEL2 RSND_REG_SHARE11
  113. #define RSND_REG_SRCIN_TIMSEL3 RSND_REG_SHARE12
  114. #define RSND_REG_SRCIN_TIMSEL4 RSND_REG_SHARE13
  115. #define RSND_REG_SRCOUT_TIMSEL0 RSND_REG_SHARE14
  116. #define RSND_REG_SRCOUT_TIMSEL1 RSND_REG_SHARE15
  117. #define RSND_REG_SRCOUT_TIMSEL2 RSND_REG_SHARE16
  118. #define RSND_REG_SRCOUT_TIMSEL3 RSND_REG_SHARE17
  119. #define RSND_REG_SRCOUT_TIMSEL4 RSND_REG_SHARE18
  120. #define RSND_REG_AUDIO_CLK_SEL2 RSND_REG_SHARE19
  121. #define RSND_REG_CMD_CTRL RSND_REG_SHARE20
  122. #define RSND_REG_CMDOUT_TIMSEL RSND_REG_SHARE21
  123. #define RSND_REG_BUSIF_DALIGN RSND_REG_SHARE22
  124. struct rsnd_of_data;
  125. struct rsnd_priv;
  126. struct rsnd_mod;
  127. struct rsnd_dai;
  128. struct rsnd_dai_stream;
  129. /*
  130. * R-Car basic functions
  131. */
  132. #define rsnd_mod_read(m, r) \
  133. rsnd_read(rsnd_mod_to_priv(m), m, RSND_REG_##r)
  134. #define rsnd_mod_write(m, r, d) \
  135. rsnd_write(rsnd_mod_to_priv(m), m, RSND_REG_##r, d)
  136. #define rsnd_mod_bset(m, r, s, d) \
  137. rsnd_bset(rsnd_mod_to_priv(m), m, RSND_REG_##r, s, d)
  138. u32 rsnd_read(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg);
  139. void rsnd_write(struct rsnd_priv *priv, struct rsnd_mod *mod,
  140. enum rsnd_reg reg, u32 data);
  141. void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg,
  142. u32 mask, u32 data);
  143. u32 rsnd_get_adinr(struct rsnd_mod *mod);
  144. /*
  145. * R-Car DMA
  146. */
  147. struct rsnd_dma {
  148. struct sh_dmae_slave slave;
  149. struct dma_chan *chan;
  150. enum dma_transfer_direction dir;
  151. dma_addr_t addr;
  152. };
  153. void rsnd_dma_start(struct rsnd_dma *dma);
  154. void rsnd_dma_stop(struct rsnd_dma *dma);
  155. int rsnd_dma_available(struct rsnd_dma *dma);
  156. int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
  157. int is_play, int id);
  158. void rsnd_dma_quit(struct rsnd_priv *priv,
  159. struct rsnd_dma *dma);
  160. /*
  161. * R-Car sound mod
  162. */
  163. enum rsnd_mod_type {
  164. RSND_MOD_SRC = 0,
  165. RSND_MOD_SSI,
  166. RSND_MOD_DVC,
  167. RSND_MOD_MAX,
  168. };
  169. struct rsnd_mod_ops {
  170. char *name;
  171. char* (*dma_name)(struct rsnd_mod *mod);
  172. int (*probe)(struct rsnd_mod *mod,
  173. struct rsnd_dai *rdai);
  174. int (*remove)(struct rsnd_mod *mod,
  175. struct rsnd_dai *rdai);
  176. int (*init)(struct rsnd_mod *mod,
  177. struct rsnd_dai *rdai);
  178. int (*quit)(struct rsnd_mod *mod,
  179. struct rsnd_dai *rdai);
  180. int (*start)(struct rsnd_mod *mod,
  181. struct rsnd_dai *rdai);
  182. int (*stop)(struct rsnd_mod *mod,
  183. struct rsnd_dai *rdai);
  184. int (*pcm_new)(struct rsnd_mod *mod,
  185. struct rsnd_dai *rdai,
  186. struct snd_soc_pcm_runtime *rtd);
  187. };
  188. struct rsnd_dai_stream;
  189. struct rsnd_mod {
  190. int id;
  191. enum rsnd_mod_type type;
  192. struct rsnd_priv *priv;
  193. struct rsnd_mod_ops *ops;
  194. struct rsnd_dma dma;
  195. struct rsnd_dai_stream *io;
  196. };
  197. #define rsnd_mod_to_priv(mod) ((mod)->priv)
  198. #define rsnd_mod_to_dma(mod) (&(mod)->dma)
  199. #define rsnd_dma_to_mod(_dma) container_of((_dma), struct rsnd_mod, dma)
  200. #define rsnd_mod_to_io(mod) ((mod)->io)
  201. #define rsnd_mod_id(mod) ((mod)->id)
  202. void rsnd_mod_init(struct rsnd_priv *priv,
  203. struct rsnd_mod *mod,
  204. struct rsnd_mod_ops *ops,
  205. enum rsnd_mod_type type,
  206. int id);
  207. char *rsnd_mod_name(struct rsnd_mod *mod);
  208. char *rsnd_mod_dma_name(struct rsnd_mod *mod);
  209. /*
  210. * R-Car sound DAI
  211. */
  212. #define RSND_DAI_NAME_SIZE 16
  213. struct rsnd_dai_stream {
  214. struct snd_pcm_substream *substream;
  215. struct rsnd_mod *mod[RSND_MOD_MAX];
  216. struct rsnd_dai_path_info *info; /* rcar_snd.h */
  217. int byte_pos;
  218. int period_pos;
  219. int byte_per_period;
  220. int next_period_byte;
  221. };
  222. #define rsnd_io_to_mod_ssi(io) ((io)->mod[RSND_MOD_SSI])
  223. #define rsnd_io_to_mod_src(io) ((io)->mod[RSND_MOD_SRC])
  224. #define rsnd_io_to_mod_dvc(io) ((io)->mod[RSND_MOD_DVC])
  225. struct rsnd_dai {
  226. char name[RSND_DAI_NAME_SIZE];
  227. struct rsnd_dai_platform_info *info; /* rcar_snd.h */
  228. struct rsnd_dai_stream playback;
  229. struct rsnd_dai_stream capture;
  230. unsigned int clk_master:1;
  231. unsigned int bit_clk_inv:1;
  232. unsigned int frm_clk_inv:1;
  233. unsigned int sys_delay:1;
  234. unsigned int data_alignment:1;
  235. };
  236. #define rsnd_rdai_nr(priv) ((priv)->rdai_nr)
  237. #define for_each_rsnd_dai(rdai, priv, i) \
  238. for (i = 0; \
  239. (i < rsnd_rdai_nr(priv)) && \
  240. ((rdai) = rsnd_dai_get(priv, i)); \
  241. i++)
  242. struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id);
  243. int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io);
  244. int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai);
  245. #define rsnd_dai_get_platform_info(rdai) ((rdai)->info)
  246. #define rsnd_io_to_runtime(io) ((io)->substream->runtime)
  247. void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int cnt);
  248. int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional);
  249. #define rsnd_dai_is_clk_master(rdai) ((rdai)->clk_master)
  250. /*
  251. * R-Car Gen1/Gen2
  252. */
  253. int rsnd_gen_probe(struct platform_device *pdev,
  254. const struct rsnd_of_data *of_data,
  255. struct rsnd_priv *priv);
  256. void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv,
  257. struct rsnd_mod *mod,
  258. enum rsnd_reg reg);
  259. dma_addr_t rsnd_gen_dma_addr(struct rsnd_priv *priv,
  260. struct rsnd_mod *mod,
  261. int is_play, int is_from);
  262. #define rsnd_is_gen1(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN1)
  263. #define rsnd_is_gen2(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN2)
  264. /*
  265. * R-Car ADG
  266. */
  267. int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod);
  268. int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate);
  269. int rsnd_adg_probe(struct platform_device *pdev,
  270. const struct rsnd_of_data *of_data,
  271. struct rsnd_priv *priv);
  272. int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv,
  273. struct rsnd_mod *mod,
  274. unsigned int src_rate,
  275. unsigned int dst_rate);
  276. int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *mod,
  277. struct rsnd_dai *rdai,
  278. struct rsnd_dai_stream *io,
  279. unsigned int src_rate,
  280. unsigned int dst_rate);
  281. int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *mod,
  282. struct rsnd_dai *rdai,
  283. struct rsnd_dai_stream *io);
  284. int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_dai *rdai,
  285. struct rsnd_mod *mod,
  286. struct rsnd_dai_stream *io);
  287. /*
  288. * R-Car sound priv
  289. */
  290. struct rsnd_of_data {
  291. u32 flags;
  292. };
  293. struct rsnd_priv {
  294. struct platform_device *pdev;
  295. struct rcar_snd_info *info;
  296. spinlock_t lock;
  297. /*
  298. * below value will be filled on rsnd_gen_probe()
  299. */
  300. void *gen;
  301. /*
  302. * below value will be filled on rsnd_src_probe()
  303. */
  304. void *src;
  305. int src_nr;
  306. /*
  307. * below value will be filled on rsnd_adg_probe()
  308. */
  309. void *adg;
  310. /*
  311. * below value will be filled on rsnd_ssi_probe()
  312. */
  313. void *ssi;
  314. int ssi_nr;
  315. /*
  316. * below value will be filled on rsnd_dvc_probe()
  317. */
  318. void *dvc;
  319. int dvc_nr;
  320. /*
  321. * below value will be filled on rsnd_dai_probe()
  322. */
  323. struct snd_soc_dai_driver *daidrv;
  324. struct rsnd_dai *rdai;
  325. int rdai_nr;
  326. };
  327. #define rsnd_priv_to_pdev(priv) ((priv)->pdev)
  328. #define rsnd_priv_to_dev(priv) (&(rsnd_priv_to_pdev(priv)->dev))
  329. #define rsnd_priv_to_info(priv) ((priv)->info)
  330. #define rsnd_lock(priv, flags) spin_lock_irqsave(&priv->lock, flags)
  331. #define rsnd_unlock(priv, flags) spin_unlock_irqrestore(&priv->lock, flags)
  332. #define rsnd_info_is_playback(priv, type) \
  333. ({ \
  334. struct rcar_snd_info *info = rsnd_priv_to_info(priv); \
  335. int i, is_play = 0; \
  336. for (i = 0; i < info->dai_info_nr; i++) { \
  337. if (info->dai_info[i].playback.type == (type)->info) { \
  338. is_play = 1; \
  339. break; \
  340. } \
  341. } \
  342. is_play; \
  343. })
  344. /*
  345. * R-Car SRC
  346. */
  347. int rsnd_src_probe(struct platform_device *pdev,
  348. const struct rsnd_of_data *of_data,
  349. struct rsnd_priv *priv);
  350. struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id);
  351. unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
  352. struct rsnd_dai_stream *io,
  353. struct snd_pcm_runtime *runtime);
  354. int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
  355. struct rsnd_dai *rdai,
  356. int use_busif);
  357. int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
  358. struct rsnd_dai *rdai,
  359. int use_busif);
  360. int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
  361. struct rsnd_dai *rdai);
  362. #define rsnd_src_nr(priv) ((priv)->src_nr)
  363. /*
  364. * R-Car SSI
  365. */
  366. int rsnd_ssi_probe(struct platform_device *pdev,
  367. const struct rsnd_of_data *of_data,
  368. struct rsnd_priv *priv);
  369. struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id);
  370. int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod);
  371. /*
  372. * R-Car DVC
  373. */
  374. int rsnd_dvc_probe(struct platform_device *pdev,
  375. const struct rsnd_of_data *of_data,
  376. struct rsnd_priv *priv);
  377. void rsnd_dvc_remove(struct platform_device *pdev,
  378. struct rsnd_priv *priv);
  379. struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id);
  380. #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
  381. #endif