Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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> |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 16 | #include <linux/dma-mapping.h> |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 20 | #include <linux/sh_dma.h> |
| 21 | #include <linux/workqueue.h> |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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 | */ |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 33 | enum rsnd_reg { |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 34 | /* SRU/SCU */ |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 35 | RSND_REG_SRC_ROUTE_SEL, |
| 36 | RSND_REG_SRC_TMG_SEL0, |
| 37 | RSND_REG_SRC_TMG_SEL1, |
| 38 | RSND_REG_SRC_TMG_SEL2, |
| 39 | RSND_REG_SRC_CTRL, |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 40 | RSND_REG_SSI_MODE0, |
| 41 | RSND_REG_SSI_MODE1, |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 42 | RSND_REG_BUSIF_MODE, |
| 43 | RSND_REG_BUSIF_ADINR, |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 44 | |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 45 | /* ADG */ |
| 46 | RSND_REG_BRRA, |
| 47 | RSND_REG_BRRB, |
| 48 | RSND_REG_SSICKR, |
| 49 | RSND_REG_AUDIO_CLK_SEL0, |
| 50 | RSND_REG_AUDIO_CLK_SEL1, |
| 51 | RSND_REG_AUDIO_CLK_SEL2, |
| 52 | RSND_REG_AUDIO_CLK_SEL3, |
| 53 | RSND_REG_AUDIO_CLK_SEL4, |
| 54 | RSND_REG_AUDIO_CLK_SEL5, |
| 55 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 56 | /* SSI */ |
| 57 | RSND_REG_SSICR, |
| 58 | RSND_REG_SSISR, |
| 59 | RSND_REG_SSITDR, |
| 60 | RSND_REG_SSIRDR, |
| 61 | RSND_REG_SSIWSR, |
| 62 | |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 63 | RSND_REG_MAX, |
| 64 | }; |
| 65 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 66 | struct rsnd_priv; |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 67 | struct rsnd_mod; |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 68 | struct rsnd_dai; |
| 69 | struct rsnd_dai_stream; |
| 70 | |
| 71 | /* |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 72 | * R-Car basic functions |
| 73 | */ |
| 74 | #define rsnd_mod_read(m, r) \ |
| 75 | rsnd_read(rsnd_mod_to_priv(m), m, RSND_REG_##r) |
| 76 | #define rsnd_mod_write(m, r, d) \ |
| 77 | rsnd_write(rsnd_mod_to_priv(m), m, RSND_REG_##r, d) |
| 78 | #define rsnd_mod_bset(m, r, s, d) \ |
| 79 | rsnd_bset(rsnd_mod_to_priv(m), m, RSND_REG_##r, s, d) |
| 80 | |
| 81 | #define rsnd_priv_read(p, r) rsnd_read(p, NULL, RSND_REG_##r) |
| 82 | #define rsnd_priv_write(p, r, d) rsnd_write(p, NULL, RSND_REG_##r, d) |
| 83 | #define rsnd_priv_bset(p, r, s, d) rsnd_bset(p, NULL, RSND_REG_##r, s, d) |
| 84 | |
| 85 | u32 rsnd_read(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg); |
| 86 | void rsnd_write(struct rsnd_priv *priv, struct rsnd_mod *mod, |
| 87 | enum rsnd_reg reg, u32 data); |
| 88 | void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg, |
| 89 | u32 mask, u32 data); |
| 90 | |
| 91 | /* |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 92 | * R-Car DMA |
| 93 | */ |
| 94 | struct rsnd_dma { |
| 95 | struct rsnd_priv *priv; |
| 96 | struct sh_dmae_slave slave; |
| 97 | struct work_struct work; |
| 98 | struct dma_chan *chan; |
| 99 | enum dma_data_direction dir; |
| 100 | int (*inquiry)(struct rsnd_dma *dma, dma_addr_t *buf, int *len); |
| 101 | int (*complete)(struct rsnd_dma *dma); |
| 102 | |
| 103 | int submit_loop; |
| 104 | }; |
| 105 | |
| 106 | void rsnd_dma_start(struct rsnd_dma *dma); |
| 107 | void rsnd_dma_stop(struct rsnd_dma *dma); |
| 108 | int rsnd_dma_available(struct rsnd_dma *dma); |
| 109 | int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, |
| 110 | int is_play, int id, |
| 111 | int (*inquiry)(struct rsnd_dma *dma, dma_addr_t *buf, int *len), |
| 112 | int (*complete)(struct rsnd_dma *dma)); |
| 113 | void rsnd_dma_quit(struct rsnd_priv *priv, |
| 114 | struct rsnd_dma *dma); |
| 115 | |
| 116 | |
| 117 | /* |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 118 | * R-Car sound mod |
| 119 | */ |
| 120 | |
| 121 | struct rsnd_mod_ops { |
| 122 | char *name; |
| 123 | int (*init)(struct rsnd_mod *mod, |
| 124 | struct rsnd_dai *rdai, |
| 125 | struct rsnd_dai_stream *io); |
| 126 | int (*quit)(struct rsnd_mod *mod, |
| 127 | struct rsnd_dai *rdai, |
| 128 | struct rsnd_dai_stream *io); |
| 129 | int (*start)(struct rsnd_mod *mod, |
| 130 | struct rsnd_dai *rdai, |
| 131 | struct rsnd_dai_stream *io); |
| 132 | int (*stop)(struct rsnd_mod *mod, |
| 133 | struct rsnd_dai *rdai, |
| 134 | struct rsnd_dai_stream *io); |
| 135 | }; |
| 136 | |
| 137 | struct rsnd_mod { |
| 138 | int id; |
| 139 | struct rsnd_priv *priv; |
| 140 | struct rsnd_mod_ops *ops; |
| 141 | struct list_head list; /* connect to rsnd_dai playback/capture */ |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 142 | struct rsnd_dma dma; |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | #define rsnd_mod_to_priv(mod) ((mod)->priv) |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 146 | #define rsnd_mod_to_dma(mod) (&(mod)->dma) |
| 147 | #define rsnd_dma_to_mod(_dma) container_of((_dma), struct rsnd_mod, dma) |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 148 | #define rsnd_mod_id(mod) ((mod)->id) |
| 149 | #define for_each_rsnd_mod(pos, n, io) \ |
| 150 | list_for_each_entry_safe(pos, n, &(io)->head, list) |
| 151 | #define rsnd_mod_call(mod, func, rdai, io) \ |
| 152 | (!(mod) ? -ENODEV : \ |
| 153 | !((mod)->ops->func) ? 0 : \ |
| 154 | (mod)->ops->func(mod, rdai, io)) |
| 155 | |
| 156 | void rsnd_mod_init(struct rsnd_priv *priv, |
| 157 | struct rsnd_mod *mod, |
| 158 | struct rsnd_mod_ops *ops, |
| 159 | int id); |
| 160 | char *rsnd_mod_name(struct rsnd_mod *mod); |
| 161 | |
| 162 | /* |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 163 | * R-Car sound DAI |
| 164 | */ |
| 165 | #define RSND_DAI_NAME_SIZE 16 |
| 166 | struct rsnd_dai_stream { |
| 167 | struct list_head head; /* head of rsnd_mod list */ |
| 168 | struct snd_pcm_substream *substream; |
| 169 | int byte_pos; |
| 170 | int period_pos; |
| 171 | int byte_per_period; |
| 172 | int next_period_byte; |
| 173 | }; |
| 174 | |
| 175 | struct rsnd_dai { |
| 176 | char name[RSND_DAI_NAME_SIZE]; |
| 177 | struct rsnd_dai_platform_info *info; /* rcar_snd.h */ |
| 178 | struct rsnd_dai_stream playback; |
| 179 | struct rsnd_dai_stream capture; |
| 180 | |
| 181 | int clk_master:1; |
| 182 | int bit_clk_inv:1; |
| 183 | int frm_clk_inv:1; |
| 184 | int sys_delay:1; |
| 185 | int data_alignment:1; |
| 186 | }; |
| 187 | |
| 188 | #define rsnd_dai_nr(priv) ((priv)->dai_nr) |
| 189 | #define for_each_rsnd_dai(rdai, priv, i) \ |
| 190 | for (i = 0, (rdai) = rsnd_dai_get(priv, i); \ |
| 191 | i < rsnd_dai_nr(priv); \ |
| 192 | i++, (rdai) = rsnd_dai_get(priv, i)) |
| 193 | |
| 194 | struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id); |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 195 | int rsnd_dai_disconnect(struct rsnd_mod *mod); |
| 196 | int rsnd_dai_connect(struct rsnd_dai *rdai, struct rsnd_mod *mod, |
| 197 | struct rsnd_dai_stream *io); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 198 | int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io); |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 199 | int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 200 | #define rsnd_dai_get_platform_info(rdai) ((rdai)->info) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 201 | #define rsnd_io_to_runtime(io) ((io)->substream->runtime) |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 202 | |
| 203 | void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int cnt); |
| 204 | int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional); |
| 205 | |
| 206 | /* |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 207 | * R-Car Gen1/Gen2 |
| 208 | */ |
| 209 | int rsnd_gen_probe(struct platform_device *pdev, |
| 210 | struct rcar_snd_info *info, |
| 211 | struct rsnd_priv *priv); |
| 212 | void rsnd_gen_remove(struct platform_device *pdev, |
| 213 | struct rsnd_priv *priv); |
| 214 | int rsnd_gen_path_init(struct rsnd_priv *priv, |
| 215 | struct rsnd_dai *rdai, |
| 216 | struct rsnd_dai_stream *io); |
| 217 | int rsnd_gen_path_exit(struct rsnd_priv *priv, |
| 218 | struct rsnd_dai *rdai, |
| 219 | struct rsnd_dai_stream *io); |
| 220 | void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv, |
| 221 | struct rsnd_mod *mod, |
| 222 | enum rsnd_reg reg); |
Kuninori Morimoto | c5d5a58 | 2013-10-11 00:07:01 -0700 | [diff] [blame] | 223 | #define rsnd_is_gen1(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN1) |
| 224 | #define rsnd_is_gen2(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN2) |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 225 | |
| 226 | /* |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 227 | * R-Car ADG |
| 228 | */ |
| 229 | int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod); |
| 230 | int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate); |
| 231 | int rsnd_adg_probe(struct platform_device *pdev, |
| 232 | struct rcar_snd_info *info, |
| 233 | struct rsnd_priv *priv); |
| 234 | void rsnd_adg_remove(struct platform_device *pdev, |
| 235 | struct rsnd_priv *priv); |
| 236 | |
| 237 | /* |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 238 | * R-Car sound priv |
| 239 | */ |
| 240 | struct rsnd_priv { |
| 241 | |
| 242 | struct device *dev; |
| 243 | struct rcar_snd_info *info; |
| 244 | spinlock_t lock; |
| 245 | |
| 246 | /* |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 247 | * below value will be filled on rsnd_gen_probe() |
| 248 | */ |
| 249 | void *gen; |
| 250 | |
| 251 | /* |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 252 | * below value will be filled on rsnd_scu_probe() |
| 253 | */ |
| 254 | void *scu; |
| 255 | int scu_nr; |
| 256 | |
| 257 | /* |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 258 | * below value will be filled on rsnd_adg_probe() |
| 259 | */ |
| 260 | void *adg; |
| 261 | |
| 262 | /* |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 263 | * below value will be filled on rsnd_ssi_probe() |
| 264 | */ |
| 265 | void *ssiu; |
| 266 | |
| 267 | /* |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 268 | * below value will be filled on rsnd_dai_probe() |
| 269 | */ |
| 270 | struct snd_soc_dai_driver *daidrv; |
| 271 | struct rsnd_dai *rdai; |
| 272 | int dai_nr; |
| 273 | }; |
| 274 | |
| 275 | #define rsnd_priv_to_dev(priv) ((priv)->dev) |
| 276 | #define rsnd_lock(priv, flags) spin_lock_irqsave(&priv->lock, flags) |
| 277 | #define rsnd_unlock(priv, flags) spin_unlock_irqrestore(&priv->lock, flags) |
| 278 | |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 279 | /* |
| 280 | * R-Car SCU |
| 281 | */ |
| 282 | int rsnd_scu_probe(struct platform_device *pdev, |
| 283 | struct rcar_snd_info *info, |
| 284 | struct rsnd_priv *priv); |
| 285 | void rsnd_scu_remove(struct platform_device *pdev, |
| 286 | struct rsnd_priv *priv); |
| 287 | struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id); |
| 288 | #define rsnd_scu_nr(priv) ((priv)->scu_nr) |
| 289 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 290 | /* |
| 291 | * R-Car SSI |
| 292 | */ |
| 293 | int rsnd_ssi_probe(struct platform_device *pdev, |
| 294 | struct rcar_snd_info *info, |
| 295 | struct rsnd_priv *priv); |
| 296 | void rsnd_ssi_remove(struct platform_device *pdev, |
| 297 | struct rsnd_priv *priv); |
| 298 | struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id); |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 299 | struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv, |
| 300 | int dai_id, int is_play); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 301 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 302 | #endif |