4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/sh_dma.h>
23 #include <linux/workqueue.h>
24 #include <sound/soc.h>
25 #include <sound/pcm_params.h>
32 * The register address offsets SRU/SCU/SSIU on Gen1/Gen2 are very different.
33 * This driver uses pseudo register in order to hide it.
34 * see gen1/gen2 for detail
40 RSND_REG_SRC_BUSIF_MODE,
41 RSND_REG_SRC_ROUTE_MODE0,
48 RSND_REG_SCU_SYS_STATUS0,
49 RSND_REG_SCU_SYS_INT_EN0,
50 RSND_REG_CMD_ROUTE_SLCT,
76 RSND_REG_AUDIO_CLK_SEL0,
77 RSND_REG_AUDIO_CLK_SEL1,
121 #define RSND_REG_SRC_ROUTE_SEL RSND_REG_SHARE01
122 #define RSND_REG_SRC_TMG_SEL0 RSND_REG_SHARE02
123 #define RSND_REG_SRC_TMG_SEL1 RSND_REG_SHARE03
124 #define RSND_REG_SRC_TMG_SEL2 RSND_REG_SHARE04
125 #define RSND_REG_SRC_ROUTE_CTRL RSND_REG_SHARE05
126 #define RSND_REG_SRC_MNFSR RSND_REG_SHARE06
127 #define RSND_REG_AUDIO_CLK_SEL3 RSND_REG_SHARE07
128 #define RSND_REG_AUDIO_CLK_SEL4 RSND_REG_SHARE08
129 #define RSND_REG_AUDIO_CLK_SEL5 RSND_REG_SHARE09
132 #define RSND_REG_SRC_CTRL RSND_REG_SHARE01
133 #define RSND_REG_SSI_CTRL RSND_REG_SHARE02
134 #define RSND_REG_SSI_BUSIF_MODE RSND_REG_SHARE03
135 #define RSND_REG_SSI_BUSIF_ADINR RSND_REG_SHARE04
136 #define RSND_REG_SSI_INT_ENABLE RSND_REG_SHARE05
137 #define RSND_REG_SRC_BSDSR RSND_REG_SHARE06
138 #define RSND_REG_SRC_BSISR RSND_REG_SHARE07
139 #define RSND_REG_DIV_EN RSND_REG_SHARE08
140 #define RSND_REG_SRCIN_TIMSEL0 RSND_REG_SHARE09
141 #define RSND_REG_SRCIN_TIMSEL1 RSND_REG_SHARE10
142 #define RSND_REG_SRCIN_TIMSEL2 RSND_REG_SHARE11
143 #define RSND_REG_SRCIN_TIMSEL3 RSND_REG_SHARE12
144 #define RSND_REG_SRCIN_TIMSEL4 RSND_REG_SHARE13
145 #define RSND_REG_SRCOUT_TIMSEL0 RSND_REG_SHARE14
146 #define RSND_REG_SRCOUT_TIMSEL1 RSND_REG_SHARE15
147 #define RSND_REG_SRCOUT_TIMSEL2 RSND_REG_SHARE16
148 #define RSND_REG_SRCOUT_TIMSEL3 RSND_REG_SHARE17
149 #define RSND_REG_SRCOUT_TIMSEL4 RSND_REG_SHARE18
150 #define RSND_REG_AUDIO_CLK_SEL2 RSND_REG_SHARE19
151 #define RSND_REG_CMD_CTRL RSND_REG_SHARE20
152 #define RSND_REG_CMDOUT_TIMSEL RSND_REG_SHARE21
153 #define RSND_REG_SSI_BUSIF_DALIGN RSND_REG_SHARE22
154 #define RSND_REG_DVC_VRCTR RSND_REG_SHARE23
155 #define RSND_REG_DVC_VRPDR RSND_REG_SHARE24
156 #define RSND_REG_DVC_VRDBR RSND_REG_SHARE25
157 #define RSND_REG_SCU_SYS_STATUS1 RSND_REG_SHARE26
158 #define RSND_REG_SCU_SYS_INT_EN1 RSND_REG_SHARE27
159 #define RSND_REG_SRC_INT_ENABLE0 RSND_REG_SHARE28
160 #define RSND_REG_SRC_BUSIF_DALIGN RSND_REG_SHARE29
166 struct rsnd_dai_stream;
169 * R-Car basic functions
171 #define rsnd_mod_read(m, r) \
172 rsnd_read(rsnd_mod_to_priv(m), m, RSND_REG_##r)
173 #define rsnd_mod_write(m, r, d) \
174 rsnd_write(rsnd_mod_to_priv(m), m, RSND_REG_##r, d)
175 #define rsnd_mod_force_write(m, r, d) \
176 rsnd_force_write(rsnd_mod_to_priv(m), m, RSND_REG_##r, d)
177 #define rsnd_mod_bset(m, r, s, d) \
178 rsnd_bset(rsnd_mod_to_priv(m), m, RSND_REG_##r, s, d)
180 u32 rsnd_read(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg);
181 void rsnd_write(struct rsnd_priv *priv, struct rsnd_mod *mod,
182 enum rsnd_reg reg, u32 data);
183 void rsnd_force_write(struct rsnd_priv *priv, struct rsnd_mod *mod,
184 enum rsnd_reg reg, u32 data);
185 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod, enum rsnd_reg reg,
187 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io);
188 u32 rsnd_get_adinr_chan(struct rsnd_mod *mod, struct rsnd_dai_stream *io);
189 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io);
190 void rsnd_path_parse(struct rsnd_priv *priv,
191 struct rsnd_dai_stream *io);
199 struct dma_chan *chan;
208 struct rsnd_dma_ops *ops;
212 struct rsnd_dmaen en;
213 struct rsnd_dmapp pp;
216 #define rsnd_dma_to_dmaen(dma) (&(dma)->dma.en)
217 #define rsnd_dma_to_dmapp(dma) (&(dma)->dma.pp)
218 #define rsnd_dma_to_mod(_dma) container_of((_dma), struct rsnd_mod, dma)
220 void rsnd_dma_start(struct rsnd_dai_stream *io, struct rsnd_dma *dma);
221 void rsnd_dma_stop(struct rsnd_dai_stream *io, struct rsnd_dma *dma);
222 int rsnd_dma_init(struct rsnd_dai_stream *io, struct rsnd_dma *dma, int id);
223 void rsnd_dma_quit(struct rsnd_dai_stream *io, struct rsnd_dma *dma);
224 int rsnd_dma_probe(struct platform_device *pdev,
225 const struct rsnd_of_data *of_data,
226 struct rsnd_priv *priv);
227 struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node,
228 struct rsnd_mod *mod, char *name);
238 RSND_MOD_SSIP, /* SSI parent */
243 struct rsnd_mod_ops {
245 struct dma_chan* (*dma_req)(struct rsnd_dai_stream *io,
246 struct rsnd_mod *mod);
247 int (*probe)(struct rsnd_mod *mod,
248 struct rsnd_dai_stream *io,
249 struct rsnd_priv *priv);
250 int (*remove)(struct rsnd_mod *mod,
251 struct rsnd_dai_stream *io,
252 struct rsnd_priv *priv);
253 int (*init)(struct rsnd_mod *mod,
254 struct rsnd_dai_stream *io,
255 struct rsnd_priv *priv);
256 int (*quit)(struct rsnd_mod *mod,
257 struct rsnd_dai_stream *io,
258 struct rsnd_priv *priv);
259 int (*start)(struct rsnd_mod *mod,
260 struct rsnd_dai_stream *io,
261 struct rsnd_priv *priv);
262 int (*stop)(struct rsnd_mod *mod,
263 struct rsnd_dai_stream *io,
264 struct rsnd_priv *priv);
265 int (*pcm_new)(struct rsnd_mod *mod,
266 struct rsnd_dai_stream *io,
267 struct snd_soc_pcm_runtime *rtd);
268 int (*hw_params)(struct rsnd_mod *mod,
269 struct rsnd_dai_stream *io,
270 struct snd_pcm_substream *substream,
271 struct snd_pcm_hw_params *hw_params);
272 int (*fallback)(struct rsnd_mod *mod,
273 struct rsnd_dai_stream *io,
274 struct rsnd_priv *priv);
277 struct rsnd_dai_stream;
280 enum rsnd_mod_type type;
281 struct rsnd_mod_ops *ops;
283 struct rsnd_priv *priv;
292 * A 0: probe 1: remove
296 * H is always called (see __rsnd_mod_call)
301 #define __rsnd_mod_shift_probe 0
302 #define __rsnd_mod_shift_remove 0
303 #define __rsnd_mod_shift_init 4
304 #define __rsnd_mod_shift_quit 4
305 #define __rsnd_mod_shift_start 8
306 #define __rsnd_mod_shift_stop 8
307 #define __rsnd_mod_shift_pcm_new 28 /* always called */
308 #define __rsnd_mod_shift_fallback 28 /* always called */
309 #define __rsnd_mod_shift_hw_params 28 /* always called */
311 #define __rsnd_mod_add_probe 1
312 #define __rsnd_mod_add_remove -1
313 #define __rsnd_mod_add_init 1
314 #define __rsnd_mod_add_quit -1
315 #define __rsnd_mod_add_start 1
316 #define __rsnd_mod_add_stop -1
317 #define __rsnd_mod_add_pcm_new 0
318 #define __rsnd_mod_add_fallback 0
319 #define __rsnd_mod_add_hw_params 0
321 #define __rsnd_mod_call_probe 0
322 #define __rsnd_mod_call_remove 1
323 #define __rsnd_mod_call_init 0
324 #define __rsnd_mod_call_quit 1
325 #define __rsnd_mod_call_start 0
326 #define __rsnd_mod_call_stop 1
327 #define __rsnd_mod_call_pcm_new 0
328 #define __rsnd_mod_call_fallback 0
329 #define __rsnd_mod_call_hw_params 0
331 #define rsnd_mod_to_priv(mod) ((mod)->priv)
332 #define rsnd_mod_to_dma(mod) (&(mod)->dma)
333 #define rsnd_mod_id(mod) ((mod) ? (mod)->id : -1)
334 #define rsnd_mod_power_on(mod) clk_enable((mod)->clk)
335 #define rsnd_mod_power_off(mod) clk_disable((mod)->clk)
336 #define rsnd_mod_get(ip) (&(ip)->mod)
338 int rsnd_mod_init(struct rsnd_priv *priv,
339 struct rsnd_mod *mod,
340 struct rsnd_mod_ops *ops,
342 enum rsnd_mod_type type,
344 void rsnd_mod_quit(struct rsnd_mod *mod);
345 char *rsnd_mod_name(struct rsnd_mod *mod);
346 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
347 struct rsnd_mod *mod);
348 void rsnd_mod_interrupt(struct rsnd_mod *mod,
349 void (*callback)(struct rsnd_mod *mod,
350 struct rsnd_dai_stream *io));
355 #define RSND_DAI_NAME_SIZE 16
356 struct rsnd_dai_stream {
357 char name[RSND_DAI_NAME_SIZE];
358 struct snd_pcm_substream *substream;
359 struct rsnd_mod *mod[RSND_MOD_MAX];
360 struct rsnd_dai_path_info *info; /* rcar_snd.h */
361 struct rsnd_dai *rdai;
365 int next_period_byte;
367 #define rsnd_io_to_mod(io, i) ((i) < RSND_MOD_MAX ? (io)->mod[(i)] : NULL)
368 #define rsnd_io_to_mod_ssi(io) rsnd_io_to_mod((io), RSND_MOD_SSI)
369 #define rsnd_io_to_mod_ssip(io) rsnd_io_to_mod((io), RSND_MOD_SSIP)
370 #define rsnd_io_to_mod_src(io) rsnd_io_to_mod((io), RSND_MOD_SRC)
371 #define rsnd_io_to_mod_ctu(io) rsnd_io_to_mod((io), RSND_MOD_CTU)
372 #define rsnd_io_to_mod_mix(io) rsnd_io_to_mod((io), RSND_MOD_MIX)
373 #define rsnd_io_to_mod_dvc(io) rsnd_io_to_mod((io), RSND_MOD_DVC)
374 #define rsnd_io_to_rdai(io) ((io)->rdai)
375 #define rsnd_io_to_priv(io) (rsnd_rdai_to_priv(rsnd_io_to_rdai(io)))
376 #define rsnd_io_is_play(io) (&rsnd_io_to_rdai(io)->playback == io)
377 #define rsnd_io_to_runtime(io) ((io)->substream ? \
378 (io)->substream->runtime : NULL)
379 int rsnd_io_is_working(struct rsnd_dai_stream *io);
382 char name[RSND_DAI_NAME_SIZE];
383 struct rsnd_dai_stream playback;
384 struct rsnd_dai_stream capture;
385 struct rsnd_priv *priv;
387 unsigned int clk_master:1;
388 unsigned int bit_clk_inv:1;
389 unsigned int frm_clk_inv:1;
390 unsigned int sys_delay:1;
391 unsigned int data_alignment:1;
394 #define rsnd_rdai_nr(priv) ((priv)->rdai_nr)
395 #define rsnd_rdai_is_clk_master(rdai) ((rdai)->clk_master)
396 #define rsnd_rdai_to_priv(rdai) ((rdai)->priv)
397 #define for_each_rsnd_dai(rdai, priv, i) \
399 (i < rsnd_rdai_nr(priv)) && \
400 ((rdai) = rsnd_rdai_get(priv, i)); \
403 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id);
405 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int cnt);
406 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io);
407 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional);
412 int rsnd_gen_probe(struct platform_device *pdev,
413 const struct rsnd_of_data *of_data,
414 struct rsnd_priv *priv);
415 void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv,
416 struct rsnd_mod *mod,
418 phys_addr_t rsnd_gen_get_phy_addr(struct rsnd_priv *priv, int reg_id);
420 #define rsnd_is_gen1(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN1)
421 #define rsnd_is_gen2(s) (((s)->info->flags & RSND_GEN_MASK) == RSND_GEN2)
426 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod);
427 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate);
428 int rsnd_adg_probe(struct platform_device *pdev,
429 const struct rsnd_of_data *of_data,
430 struct rsnd_priv *priv);
431 int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv,
432 struct rsnd_mod *mod,
433 unsigned int src_rate,
434 unsigned int dst_rate);
435 int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *mod,
436 struct rsnd_dai_stream *io,
437 unsigned int src_rate,
438 unsigned int dst_rate);
439 int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *mod,
440 struct rsnd_dai_stream *io);
441 int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *mod,
442 struct rsnd_dai_stream *io);
447 struct rsnd_of_data {
453 struct platform_device *pdev;
454 struct rcar_snd_info *info;
458 * below value will be filled on rsnd_gen_probe()
463 * below value will be filled on rsnd_adg_probe()
468 * below value will be filled on rsnd_dma_probe()
473 * below value will be filled on rsnd_ssi_probe()
479 * below value will be filled on rsnd_src_probe()
485 * below value will be filled on rsnd_ctu_probe()
491 * below value will be filled on rsnd_mix_probe()
497 * below value will be filled on rsnd_dvc_probe()
503 * below value will be filled on rsnd_dai_probe()
505 struct snd_soc_dai_driver *daidrv;
506 struct rsnd_dai *rdai;
510 #define rsnd_priv_to_pdev(priv) ((priv)->pdev)
511 #define rsnd_priv_to_dev(priv) (&(rsnd_priv_to_pdev(priv)->dev))
512 #define rsnd_priv_to_info(priv) ((priv)->info)
517 struct rsnd_kctrl_cfg {
521 const char * const *texts;
522 void (*update)(struct rsnd_dai_stream *io, struct rsnd_mod *mod);
523 struct rsnd_dai_stream *io;
524 struct snd_card *card;
525 struct snd_kcontrol *kctrl;
528 #define RSND_DVC_CHANNELS 2
529 struct rsnd_kctrl_cfg_m {
530 struct rsnd_kctrl_cfg cfg;
531 u32 val[RSND_DVC_CHANNELS];
534 struct rsnd_kctrl_cfg_s {
535 struct rsnd_kctrl_cfg cfg;
539 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg);
540 #define rsnd_kctrl_remove(_cfg) _rsnd_kctrl_remove(&((_cfg).cfg))
542 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
543 struct rsnd_dai_stream *io,
544 struct snd_soc_pcm_runtime *rtd,
545 const unsigned char *name,
546 void (*update)(struct rsnd_dai_stream *io,
547 struct rsnd_mod *mod),
548 struct rsnd_kctrl_cfg_m *_cfg,
550 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
551 struct rsnd_dai_stream *io,
552 struct snd_soc_pcm_runtime *rtd,
553 const unsigned char *name,
554 void (*update)(struct rsnd_dai_stream *io,
555 struct rsnd_mod *mod),
556 struct rsnd_kctrl_cfg_s *_cfg,
558 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
559 struct rsnd_dai_stream *io,
560 struct snd_soc_pcm_runtime *rtd,
561 const unsigned char *name,
562 struct rsnd_kctrl_cfg_s *_cfg,
563 void (*update)(struct rsnd_dai_stream *io,
564 struct rsnd_mod *mod),
565 const char * const *texts,
571 int rsnd_ssi_probe(struct platform_device *pdev,
572 const struct rsnd_of_data *of_data,
573 struct rsnd_priv *priv);
574 void rsnd_ssi_remove(struct platform_device *pdev,
575 struct rsnd_priv *priv);
576 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id);
577 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod);
578 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io);
580 #define rsnd_ssi_is_pin_sharing(io) \
581 __rsnd_ssi_is_pin_sharing(rsnd_io_to_mod_ssi(io))
582 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod);
587 int rsnd_src_probe(struct platform_device *pdev,
588 const struct rsnd_of_data *of_data,
589 struct rsnd_priv *priv);
590 void rsnd_src_remove(struct platform_device *pdev,
591 struct rsnd_priv *priv);
592 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id);
593 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
594 struct rsnd_dai_stream *io,
595 struct snd_pcm_runtime *runtime);
596 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
597 struct rsnd_dai_stream *io,
599 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
600 struct rsnd_dai_stream *io);
601 int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod);
602 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod);
607 int rsnd_ctu_probe(struct platform_device *pdev,
608 const struct rsnd_of_data *of_data,
609 struct rsnd_priv *priv);
611 void rsnd_ctu_remove(struct platform_device *pdev,
612 struct rsnd_priv *priv);
613 struct rsnd_mod *rsnd_ctu_mod_get(struct rsnd_priv *priv, int id);
618 int rsnd_mix_probe(struct platform_device *pdev,
619 const struct rsnd_of_data *of_data,
620 struct rsnd_priv *priv);
622 void rsnd_mix_remove(struct platform_device *pdev,
623 struct rsnd_priv *priv);
624 struct rsnd_mod *rsnd_mix_mod_get(struct rsnd_priv *priv, int id);
629 int rsnd_dvc_probe(struct platform_device *pdev,
630 const struct rsnd_of_data *of_data,
631 struct rsnd_priv *priv);
632 void rsnd_dvc_remove(struct platform_device *pdev,
633 struct rsnd_priv *priv);
634 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id);
637 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type);
638 #define rsnd_mod_confirm_ssi(mssi) rsnd_mod_make_sure(mssi, RSND_MOD_SSI)
639 #define rsnd_mod_confirm_src(msrc) rsnd_mod_make_sure(msrc, RSND_MOD_SRC)
640 #define rsnd_mod_confirm_dvc(mdvc) rsnd_mod_make_sure(mdvc, RSND_MOD_DVC)
642 #define rsnd_mod_confirm_ssi(mssi)
643 #define rsnd_mod_confirm_src(msrc)
644 #define rsnd_mod_confirm_dvc(mdvc)