blob: 4ca66cd899c87415e263219c400fea5ce4e51df0 [file] [log] [blame]
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001/*
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 Morimoto0a4d94c2013-07-28 18:58:50 -070016#include <linux/dma-mapping.h>
Kuninori Morimoto1536a962013-07-21 21:35:52 -070017#include <linux/io.h>
18#include <linux/list.h>
19#include <linux/module.h>
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -070020#include <linux/sh_dma.h>
21#include <linux/workqueue.h>
Kuninori Morimoto1536a962013-07-21 21:35:52 -070022#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 Morimoto33377442013-07-21 21:36:21 -070033enum rsnd_reg {
Kuninori Morimoto507d4662013-11-28 18:43:45 -080034 /* SRU/SCU/SSIU */
Kuninori Morimoto2582718c2013-12-19 19:27:37 -080035 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 */
Kuninori Morimoto07539c12013-07-21 21:36:35 -070040 RSND_REG_SSI_MODE0,
41 RSND_REG_SSI_MODE1,
Kuninori Morimoto374a52812013-07-28 18:59:12 -070042 RSND_REG_BUSIF_MODE,
Kuninori Morimoto52ea2a72013-12-19 19:28:19 -080043 RSND_REG_INT_ENABLE, /* for Gen2 */
Kuninori Morimotoef749402013-12-19 19:28:51 -080044 RSND_REG_SRC_ROUTE_MODE0,
45 RSND_REG_SRC_SWRSR,
46 RSND_REG_SRC_SRCIR,
Kuninori Morimoto690ef812013-12-19 19:27:03 -080047 RSND_REG_SRC_ADINR,
Kuninori Morimotoef749402013-12-19 19:28:51 -080048 RSND_REG_SRC_IFSCR,
49 RSND_REG_SRC_IFSVR,
50 RSND_REG_SRC_SRCCR,
51 RSND_REG_SRC_MNFSR,
Kuninori Morimoto07539c12013-07-21 21:36:35 -070052
Kuninori Morimotodfc94032013-07-21 21:36:46 -070053 /* ADG */
54 RSND_REG_BRRA,
55 RSND_REG_BRRB,
56 RSND_REG_SSICKR,
57 RSND_REG_AUDIO_CLK_SEL0,
58 RSND_REG_AUDIO_CLK_SEL1,
59 RSND_REG_AUDIO_CLK_SEL2,
Kuninori Morimotoef749402013-12-19 19:28:51 -080060 RSND_REG_AUDIO_CLK_SEL3, /* for Gen1 */
61 RSND_REG_AUDIO_CLK_SEL4, /* for Gen1 */
62 RSND_REG_AUDIO_CLK_SEL5, /* for Gen1 */
Kuninori Morimotodfc94032013-07-21 21:36:46 -070063
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070064 /* SSI */
65 RSND_REG_SSICR,
66 RSND_REG_SSISR,
67 RSND_REG_SSITDR,
68 RSND_REG_SSIRDR,
69 RSND_REG_SSIWSR,
70
Kuninori Morimoto33377442013-07-21 21:36:21 -070071 RSND_REG_MAX,
72};
73
Kuninori Morimoto1536a962013-07-21 21:35:52 -070074struct rsnd_priv;
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -070075struct rsnd_mod;
Kuninori Morimoto1536a962013-07-21 21:35:52 -070076struct rsnd_dai;
77struct rsnd_dai_stream;
78
79/*
Kuninori Morimoto33377442013-07-21 21:36:21 -070080 * R-Car basic functions
81 */
82#define rsnd_mod_read(m, r) \
83 rsnd_read(rsnd_mod_to_priv(m), m, RSND_REG_##r)
84#define rsnd_mod_write(m, r, d) \
85 rsnd_write(rsnd_mod_to_priv(m), m, RSND_REG_##r, d)
86#define rsnd_mod_bset(m, r, s, d) \
87 rsnd_bset(rsnd_mod_to_priv(m), m, RSND_REG_##r, s, d)
88
Kuninori Morimoto33377442013-07-21 21:36:21 -070089u32 rsnd_read(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg);
90void rsnd_write(struct rsnd_priv *priv, struct rsnd_mod *mod,
91 enum rsnd_reg reg, u32 data);
92void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg,
93 u32 mask, u32 data);
94
95/*
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -070096 * R-Car DMA
97 */
98struct rsnd_dma {
99 struct rsnd_priv *priv;
100 struct sh_dmae_slave slave;
101 struct work_struct work;
102 struct dma_chan *chan;
103 enum dma_data_direction dir;
104 int (*inquiry)(struct rsnd_dma *dma, dma_addr_t *buf, int *len);
105 int (*complete)(struct rsnd_dma *dma);
106
107 int submit_loop;
108};
109
110void rsnd_dma_start(struct rsnd_dma *dma);
111void rsnd_dma_stop(struct rsnd_dma *dma);
112int rsnd_dma_available(struct rsnd_dma *dma);
113int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
114 int is_play, int id,
115 int (*inquiry)(struct rsnd_dma *dma, dma_addr_t *buf, int *len),
116 int (*complete)(struct rsnd_dma *dma));
117void rsnd_dma_quit(struct rsnd_priv *priv,
118 struct rsnd_dma *dma);
119
120
121/*
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700122 * R-Car sound mod
123 */
124
125struct rsnd_mod_ops {
126 char *name;
127 int (*init)(struct rsnd_mod *mod,
128 struct rsnd_dai *rdai,
129 struct rsnd_dai_stream *io);
130 int (*quit)(struct rsnd_mod *mod,
131 struct rsnd_dai *rdai,
132 struct rsnd_dai_stream *io);
133 int (*start)(struct rsnd_mod *mod,
134 struct rsnd_dai *rdai,
135 struct rsnd_dai_stream *io);
136 int (*stop)(struct rsnd_mod *mod,
137 struct rsnd_dai *rdai,
138 struct rsnd_dai_stream *io);
139};
140
141struct rsnd_mod {
142 int id;
143 struct rsnd_priv *priv;
144 struct rsnd_mod_ops *ops;
145 struct list_head list; /* connect to rsnd_dai playback/capture */
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700146 struct rsnd_dma dma;
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700147};
148
149#define rsnd_mod_to_priv(mod) ((mod)->priv)
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700150#define rsnd_mod_to_dma(mod) (&(mod)->dma)
151#define rsnd_dma_to_mod(_dma) container_of((_dma), struct rsnd_mod, dma)
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700152#define rsnd_mod_id(mod) ((mod)->id)
153#define for_each_rsnd_mod(pos, n, io) \
154 list_for_each_entry_safe(pos, n, &(io)->head, list)
155#define rsnd_mod_call(mod, func, rdai, io) \
156 (!(mod) ? -ENODEV : \
157 !((mod)->ops->func) ? 0 : \
158 (mod)->ops->func(mod, rdai, io))
159
160void rsnd_mod_init(struct rsnd_priv *priv,
161 struct rsnd_mod *mod,
162 struct rsnd_mod_ops *ops,
163 int id);
164char *rsnd_mod_name(struct rsnd_mod *mod);
165
166/*
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700167 * R-Car sound DAI
168 */
169#define RSND_DAI_NAME_SIZE 16
170struct rsnd_dai_stream {
171 struct list_head head; /* head of rsnd_mod list */
172 struct snd_pcm_substream *substream;
173 int byte_pos;
174 int period_pos;
175 int byte_per_period;
176 int next_period_byte;
177};
178
179struct rsnd_dai {
180 char name[RSND_DAI_NAME_SIZE];
181 struct rsnd_dai_platform_info *info; /* rcar_snd.h */
182 struct rsnd_dai_stream playback;
183 struct rsnd_dai_stream capture;
184
Dan Carpentera3737732013-11-08 12:46:53 +0300185 unsigned int clk_master:1;
186 unsigned int bit_clk_inv:1;
187 unsigned int frm_clk_inv:1;
188 unsigned int sys_delay:1;
189 unsigned int data_alignment:1;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700190};
191
192#define rsnd_dai_nr(priv) ((priv)->dai_nr)
193#define for_each_rsnd_dai(rdai, priv, i) \
194 for (i = 0, (rdai) = rsnd_dai_get(priv, i); \
195 i < rsnd_dai_nr(priv); \
196 i++, (rdai) = rsnd_dai_get(priv, i))
197
198struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id);
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700199int rsnd_dai_disconnect(struct rsnd_mod *mod);
200int rsnd_dai_connect(struct rsnd_dai *rdai, struct rsnd_mod *mod,
201 struct rsnd_dai_stream *io);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700202int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io);
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700203int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700204#define rsnd_dai_get_platform_info(rdai) ((rdai)->info)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700205#define rsnd_io_to_runtime(io) ((io)->substream->runtime)
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700206
207void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int cnt);
208int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional);
209
210/*
Kuninori Morimoto33377442013-07-21 21:36:21 -0700211 * R-Car Gen1/Gen2
212 */
213int rsnd_gen_probe(struct platform_device *pdev,
214 struct rcar_snd_info *info,
215 struct rsnd_priv *priv);
216void rsnd_gen_remove(struct platform_device *pdev,
217 struct rsnd_priv *priv);
218int rsnd_gen_path_init(struct rsnd_priv *priv,
219 struct rsnd_dai *rdai,
220 struct rsnd_dai_stream *io);
221int rsnd_gen_path_exit(struct rsnd_priv *priv,
222 struct rsnd_dai *rdai,
223 struct rsnd_dai_stream *io);
224void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv,
225 struct rsnd_mod *mod,
226 enum rsnd_reg reg);
Kuninori Morimotoc5d5a582013-10-11 00:07:01 -0700227#define rsnd_is_gen1(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN1)
228#define rsnd_is_gen2(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN2)
Kuninori Morimoto33377442013-07-21 21:36:21 -0700229
230/*
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700231 * R-Car ADG
232 */
233int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod);
234int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate);
235int rsnd_adg_probe(struct platform_device *pdev,
236 struct rcar_snd_info *info,
237 struct rsnd_priv *priv);
238void rsnd_adg_remove(struct platform_device *pdev,
239 struct rsnd_priv *priv);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800240int rsnd_adg_set_convert_clk(struct rsnd_priv *priv,
241 struct rsnd_mod *mod,
242 unsigned int src_rate,
243 unsigned int dst_rate);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700244
245/*
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700246 * R-Car sound priv
247 */
248struct rsnd_priv {
249
250 struct device *dev;
251 struct rcar_snd_info *info;
252 spinlock_t lock;
253
254 /*
Kuninori Morimoto33377442013-07-21 21:36:21 -0700255 * below value will be filled on rsnd_gen_probe()
256 */
257 void *gen;
258
259 /*
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700260 * below value will be filled on rsnd_scu_probe()
261 */
262 void *scu;
263 int scu_nr;
264
265 /*
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700266 * below value will be filled on rsnd_adg_probe()
267 */
268 void *adg;
269
270 /*
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700271 * below value will be filled on rsnd_ssi_probe()
272 */
273 void *ssiu;
274
275 /*
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700276 * below value will be filled on rsnd_dai_probe()
277 */
278 struct snd_soc_dai_driver *daidrv;
279 struct rsnd_dai *rdai;
280 int dai_nr;
281};
282
283#define rsnd_priv_to_dev(priv) ((priv)->dev)
284#define rsnd_lock(priv, flags) spin_lock_irqsave(&priv->lock, flags)
285#define rsnd_unlock(priv, flags) spin_unlock_irqrestore(&priv->lock, flags)
286
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700287/*
288 * R-Car SCU
289 */
290int rsnd_scu_probe(struct platform_device *pdev,
291 struct rcar_snd_info *info,
292 struct rsnd_priv *priv);
293void rsnd_scu_remove(struct platform_device *pdev,
294 struct rsnd_priv *priv);
295struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id);
Kuninori Morimotocdcfcac2013-10-17 22:50:59 -0700296bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800297unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
298 struct rsnd_mod *ssi_mod,
299 struct snd_pcm_runtime *runtime);
300
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700301#define rsnd_scu_nr(priv) ((priv)->scu_nr)
302
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700303/*
304 * R-Car SSI
305 */
306int rsnd_ssi_probe(struct platform_device *pdev,
307 struct rcar_snd_info *info,
308 struct rsnd_priv *priv);
309void rsnd_ssi_remove(struct platform_device *pdev,
310 struct rsnd_priv *priv);
311struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id);
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700312struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv,
313 int dai_id, int is_play);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700314
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700315#endif