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 14 #include <linux/clk.h> 15 #include <linux/device.h> 16 #include <linux/dma-mapping.h> 17 #include <linux/io.h> 18 #include <linux/list.h> 19 #include <linux/module.h> 20 #include <linux/sh_dma.h> 21 #include <linux/workqueue.h> 22 #include <sound/rcar_snd.h> 23 #include <sound/soc.h> 24 #include <sound/pcm_params.h> 25 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_SRC_ROUTE_SEL, /* for Gen1 */ 36 RSND_REG_SRC_TMG_SEL0, /* for Gen1 */ 37 RSND_REG_SRC_TMG_SEL1, /* for Gen1 */ 38 RSND_REG_SRC_TMG_SEL2, /* for Gen1 */ 39 RSND_REG_SRC_ROUTE_CTRL, /* for Gen1 */ 40 RSND_REG_SRC_CTRL, /* for Gen2 */ 41 RSND_REG_SSI_CTRL, /* for Gen2 */ 42 RSND_REG_SSI_CONTROL, 43 RSND_REG_SSI_BUSIF_MODE, /* for Gen2 */ 44 RSND_REG_SSI_BUSIF_ADINR, /* for Gen2 */ 45 RSND_REG_SSI_MODE0, 46 RSND_REG_SSI_MODE1, 47 RSND_REG_INT_ENABLE, /* for Gen2 */ 48 RSND_REG_SRC_BUSIF_MODE, 49 RSND_REG_SRC_ROUTE_MODE0, 50 RSND_REG_SRC_SWRSR, 51 RSND_REG_SRC_SRCIR, 52 RSND_REG_SRC_ADINR, 53 RSND_REG_SRC_IFSCR, 54 RSND_REG_SRC_IFSVR, 55 RSND_REG_SRC_SRCCR, 56 RSND_REG_SRC_MNFSR, /* for Gen1 */ 57 RSND_REG_SRC_BSDSR, /* for Gen2 */ 58 RSND_REG_SRC_BSISR, /* for Gen2 */ 59 60 /* ADG */ 61 RSND_REG_BRRA, 62 RSND_REG_BRRB, 63 RSND_REG_SSICKR, 64 RSND_REG_AUDIO_CLK_SEL0, 65 RSND_REG_AUDIO_CLK_SEL1, 66 RSND_REG_AUDIO_CLK_SEL2, 67 RSND_REG_AUDIO_CLK_SEL3, /* for Gen1 */ 68 RSND_REG_AUDIO_CLK_SEL4, /* for Gen1 */ 69 RSND_REG_AUDIO_CLK_SEL5, /* for Gen1 */ 70 RSND_REG_SRCIN_TIMSEL0, /* for Gen2 */ 71 RSND_REG_SRCIN_TIMSEL1, /* for Gen2 */ 72 RSND_REG_SRCIN_TIMSEL2, /* for Gen2 */ 73 RSND_REG_SRCIN_TIMSEL3, /* for Gen2 */ 74 RSND_REG_SRCIN_TIMSEL4, /* for Gen2 */ 75 RSND_REG_SRCOUT_TIMSEL0, /* for Gen2 */ 76 RSND_REG_SRCOUT_TIMSEL1, /* for Gen2 */ 77 RSND_REG_SRCOUT_TIMSEL2, /* for Gen2 */ 78 RSND_REG_SRCOUT_TIMSEL3, /* for Gen2 */ 79 RSND_REG_SRCOUT_TIMSEL4, /* for Gen2 */ 80 81 /* SSI */ 82 RSND_REG_SSICR, 83 RSND_REG_SSISR, 84 RSND_REG_SSITDR, 85 RSND_REG_SSIRDR, 86 RSND_REG_SSIWSR, 87 88 RSND_REG_MAX, 89 }; 90 91 struct rsnd_priv; 92 struct rsnd_mod; 93 struct rsnd_dai; 94 struct rsnd_dai_stream; 95 96 /* 97 * R-Car basic functions 98 */ 99 #define rsnd_mod_read(m, r) \ 100 rsnd_read(rsnd_mod_to_priv(m), m, RSND_REG_##r) 101 #define rsnd_mod_write(m, r, d) \ 102 rsnd_write(rsnd_mod_to_priv(m), m, RSND_REG_##r, d) 103 #define rsnd_mod_bset(m, r, s, d) \ 104 rsnd_bset(rsnd_mod_to_priv(m), m, RSND_REG_##r, s, d) 105 106 u32 rsnd_read(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg); 107 void rsnd_write(struct rsnd_priv *priv, struct rsnd_mod *mod, 108 enum rsnd_reg reg, u32 data); 109 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg, 110 u32 mask, u32 data); 111 112 /* 113 * R-Car DMA 114 */ 115 struct rsnd_dma { 116 struct sh_dmae_slave slave; 117 struct work_struct work; 118 struct dma_chan *chan; 119 enum dma_data_direction dir; 120 121 int submit_loop; 122 int offset; /* it cares A/B plane */ 123 }; 124 125 void rsnd_dma_start(struct rsnd_dma *dma); 126 void rsnd_dma_stop(struct rsnd_dma *dma); 127 int rsnd_dma_available(struct rsnd_dma *dma); 128 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, 129 int is_play, int id); 130 void rsnd_dma_quit(struct rsnd_priv *priv, 131 struct rsnd_dma *dma); 132 133 134 /* 135 * R-Car sound mod 136 */ 137 138 struct rsnd_mod_ops { 139 char *name; 140 int (*init)(struct rsnd_mod *mod, 141 struct rsnd_dai *rdai, 142 struct rsnd_dai_stream *io); 143 int (*quit)(struct rsnd_mod *mod, 144 struct rsnd_dai *rdai, 145 struct rsnd_dai_stream *io); 146 int (*start)(struct rsnd_mod *mod, 147 struct rsnd_dai *rdai, 148 struct rsnd_dai_stream *io); 149 int (*stop)(struct rsnd_mod *mod, 150 struct rsnd_dai *rdai, 151 struct rsnd_dai_stream *io); 152 }; 153 154 struct rsnd_dai_stream; 155 struct rsnd_mod { 156 int id; 157 struct rsnd_priv *priv; 158 struct rsnd_mod_ops *ops; 159 struct list_head list; /* connect to rsnd_dai playback/capture */ 160 struct rsnd_dma dma; 161 struct rsnd_dai_stream *io; 162 }; 163 164 #define rsnd_mod_to_priv(mod) ((mod)->priv) 165 #define rsnd_mod_to_dma(mod) (&(mod)->dma) 166 #define rsnd_dma_to_mod(_dma) container_of((_dma), struct rsnd_mod, dma) 167 #define rsnd_mod_to_io(mod) ((mod)->io) 168 #define rsnd_mod_id(mod) ((mod)->id) 169 #define for_each_rsnd_mod(pos, n, io) \ 170 list_for_each_entry_safe(pos, n, &(io)->head, list) 171 #define __rsnd_mod_call(mod, func, rdai, io) \ 172 ({ \ 173 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \ 174 struct device *dev = rsnd_priv_to_dev(priv); \ 175 dev_dbg(dev, "%s-%d-%s\n", \ 176 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \ 177 (mod)->ops->func(mod, rdai, io); \ 178 }) 179 180 #define rsnd_mod_call(mod, func, rdai, io) \ 181 (!(mod) ? -ENODEV : \ 182 !((mod)->ops->func) ? 0 : \ 183 __rsnd_mod_call(mod, func, rdai, io)) 184 185 void rsnd_mod_init(struct rsnd_priv *priv, 186 struct rsnd_mod *mod, 187 struct rsnd_mod_ops *ops, 188 int id); 189 char *rsnd_mod_name(struct rsnd_mod *mod); 190 191 /* 192 * R-Car sound DAI 193 */ 194 #define RSND_DAI_NAME_SIZE 16 195 struct rsnd_dai_stream { 196 struct list_head head; /* head of rsnd_mod list */ 197 struct snd_pcm_substream *substream; 198 int byte_pos; 199 int period_pos; 200 int byte_per_period; 201 int next_period_byte; 202 }; 203 204 struct rsnd_dai { 205 char name[RSND_DAI_NAME_SIZE]; 206 struct rsnd_dai_platform_info *info; /* rcar_snd.h */ 207 struct rsnd_dai_stream playback; 208 struct rsnd_dai_stream capture; 209 210 unsigned int clk_master:1; 211 unsigned int bit_clk_inv:1; 212 unsigned int frm_clk_inv:1; 213 unsigned int sys_delay:1; 214 unsigned int data_alignment:1; 215 }; 216 217 #define rsnd_dai_nr(priv) ((priv)->dai_nr) 218 #define for_each_rsnd_dai(rdai, priv, i) \ 219 for (i = 0, (rdai) = rsnd_dai_get(priv, i); \ 220 i < rsnd_dai_nr(priv); \ 221 i++, (rdai) = rsnd_dai_get(priv, i)) 222 223 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id); 224 int rsnd_dai_disconnect(struct rsnd_mod *mod); 225 int rsnd_dai_connect(struct rsnd_dai *rdai, struct rsnd_mod *mod, 226 struct rsnd_dai_stream *io); 227 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io); 228 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai); 229 #define rsnd_dai_get_platform_info(rdai) ((rdai)->info) 230 #define rsnd_io_to_runtime(io) ((io)->substream->runtime) 231 232 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int cnt); 233 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional); 234 #define rsnd_dai_is_clk_master(rdai) ((rdai)->clk_master) 235 236 /* 237 * R-Car Gen1/Gen2 238 */ 239 int rsnd_gen_probe(struct platform_device *pdev, 240 struct rcar_snd_info *info, 241 struct rsnd_priv *priv); 242 void rsnd_gen_remove(struct platform_device *pdev, 243 struct rsnd_priv *priv); 244 int rsnd_gen_path_init(struct rsnd_priv *priv, 245 struct rsnd_dai *rdai, 246 struct rsnd_dai_stream *io); 247 int rsnd_gen_path_exit(struct rsnd_priv *priv, 248 struct rsnd_dai *rdai, 249 struct rsnd_dai_stream *io); 250 void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv, 251 struct rsnd_mod *mod, 252 enum rsnd_reg reg); 253 #define rsnd_is_gen1(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN1) 254 #define rsnd_is_gen2(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN2) 255 256 /* 257 * R-Car ADG 258 */ 259 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod); 260 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate); 261 int rsnd_adg_probe(struct platform_device *pdev, 262 struct rcar_snd_info *info, 263 struct rsnd_priv *priv); 264 void rsnd_adg_remove(struct platform_device *pdev, 265 struct rsnd_priv *priv); 266 int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv, 267 struct rsnd_mod *mod, 268 unsigned int src_rate, 269 unsigned int dst_rate); 270 int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *mod, 271 struct rsnd_dai *rdai, 272 struct rsnd_dai_stream *io, 273 unsigned int src_rate, 274 unsigned int dst_rate); 275 int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *mod, 276 struct rsnd_dai *rdai, 277 struct rsnd_dai_stream *io); 278 279 /* 280 * R-Car sound priv 281 */ 282 struct rsnd_priv { 283 284 struct device *dev; 285 struct rcar_snd_info *info; 286 spinlock_t lock; 287 288 /* 289 * below value will be filled on rsnd_gen_probe() 290 */ 291 void *gen; 292 293 /* 294 * below value will be filled on rsnd_scu_probe() 295 */ 296 void *scu; 297 int scu_nr; 298 299 /* 300 * below value will be filled on rsnd_adg_probe() 301 */ 302 void *adg; 303 304 /* 305 * below value will be filled on rsnd_ssi_probe() 306 */ 307 void *ssi; 308 int ssi_nr; 309 310 /* 311 * below value will be filled on rsnd_dai_probe() 312 */ 313 struct snd_soc_dai_driver *daidrv; 314 struct rsnd_dai *rdai; 315 int dai_nr; 316 }; 317 318 #define rsnd_priv_to_dev(priv) ((priv)->dev) 319 #define rsnd_lock(priv, flags) spin_lock_irqsave(&priv->lock, flags) 320 #define rsnd_unlock(priv, flags) spin_unlock_irqrestore(&priv->lock, flags) 321 322 /* 323 * R-Car SCU 324 */ 325 int rsnd_scu_probe(struct platform_device *pdev, 326 struct rcar_snd_info *info, 327 struct rsnd_priv *priv); 328 void rsnd_scu_remove(struct platform_device *pdev, 329 struct rsnd_priv *priv); 330 struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id); 331 unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv, 332 struct rsnd_mod *ssi_mod, 333 struct snd_pcm_runtime *runtime); 334 335 #define rsnd_scu_nr(priv) ((priv)->scu_nr) 336 337 /* 338 * R-Car SSI 339 */ 340 int rsnd_ssi_probe(struct platform_device *pdev, 341 struct rcar_snd_info *info, 342 struct rsnd_priv *priv); 343 void rsnd_ssi_remove(struct platform_device *pdev, 344 struct rsnd_priv *priv); 345 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id); 346 struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv, 347 int dai_id, int is_play); 348 int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod); 349 int rsnd_ssi_is_play(struct rsnd_mod *mod); 350 351 #endif 352