blob: c397dc8edee533c62c834d8007c714014248809e [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 Morimoto52ea2a72013-12-19 19:28:19 -080042 RSND_REG_INT_ENABLE, /* for Gen2 */
Kuninori Morimoto0290d2a2014-01-23 18:37:39 -080043 RSND_REG_SRC_BUSIF_MODE,
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,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -080051 RSND_REG_SRC_MNFSR, /* for Gen1 */
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 {
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -070099 struct sh_dmae_slave slave;
100 struct work_struct work;
101 struct dma_chan *chan;
102 enum dma_data_direction dir;
103 int (*inquiry)(struct rsnd_dma *dma, dma_addr_t *buf, int *len);
104 int (*complete)(struct rsnd_dma *dma);
105
106 int submit_loop;
107};
108
109void rsnd_dma_start(struct rsnd_dma *dma);
110void rsnd_dma_stop(struct rsnd_dma *dma);
111int rsnd_dma_available(struct rsnd_dma *dma);
112int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
113 int is_play, int id,
114 int (*inquiry)(struct rsnd_dma *dma, dma_addr_t *buf, int *len),
115 int (*complete)(struct rsnd_dma *dma));
116void rsnd_dma_quit(struct rsnd_priv *priv,
117 struct rsnd_dma *dma);
118
119
120/*
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700121 * R-Car sound mod
122 */
123
124struct rsnd_mod_ops {
125 char *name;
126 int (*init)(struct rsnd_mod *mod,
127 struct rsnd_dai *rdai,
128 struct rsnd_dai_stream *io);
129 int (*quit)(struct rsnd_mod *mod,
130 struct rsnd_dai *rdai,
131 struct rsnd_dai_stream *io);
132 int (*start)(struct rsnd_mod *mod,
133 struct rsnd_dai *rdai,
134 struct rsnd_dai_stream *io);
135 int (*stop)(struct rsnd_mod *mod,
136 struct rsnd_dai *rdai,
137 struct rsnd_dai_stream *io);
138};
139
140struct rsnd_mod {
141 int id;
142 struct rsnd_priv *priv;
143 struct rsnd_mod_ops *ops;
144 struct list_head list; /* connect to rsnd_dai playback/capture */
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700145 struct rsnd_dma dma;
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700146};
147
148#define rsnd_mod_to_priv(mod) ((mod)->priv)
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700149#define rsnd_mod_to_dma(mod) (&(mod)->dma)
150#define rsnd_dma_to_mod(_dma) container_of((_dma), struct rsnd_mod, dma)
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700151#define rsnd_mod_id(mod) ((mod)->id)
152#define for_each_rsnd_mod(pos, n, io) \
153 list_for_each_entry_safe(pos, n, &(io)->head, list)
Kuninori Morimoto92d95872014-01-23 18:38:08 -0800154#define __rsnd_mod_call(mod, func, rdai, io) \
155({ \
156 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
157 struct device *dev = rsnd_priv_to_dev(priv); \
158 dev_dbg(dev, "%s-%d-%s\n", \
159 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
160 (mod)->ops->func(mod, rdai, io); \
161})
162
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700163#define rsnd_mod_call(mod, func, rdai, io) \
164 (!(mod) ? -ENODEV : \
165 !((mod)->ops->func) ? 0 : \
Kuninori Morimoto92d95872014-01-23 18:38:08 -0800166 __rsnd_mod_call(mod, func, rdai, io))
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700167
168void rsnd_mod_init(struct rsnd_priv *priv,
169 struct rsnd_mod *mod,
170 struct rsnd_mod_ops *ops,
171 int id);
172char *rsnd_mod_name(struct rsnd_mod *mod);
173
174/*
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700175 * R-Car sound DAI
176 */
177#define RSND_DAI_NAME_SIZE 16
178struct rsnd_dai_stream {
179 struct list_head head; /* head of rsnd_mod list */
180 struct snd_pcm_substream *substream;
181 int byte_pos;
182 int period_pos;
183 int byte_per_period;
184 int next_period_byte;
185};
186
187struct rsnd_dai {
188 char name[RSND_DAI_NAME_SIZE];
189 struct rsnd_dai_platform_info *info; /* rcar_snd.h */
190 struct rsnd_dai_stream playback;
191 struct rsnd_dai_stream capture;
192
Dan Carpentera3737732013-11-08 12:46:53 +0300193 unsigned int clk_master:1;
194 unsigned int bit_clk_inv:1;
195 unsigned int frm_clk_inv:1;
196 unsigned int sys_delay:1;
197 unsigned int data_alignment:1;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700198};
199
200#define rsnd_dai_nr(priv) ((priv)->dai_nr)
201#define for_each_rsnd_dai(rdai, priv, i) \
202 for (i = 0, (rdai) = rsnd_dai_get(priv, i); \
203 i < rsnd_dai_nr(priv); \
204 i++, (rdai) = rsnd_dai_get(priv, i))
205
206struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id);
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700207int rsnd_dai_disconnect(struct rsnd_mod *mod);
208int rsnd_dai_connect(struct rsnd_dai *rdai, struct rsnd_mod *mod,
209 struct rsnd_dai_stream *io);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700210int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io);
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700211int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700212#define rsnd_dai_get_platform_info(rdai) ((rdai)->info)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700213#define rsnd_io_to_runtime(io) ((io)->substream->runtime)
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700214
215void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int cnt);
216int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional);
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800217#define rsnd_dai_is_clk_master(rdai) ((rdai)->clk_master)
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700218
219/*
Kuninori Morimoto33377442013-07-21 21:36:21 -0700220 * R-Car Gen1/Gen2
221 */
222int rsnd_gen_probe(struct platform_device *pdev,
223 struct rcar_snd_info *info,
224 struct rsnd_priv *priv);
225void rsnd_gen_remove(struct platform_device *pdev,
226 struct rsnd_priv *priv);
227int rsnd_gen_path_init(struct rsnd_priv *priv,
228 struct rsnd_dai *rdai,
229 struct rsnd_dai_stream *io);
230int rsnd_gen_path_exit(struct rsnd_priv *priv,
231 struct rsnd_dai *rdai,
232 struct rsnd_dai_stream *io);
233void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv,
234 struct rsnd_mod *mod,
235 enum rsnd_reg reg);
Kuninori Morimotoc5d5a582013-10-11 00:07:01 -0700236#define rsnd_is_gen1(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN1)
237#define rsnd_is_gen2(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN2)
Kuninori Morimoto33377442013-07-21 21:36:21 -0700238
239/*
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700240 * R-Car ADG
241 */
242int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod);
243int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate);
244int rsnd_adg_probe(struct platform_device *pdev,
245 struct rcar_snd_info *info,
246 struct rsnd_priv *priv);
247void rsnd_adg_remove(struct platform_device *pdev,
248 struct rsnd_priv *priv);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800249int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv,
250 struct rsnd_mod *mod,
251 unsigned int src_rate,
252 unsigned int dst_rate);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700253
254/*
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700255 * R-Car sound priv
256 */
257struct rsnd_priv {
258
259 struct device *dev;
260 struct rcar_snd_info *info;
261 spinlock_t lock;
262
263 /*
Kuninori Morimoto33377442013-07-21 21:36:21 -0700264 * below value will be filled on rsnd_gen_probe()
265 */
266 void *gen;
267
268 /*
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700269 * below value will be filled on rsnd_scu_probe()
270 */
271 void *scu;
272 int scu_nr;
273
274 /*
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700275 * below value will be filled on rsnd_adg_probe()
276 */
277 void *adg;
278
279 /*
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700280 * below value will be filled on rsnd_ssi_probe()
281 */
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800282 void *ssi;
283 int ssi_nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700284
285 /*
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700286 * below value will be filled on rsnd_dai_probe()
287 */
288 struct snd_soc_dai_driver *daidrv;
289 struct rsnd_dai *rdai;
290 int dai_nr;
291};
292
293#define rsnd_priv_to_dev(priv) ((priv)->dev)
294#define rsnd_lock(priv, flags) spin_lock_irqsave(&priv->lock, flags)
295#define rsnd_unlock(priv, flags) spin_unlock_irqrestore(&priv->lock, flags)
296
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700297/*
298 * R-Car SCU
299 */
300int rsnd_scu_probe(struct platform_device *pdev,
301 struct rcar_snd_info *info,
302 struct rsnd_priv *priv);
303void rsnd_scu_remove(struct platform_device *pdev,
304 struct rsnd_priv *priv);
305struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800306unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
307 struct rsnd_mod *ssi_mod,
308 struct snd_pcm_runtime *runtime);
309
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700310#define rsnd_scu_nr(priv) ((priv)->scu_nr)
311
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700312/*
313 * R-Car SSI
314 */
315int rsnd_ssi_probe(struct platform_device *pdev,
316 struct rcar_snd_info *info,
317 struct rsnd_priv *priv);
318void rsnd_ssi_remove(struct platform_device *pdev,
319 struct rsnd_priv *priv);
320struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id);
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700321struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv,
322 int dai_id, int is_play);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800323int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod);
Kuninori Morimoto32f27eb2014-01-23 18:41:17 -0800324int rsnd_ssi_is_play(struct rsnd_mod *mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700325
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700326#endif