blob: e52a95d44a6b10ebbd43da32fbf2666aad0a974b [file] [log] [blame]
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001/*
2 * Fifo-attached Serial Interface (FSI) support for SH7724
3 *
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on ssi.c
8 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090015#include <linux/delay.h>
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080016#include <linux/dma-mapping.h>
Kuninori Morimoto785d1c42009-11-30 20:24:48 +090017#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090018#include <linux/io.h>
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080019#include <linux/scatterlist.h>
20#include <linux/sh_dma.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040022#include <linux/module.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090023#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090024#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090025
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090026/* PortA/PortB register */
27#define REG_DO_FMT 0x0000
28#define REG_DOFF_CTL 0x0004
29#define REG_DOFF_ST 0x0008
30#define REG_DI_FMT 0x000C
31#define REG_DIFF_CTL 0x0010
32#define REG_DIFF_ST 0x0014
33#define REG_CKG1 0x0018
34#define REG_CKG2 0x001C
35#define REG_DIDT 0x0020
36#define REG_DODT 0x0024
37#define REG_MUTE_ST 0x0028
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080038#define REG_OUT_DMAC 0x002C
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090039#define REG_OUT_SEL 0x0030
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080040#define REG_IN_DMAC 0x0038
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090041
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090042/* master register */
43#define MST_CLK_RST 0x0210
44#define MST_SOFT_RST 0x0214
45#define MST_FIFO_SZ 0x0218
46
47/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090048#define A_MST_CTLR 0x0180
49#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090050#define CPU_INT_ST 0x01F4
51#define CPU_IEMSK 0x01F8
52#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090053#define INT_ST 0x0200
54#define IEMSK 0x0204
55#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090056
57/* DO_FMT */
58/* DI_FMT */
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080059#define CR_BWS_MASK (0x3 << 20) /* FSI2 */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090060#define CR_BWS_24 (0x0 << 20) /* FSI2 */
61#define CR_BWS_16 (0x1 << 20) /* FSI2 */
62#define CR_BWS_20 (0x2 << 20) /* FSI2 */
63
64#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
65#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
66#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
67
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090068#define CR_MONO (0x0 << 4)
69#define CR_MONO_D (0x1 << 4)
70#define CR_PCM (0x2 << 4)
71#define CR_I2S (0x3 << 4)
72#define CR_TDM (0x4 << 4)
73#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090074
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080075/* OUT_DMAC */
76/* IN_DMAC */
77#define VDMD_MASK (0x3 << 4)
78#define VDMD_FRONT (0x0 << 4) /* Package in front */
79#define VDMD_BACK (0x1 << 4) /* Package in back */
80#define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
81
82#define DMA_ON (0x1 << 0)
83
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090084/* DOFF_CTL */
85/* DIFF_CTL */
86#define IRQ_HALF 0x00100000
87#define FIFO_CLR 0x00000001
88
89/* DOFF_ST */
90#define ERR_OVER 0x00000010
91#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090092#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090093
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090094/* CKG1 */
95#define ACKMD_MASK 0x00007000
96#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090097#define DIMD (1 << 4)
98#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090099
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900100/* A/B MST_CTLR */
101#define BP (1 << 4) /* Fix the signal of Biphase output */
102#define SE (1 << 0) /* Fix the master clock */
103
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900104/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900105#define CRB (1 << 4)
106#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900107
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900108/* IO SHIFT / MACRO */
109#define BI_SHIFT 12
110#define BO_SHIFT 8
111#define AI_SHIFT 4
112#define AO_SHIFT 0
113#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900114
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900115/* SOFT_RST */
116#define PBSR (1 << 12) /* Port B Software Reset */
117#define PASR (1 << 8) /* Port A Software Reset */
118#define IR (1 << 4) /* Interrupt Reset */
119#define FSISR (1 << 0) /* Software Reset */
120
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900121/* OUT_SEL (FSI2) */
122#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
123 /* 1: Biphase and serial */
124
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900125/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900126#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900127
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900128#define FSI_RATES SNDRV_PCM_RATE_8000_96000
129
130#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
131
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800132typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900133
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900134/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900135 * FSI driver use below type name for variable
136 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900137 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900138 * xxx_pos : position of data
139 * xxx_capa : capacity of data
140 */
141
142/*
143 * period/frame/sample image
144 *
145 * ex) PCM (2ch)
146 *
147 * period pos period pos
148 * [n] [n + 1]
149 * |<-------------------- period--------------------->|
150 * ==|============================================ ... =|==
151 * | |
152 * ||<----- frame ----->|<------ frame ----->| ... |
153 * |+--------------------+--------------------+- ... |
154 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
155 * |+--------------------+--------------------+- ... |
156 * ==|============================================ ... =|==
157 */
158
159/*
160 * FSI FIFO image
161 *
162 * | |
163 * | |
164 * | [ sample ] |
165 * | [ sample ] |
166 * | [ sample ] |
167 * | [ sample ] |
168 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900169 */
170
171/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900172 * struct
173 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900174
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800175struct fsi_stream_handler;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900176struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900177
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800178 /*
179 * these are initialized by fsi_stream_init()
180 */
181 struct snd_pcm_substream *substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900182 int fifo_sample_capa; /* sample capacity of FSI FIFO */
183 int buff_sample_capa; /* sample capacity of ALSA buffer */
184 int buff_sample_pos; /* sample position of ALSA buffer */
185 int period_samples; /* sample number / 1 period */
186 int period_pos; /* current period position */
Kuninori Morimotoc1e6f102012-02-03 00:50:09 -0800187 int sample_width; /* sample width */
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900188 int uerr_num;
189 int oerr_num;
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800190
191 /*
192 * thse are initialized by fsi_handler_init()
193 */
194 struct fsi_stream_handler *handler;
195 struct fsi_priv *priv;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800196
197 /*
198 * these are for DMAEngine
199 */
200 struct dma_chan *chan;
201 struct sh_dmae_slave slave; /* see fsi_handler_init() */
202 struct tasklet_struct tasklet;
203 dma_addr_t dma;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900204};
205
206struct fsi_priv {
207 void __iomem *base;
208 struct fsi_master *master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800209 struct sh_fsi_port_info *info;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900210
211 struct fsi_stream playback;
212 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900213
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -0700214 u32 fmt;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900215
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900216 int chan_num:16;
217 int clk_master:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900218 int spdif:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900219
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000220 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900221};
222
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800223struct fsi_stream_handler {
Kuninori Morimoto83344022012-02-03 00:59:02 -0800224 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
225 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800226 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io);
227 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
228 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800229 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
230 int enable);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800231};
232#define fsi_stream_handler_call(io, func, args...) \
233 (!(io) ? -ENODEV : \
234 !((io)->handler->func) ? 0 : \
235 (io)->handler->func(args))
236
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900237struct fsi_core {
238 int ver;
239
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900240 u32 int_st;
241 u32 iemsk;
242 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900243 u32 a_mclk;
244 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900245};
246
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247struct fsi_master {
248 void __iomem *base;
249 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900250 struct fsi_priv fsia;
251 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900252 struct fsi_core *core;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900253 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900254};
255
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800256static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
257
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900258/*
259 * basic read write function
260 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900261
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200262static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900263{
264 /* valid data area is 24bit */
265 data &= 0x00ffffff;
266
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100267 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900268}
269
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200270static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900271{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100272 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900273}
274
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200275static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900276{
277 u32 val = __fsi_reg_read(reg);
278
279 val &= ~mask;
280 val |= data & mask;
281
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100282 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900283}
284
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900285#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700286 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900287
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900288#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700289 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900290
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900291#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700292 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900293
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900294#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
295#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
296static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900297{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900298 u32 ret;
299 unsigned long flags;
300
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900301 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200302 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900303 spin_unlock_irqrestore(&master->lock, flags);
304
305 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900306}
307
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900308#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
309#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
310static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900311 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900312{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900313 unsigned long flags;
314
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900315 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200316 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900317 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900318}
319
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900320/*
321 * basic function
322 */
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700323static int fsi_version(struct fsi_master *master)
324{
325 return master->core->ver;
326}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900327
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900328static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900329{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900330 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900331}
332
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900333static int fsi_is_clk_master(struct fsi_priv *fsi)
334{
335 return fsi->clk_master;
336}
337
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900338static int fsi_is_port_a(struct fsi_priv *fsi)
339{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900340 return fsi->master->base == fsi->base;
341}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900342
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900343static int fsi_is_spdif(struct fsi_priv *fsi)
344{
345 return fsi->spdif;
346}
347
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800348static int fsi_is_play(struct snd_pcm_substream *substream)
349{
350 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
351}
352
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900353static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900354{
355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900356
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000357 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900358}
359
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900360static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900361{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000362 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900363
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000364 if (dai->id == 0)
365 return &master->fsia;
366 else
367 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900368}
369
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900370static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
371{
372 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
373}
374
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800375static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900376{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800377 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900378 return NULL;
379
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800380 return fsi->info->set_rate;
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900381}
382
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900383static u32 fsi_get_info_flags(struct fsi_priv *fsi)
384{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800385 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900386 return 0;
387
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800388 return fsi->info->flags;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900389}
390
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800391static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900392{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800393 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900394 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900395 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900396
397 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900398 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900399 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900400 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900401
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900402 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900403}
404
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900405static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
406{
407 return frames * fsi->chan_num;
408}
409
410static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
411{
412 return samples / fsi->chan_num;
413}
414
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800415static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
416 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900417{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800418 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900419 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900420 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900421
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900422 status = is_play ?
423 fsi_reg_read(fsi, DOFF_ST) :
424 fsi_reg_read(fsi, DIFF_ST);
425
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900426 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900427
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900428 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900429}
430
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900431static void fsi_count_fifo_err(struct fsi_priv *fsi)
432{
433 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
434 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
435
436 if (ostatus & ERR_OVER)
437 fsi->playback.oerr_num++;
438
439 if (ostatus & ERR_UNDER)
440 fsi->playback.uerr_num++;
441
442 if (istatus & ERR_OVER)
443 fsi->capture.oerr_num++;
444
445 if (istatus & ERR_UNDER)
446 fsi->capture.uerr_num++;
447
448 fsi_reg_write(fsi, DOFF_ST, 0);
449 fsi_reg_write(fsi, DIFF_ST, 0);
450}
451
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900452/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800453 * fsi_stream_xx() function
454 */
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800455static inline int fsi_stream_is_play(struct fsi_priv *fsi,
456 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800457{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800458 return &fsi->playback == io;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800459}
460
461static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800462 struct snd_pcm_substream *substream)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800463{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800464 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800465}
466
467static int fsi_stream_is_working(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800468 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800469{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800470 struct fsi_master *master = fsi_get_master(fsi);
471 unsigned long flags;
472 int ret;
473
474 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800475 ret = !!(io->substream && io->substream->runtime);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800476 spin_unlock_irqrestore(&master->lock, flags);
477
478 return ret;
479}
480
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800481static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
482{
483 return io->priv;
484}
485
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800486static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800487 struct fsi_stream *io,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800488 struct snd_pcm_substream *substream)
489{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800490 struct snd_pcm_runtime *runtime = substream->runtime;
491 struct fsi_master *master = fsi_get_master(fsi);
492 unsigned long flags;
493
494 spin_lock_irqsave(&master->lock, flags);
495 io->substream = substream;
496 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
497 io->buff_sample_pos = 0;
498 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
499 io->period_pos = 0;
500 io->sample_width = samples_to_bytes(runtime, 1);
501 io->oerr_num = -1; /* ignore 1st err */
502 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimoto83344022012-02-03 00:59:02 -0800503 fsi_stream_handler_call(io, init, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800504 spin_unlock_irqrestore(&master->lock, flags);
505}
506
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800507static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800508{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800509 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
510 struct fsi_master *master = fsi_get_master(fsi);
511 unsigned long flags;
512
513 spin_lock_irqsave(&master->lock, flags);
514
515 if (io->oerr_num > 0)
516 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
517
518 if (io->uerr_num > 0)
519 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
520
Kuninori Morimoto83344022012-02-03 00:59:02 -0800521 fsi_stream_handler_call(io, quit, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800522 io->substream = NULL;
523 io->buff_sample_capa = 0;
524 io->buff_sample_pos = 0;
525 io->period_samples = 0;
526 io->period_pos = 0;
527 io->sample_width = 0;
528 io->oerr_num = 0;
529 io->uerr_num = 0;
530 spin_unlock_irqrestore(&master->lock, flags);
531}
532
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800533static int fsi_stream_transfer(struct fsi_stream *io)
534{
535 struct fsi_priv *fsi = fsi_stream_to_priv(io);
536 if (!fsi)
537 return -EIO;
538
539 return fsi_stream_handler_call(io, transfer, fsi, io);
540}
541
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800542#define fsi_stream_start(fsi, io)\
543 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
544
545#define fsi_stream_stop(fsi, io)\
546 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
547
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800548static int fsi_stream_probe(struct fsi_priv *fsi)
549{
550 struct fsi_stream *io;
551 int ret1, ret2;
552
553 io = &fsi->playback;
554 ret1 = fsi_stream_handler_call(io, probe, fsi, io);
555
556 io = &fsi->capture;
557 ret2 = fsi_stream_handler_call(io, probe, fsi, io);
558
559 if (ret1 < 0)
560 return ret1;
561 if (ret2 < 0)
562 return ret2;
563
564 return 0;
565}
566
567static int fsi_stream_remove(struct fsi_priv *fsi)
568{
569 struct fsi_stream *io;
570 int ret1, ret2;
571
572 io = &fsi->playback;
573 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
574
575 io = &fsi->capture;
576 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
577
578 if (ret1 < 0)
579 return ret1;
580 if (ret2 < 0)
581 return ret2;
582
583 return 0;
584}
585
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800586/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900587 * irq function
588 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900589
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800590static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900591{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800592 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900593 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900594
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900595 fsi_core_mask_set(master, imsk, data, data);
596 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900597}
598
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800599static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900600{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800601 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900602 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900603
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900604 fsi_core_mask_set(master, imsk, data, 0);
605 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900606}
607
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900608static u32 fsi_irq_get_status(struct fsi_master *master)
609{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900610 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900611}
612
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900613static void fsi_irq_clear_status(struct fsi_priv *fsi)
614{
615 u32 data = 0;
616 struct fsi_master *master = fsi_get_master(fsi);
617
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800618 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
619 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900620
621 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900622 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900623}
624
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900625/*
626 * SPDIF master clock function
627 *
628 * These functions are used later FSI2
629 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900630static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
631{
632 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900633 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900634
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900635 mask = BP | SE;
636 val = enable ? mask : 0;
637
638 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900639 fsi_core_mask_set(master, a_mclk, mask, val) :
640 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900641}
642
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900643/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900644 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900645 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900646static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
647 long rate, int enable)
648{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800649 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900650 int ret;
651
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800652 if (!set_rate)
653 return 0;
654
655 ret = set_rate(dev, rate, enable);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900656 if (ret < 0) /* error */
657 return ret;
658
659 if (!enable)
660 return 0;
661
662 if (ret > 0) {
663 u32 data = 0;
664
665 switch (ret & SH_FSI_ACKMD_MASK) {
666 default:
667 /* FALL THROUGH */
668 case SH_FSI_ACKMD_512:
669 data |= (0x0 << 12);
670 break;
671 case SH_FSI_ACKMD_256:
672 data |= (0x1 << 12);
673 break;
674 case SH_FSI_ACKMD_128:
675 data |= (0x2 << 12);
676 break;
677 case SH_FSI_ACKMD_64:
678 data |= (0x3 << 12);
679 break;
680 case SH_FSI_ACKMD_32:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700681 data |= (0x4 << 12);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900682 break;
683 }
684
685 switch (ret & SH_FSI_BPFMD_MASK) {
686 default:
687 /* FALL THROUGH */
688 case SH_FSI_BPFMD_32:
689 data |= (0x0 << 8);
690 break;
691 case SH_FSI_BPFMD_64:
692 data |= (0x1 << 8);
693 break;
694 case SH_FSI_BPFMD_128:
695 data |= (0x2 << 8);
696 break;
697 case SH_FSI_BPFMD_256:
698 data |= (0x3 << 8);
699 break;
700 case SH_FSI_BPFMD_512:
701 data |= (0x4 << 8);
702 break;
703 case SH_FSI_BPFMD_16:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700704 data |= (0x7 << 8);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900705 break;
706 }
707
708 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
709 udelay(10);
710 ret = 0;
711 }
712
713 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900714}
715
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900716/*
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800717 * pio data transfer handler
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900718 */
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800719static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
720{
721 u16 *buf = (u16 *)_buf;
722 int i;
723
724 for (i = 0; i < samples; i++)
725 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
726}
727
728static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
729{
730 u16 *buf = (u16 *)_buf;
731 int i;
732
733 for (i = 0; i < samples; i++)
734 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
735}
736
737static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
738{
739 u32 *buf = (u32 *)_buf;
740 int i;
741
742 for (i = 0; i < samples; i++)
743 fsi_reg_write(fsi, DODT, *(buf + i));
744}
745
746static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
747{
748 u32 *buf = (u32 *)_buf;
749 int i;
750
751 for (i = 0; i < samples; i++)
752 *(buf + i) = fsi_reg_read(fsi, DIDT);
753}
754
755static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
756{
757 struct snd_pcm_runtime *runtime = io->substream->runtime;
758
759 return runtime->dma_area +
760 samples_to_bytes(runtime, io->buff_sample_pos);
761}
762
763static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800764 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
765 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
766 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900767{
768 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800769 struct snd_pcm_substream *substream;
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800770 u8 *buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900771 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900772
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800773 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900774 return -EINVAL;
775
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900776 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900777 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900778 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900779
780 /* FSI FIFO has limit.
781 * So, this driver can not send periods data at a time
782 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900783 if (io->buff_sample_pos >=
784 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900785
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900786 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900787 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900788
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900789 if (0 == io->period_pos)
790 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900791 }
792
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800793 buf = fsi_pio_get_area(fsi, io);
794
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800795 switch (io->sample_width) {
796 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800797 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800798 break;
799 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800800 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800801 break;
802 default:
803 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900804 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900805
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900806 /* update buff_sample_pos */
807 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900808
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900809 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900810 snd_pcm_period_elapsed(substream);
811
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900812 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900813}
814
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800815static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900816{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800817 int sample_residues; /* samples in FSI fifo */
818 int sample_space; /* ALSA free samples space */
819 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800820
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800821 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800822 sample_space = io->buff_sample_capa - io->buff_sample_pos;
823
824 samples = min(sample_residues, sample_space);
825
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800826 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800827 fsi_pio_pop16,
828 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800829 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900830}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900831
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800832static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900833{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800834 int sample_residues; /* ALSA residue samples */
835 int sample_space; /* FSI fifo free samples space */
836 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800837
838 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
839 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800840 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800841
842 samples = min(sample_residues, sample_space);
843
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800844 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800845 fsi_pio_push16,
846 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800847 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900848}
849
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800850static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
851 int enable)
852{
853 struct fsi_master *master = fsi_get_master(fsi);
854 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
855
856 if (enable)
857 fsi_irq_enable(fsi, io);
858 else
859 fsi_irq_disable(fsi, io);
860
861 if (fsi_is_clk_master(fsi))
862 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
863}
864
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800865static struct fsi_stream_handler fsi_pio_push_handler = {
866 .transfer = fsi_pio_push,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800867 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800868};
869
870static struct fsi_stream_handler fsi_pio_pop_handler = {
871 .transfer = fsi_pio_pop,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800872 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800873};
874
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900875static irqreturn_t fsi_interrupt(int irq, void *data)
876{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900877 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900878 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900879
880 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900881 fsi_master_mask_set(master, SOFT_RST, IR, 0);
882 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900883
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900884 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800885 fsi_stream_transfer(&master->fsia.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900886 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800887 fsi_stream_transfer(&master->fsib.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900888 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800889 fsi_stream_transfer(&master->fsia.capture);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900890 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800891 fsi_stream_transfer(&master->fsib.capture);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900892
893 fsi_count_fifo_err(&master->fsia);
894 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900895
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900896 fsi_irq_clear_status(&master->fsia);
897 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900898
899 return IRQ_HANDLED;
900}
901
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900902/*
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800903 * dma data transfer handler
904 */
905static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
906{
907 struct snd_pcm_runtime *runtime = io->substream->runtime;
908 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
909 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
910 DMA_TO_DEVICE : DMA_FROM_DEVICE;
911
912 io->dma = dma_map_single(dai->dev, runtime->dma_area,
913 snd_pcm_lib_buffer_bytes(io->substream), dir);
914 return 0;
915}
916
917static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
918{
919 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
920 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
921 DMA_TO_DEVICE : DMA_FROM_DEVICE;
922
923 dma_unmap_single(dai->dev, io->dma,
924 snd_pcm_lib_buffer_bytes(io->substream), dir);
925 return 0;
926}
927
928static void fsi_dma_complete(void *data)
929{
930 struct fsi_stream *io = (struct fsi_stream *)data;
931 struct fsi_priv *fsi = fsi_stream_to_priv(io);
932 struct snd_pcm_runtime *runtime = io->substream->runtime;
933 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
934 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
935 DMA_TO_DEVICE : DMA_FROM_DEVICE;
936
937 dma_sync_single_for_cpu(dai->dev, io->dma,
938 samples_to_bytes(runtime, io->period_samples), dir);
939
940 io->buff_sample_pos += io->period_samples;
941 io->period_pos++;
942
943 if (io->period_pos >= runtime->periods) {
944 io->period_pos = 0;
945 io->buff_sample_pos = 0;
946 }
947
948 fsi_count_fifo_err(fsi);
949 fsi_stream_transfer(io);
950
951 snd_pcm_period_elapsed(io->substream);
952}
953
954static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
955{
956 struct snd_pcm_runtime *runtime = io->substream->runtime;
957
958 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
959}
960
961static void fsi_dma_do_tasklet(unsigned long data)
962{
963 struct fsi_stream *io = (struct fsi_stream *)data;
964 struct fsi_priv *fsi = fsi_stream_to_priv(io);
965 struct dma_chan *chan;
966 struct snd_soc_dai *dai;
967 struct dma_async_tx_descriptor *desc;
968 struct scatterlist sg;
969 struct snd_pcm_runtime *runtime;
970 enum dma_data_direction dir;
971 dma_cookie_t cookie;
972 int is_play = fsi_stream_is_play(fsi, io);
973 int len;
974 dma_addr_t buf;
975
976 if (!fsi_stream_is_working(fsi, io))
977 return;
978
979 dai = fsi_get_dai(io->substream);
980 chan = io->chan;
981 runtime = io->substream->runtime;
982 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
983 len = samples_to_bytes(runtime, io->period_samples);
984 buf = fsi_dma_get_area(io);
985
986 dma_sync_single_for_device(dai->dev, io->dma, len, dir);
987
988 sg_init_table(&sg, 1);
989 sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
990 len , offset_in_page(buf));
991 sg_dma_address(&sg) = buf;
992 sg_dma_len(&sg) = len;
993
Paul Mundtcdf27f32012-04-17 19:13:04 -0700994 desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir,
995 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800996 if (!desc) {
Paul Mundtcdf27f32012-04-17 19:13:04 -0700997 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800998 return;
999 }
1000
1001 desc->callback = fsi_dma_complete;
1002 desc->callback_param = io;
1003
1004 cookie = desc->tx_submit(desc);
1005 if (cookie < 0) {
1006 dev_err(dai->dev, "tx_submit() fail\n");
1007 return;
1008 }
1009
1010 dma_async_issue_pending(chan);
1011
1012 /*
1013 * FIXME
1014 *
1015 * In DMAEngine case, codec and FSI cannot be started simultaneously
1016 * since FSI is using tasklet.
1017 * Therefore, in capture case, probably FSI FIFO will have got
1018 * overflow error in this point.
1019 * in that case, DMA cannot start transfer until error was cleared.
1020 */
1021 if (!is_play) {
1022 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1023 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1024 fsi_reg_write(fsi, DIFF_ST, 0);
1025 }
1026 }
1027}
1028
1029static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1030{
1031 struct sh_dmae_slave *slave = param;
1032
1033 chan->private = slave;
1034
1035 return true;
1036}
1037
1038static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1039{
1040 tasklet_schedule(&io->tasklet);
1041
1042 return 0;
1043}
1044
1045static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1046 int start)
1047{
1048 u32 bws;
1049 u32 dma;
1050
1051 switch (io->sample_width * start) {
1052 case 2:
1053 bws = CR_BWS_16;
1054 dma = VDMD_STREAM | DMA_ON;
1055 break;
1056 case 4:
1057 bws = CR_BWS_24;
1058 dma = VDMD_BACK | DMA_ON;
1059 break;
1060 default:
1061 bws = 0;
1062 dma = 0;
1063 }
1064
1065 fsi_reg_mask_set(fsi, DO_FMT, CR_BWS_MASK, bws);
1066 fsi_reg_write(fsi, OUT_DMAC, dma);
1067}
1068
1069static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io)
1070{
1071 dma_cap_mask_t mask;
1072
1073 dma_cap_zero(mask);
1074 dma_cap_set(DMA_SLAVE, mask);
1075
1076 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
1077 if (!io->chan)
1078 return -EIO;
1079
1080 tasklet_init(&io->tasklet, fsi_dma_do_tasklet, (unsigned long)io);
1081
1082 return 0;
1083}
1084
1085static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1086{
1087 tasklet_kill(&io->tasklet);
1088
1089 fsi_stream_stop(fsi, io);
1090
1091 if (io->chan)
1092 dma_release_channel(io->chan);
1093
1094 io->chan = NULL;
1095 return 0;
1096}
1097
1098static struct fsi_stream_handler fsi_dma_push_handler = {
1099 .init = fsi_dma_init,
1100 .quit = fsi_dma_quit,
1101 .probe = fsi_dma_probe,
1102 .transfer = fsi_dma_transfer,
1103 .remove = fsi_dma_remove,
1104 .start_stop = fsi_dma_push_start_stop,
1105};
1106
1107/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001108 * dai ops
1109 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001110static void fsi_fifo_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001111 struct fsi_stream *io,
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001112 struct device *dev)
1113{
1114 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001115 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001116 u32 shift, i;
1117 int frame_capa;
1118
1119 /* get on-chip RAM capacity */
1120 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001121 shift >>= fsi_get_port_shift(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001122 shift &= FIFO_SZ_MASK;
1123 frame_capa = 256 << shift;
1124 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1125
1126 /*
1127 * The maximum number of sample data varies depending
1128 * on the number of channels selected for the format.
1129 *
1130 * FIFOs are used in 4-channel units in 3-channel mode
1131 * and in 8-channel units in 5- to 7-channel mode
1132 * meaning that more FIFOs than the required size of DPRAM
1133 * are used.
1134 *
1135 * ex) if 256 words of DP-RAM is connected
1136 * 1 channel: 256 (256 x 1 = 256)
1137 * 2 channels: 128 (128 x 2 = 256)
1138 * 3 channels: 64 ( 64 x 3 = 192)
1139 * 4 channels: 64 ( 64 x 4 = 256)
1140 * 5 channels: 32 ( 32 x 5 = 160)
1141 * 6 channels: 32 ( 32 x 6 = 192)
1142 * 7 channels: 32 ( 32 x 7 = 224)
1143 * 8 channels: 32 ( 32 x 8 = 256)
1144 */
1145 for (i = 1; i < fsi->chan_num; i <<= 1)
1146 frame_capa >>= 1;
1147 dev_dbg(dev, "%d channel %d store\n",
1148 fsi->chan_num, frame_capa);
1149
1150 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1151
1152 /*
1153 * set interrupt generation factor
1154 * clear FIFO
1155 */
1156 if (is_play) {
1157 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1158 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1159 } else {
1160 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1161 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1162 }
1163}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001164
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001165static int fsi_hw_startup(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001166 struct fsi_stream *io,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001167 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001168{
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001169 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001170 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001171 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001172
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001173 /* clock setting */
1174 if (fsi_is_clk_master(fsi))
1175 data = DIMD | DOMD;
1176
1177 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001178
1179 /* clock inversion (CKG2) */
1180 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +09001181 if (SH_FSI_LRM_INV & flags)
1182 data |= 1 << 12;
1183 if (SH_FSI_BRM_INV & flags)
1184 data |= 1 << 8;
1185 if (SH_FSI_LRS_INV & flags)
1186 data |= 1 << 4;
1187 if (SH_FSI_BRS_INV & flags)
1188 data |= 1 << 0;
1189
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001190 fsi_reg_write(fsi, CKG2, data);
1191
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001192 /* set format */
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001193 fsi_reg_write(fsi, DO_FMT, fsi->fmt);
1194 fsi_reg_write(fsi, DI_FMT, fsi->fmt);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001195
1196 /* spdif ? */
1197 if (fsi_is_spdif(fsi)) {
1198 fsi_spdif_clk_ctrl(fsi, 1);
1199 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1200 }
1201
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001202 /*
1203 * FIXME
1204 *
1205 * FSI driver assumed that data package is in-back.
1206 * FSI2 chip can select it.
1207 */
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001208 if (fsi_version(master) >= 2) {
Kuninori Morimoto4d62ec12012-05-17 17:33:49 -07001209 fsi_reg_write(fsi, OUT_DMAC, VDMD_BACK);
1210 fsi_reg_write(fsi, IN_DMAC, VDMD_BACK);
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001211 }
1212
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001213 /* irq clear */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001214 fsi_irq_disable(fsi, io);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001215 fsi_irq_clear_status(fsi);
1216
1217 /* fifo init */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001218 fsi_fifo_init(fsi, io, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001219
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +09001220 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001221}
1222
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001223static void fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001224 struct device *dev)
1225{
1226 if (fsi_is_clk_master(fsi))
1227 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001228}
1229
1230static int fsi_dai_startup(struct snd_pcm_substream *substream,
1231 struct snd_soc_dai *dai)
1232{
1233 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001234
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001235 fsi->rate = 0;
1236
1237 return 0;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001238}
1239
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001240static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1241 struct snd_soc_dai *dai)
1242{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001243 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001244
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001245 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001246}
1247
1248static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1249 struct snd_soc_dai *dai)
1250{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001251 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001252 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001253 int ret = 0;
1254
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001255 switch (cmd) {
1256 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001257 fsi_stream_init(fsi, io, substream);
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001258 fsi_hw_startup(fsi, io, dai->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001259 ret = fsi_stream_transfer(io);
1260 if (0 == ret)
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001261 fsi_stream_start(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001262 break;
1263 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001264 fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001265 fsi_stream_stop(fsi, io);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001266 fsi_stream_quit(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001267 break;
1268 }
1269
1270 return ret;
1271}
1272
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001273static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1274{
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001275 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1276 case SND_SOC_DAIFMT_I2S:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001277 fsi->fmt = CR_I2S;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001278 fsi->chan_num = 2;
1279 break;
1280 case SND_SOC_DAIFMT_LEFT_J:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001281 fsi->fmt = CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001282 fsi->chan_num = 2;
1283 break;
1284 default:
1285 return -EINVAL;
1286 }
1287
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001288 return 0;
1289}
1290
1291static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1292{
1293 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001294
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001295 if (fsi_version(master) < 2)
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001296 return -EINVAL;
1297
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001298 fsi->fmt = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001299 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001300 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001301
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001302 return 0;
1303}
1304
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001305static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1306{
1307 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001308 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001309 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001310 int ret;
1311
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001312 /* set master/slave audio interface */
1313 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1314 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001315 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001316 break;
1317 case SND_SOC_DAIFMT_CBS_CFS:
1318 break;
1319 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001320 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001321 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001322
1323 if (fsi_is_clk_master(fsi) && !set_rate) {
1324 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001325 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001326 }
1327
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001328 /* set format */
1329 switch (flags & SH_FSI_FMT_MASK) {
1330 case SH_FSI_FMT_DAI:
1331 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1332 break;
1333 case SH_FSI_FMT_SPDIF:
1334 ret = fsi_set_fmt_spdif(fsi);
1335 break;
1336 default:
1337 ret = -EINVAL;
1338 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001339
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001340 return ret;
1341}
1342
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001343static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1344 struct snd_pcm_hw_params *params,
1345 struct snd_soc_dai *dai)
1346{
1347 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001348 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001349 int ret;
1350
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001351 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001352 return 0;
1353
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001354 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1355 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001356 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001357
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001358 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001359
1360 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001361}
1362
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001363static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001364 .startup = fsi_dai_startup,
1365 .shutdown = fsi_dai_shutdown,
1366 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001367 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001368 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001369};
1370
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001371/*
1372 * pcm ops
1373 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001374
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001375static struct snd_pcm_hardware fsi_pcm_hardware = {
1376 .info = SNDRV_PCM_INFO_INTERLEAVED |
1377 SNDRV_PCM_INFO_MMAP |
1378 SNDRV_PCM_INFO_MMAP_VALID |
1379 SNDRV_PCM_INFO_PAUSE,
1380 .formats = FSI_FMTS,
1381 .rates = FSI_RATES,
1382 .rate_min = 8000,
1383 .rate_max = 192000,
1384 .channels_min = 1,
1385 .channels_max = 2,
1386 .buffer_bytes_max = 64 * 1024,
1387 .period_bytes_min = 32,
1388 .period_bytes_max = 8192,
1389 .periods_min = 1,
1390 .periods_max = 32,
1391 .fifo_size = 256,
1392};
1393
1394static int fsi_pcm_open(struct snd_pcm_substream *substream)
1395{
1396 struct snd_pcm_runtime *runtime = substream->runtime;
1397 int ret = 0;
1398
1399 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1400
1401 ret = snd_pcm_hw_constraint_integer(runtime,
1402 SNDRV_PCM_HW_PARAM_PERIODS);
1403
1404 return ret;
1405}
1406
1407static int fsi_hw_params(struct snd_pcm_substream *substream,
1408 struct snd_pcm_hw_params *hw_params)
1409{
1410 return snd_pcm_lib_malloc_pages(substream,
1411 params_buffer_bytes(hw_params));
1412}
1413
1414static int fsi_hw_free(struct snd_pcm_substream *substream)
1415{
1416 return snd_pcm_lib_free_pages(substream);
1417}
1418
1419static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1420{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001421 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001422 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001423
Kuninori Morimoto19878772012-02-08 16:57:29 -08001424 return fsi_sample2frame(fsi, io->buff_sample_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001425}
1426
1427static struct snd_pcm_ops fsi_pcm_ops = {
1428 .open = fsi_pcm_open,
1429 .ioctl = snd_pcm_lib_ioctl,
1430 .hw_params = fsi_hw_params,
1431 .hw_free = fsi_hw_free,
1432 .pointer = fsi_pointer,
1433};
1434
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001435/*
1436 * snd_soc_platform
1437 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001438
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001439#define PREALLOC_BUFFER (32 * 1024)
1440#define PREALLOC_BUFFER_MAX (32 * 1024)
1441
1442static void fsi_pcm_free(struct snd_pcm *pcm)
1443{
1444 snd_pcm_lib_preallocate_free_for_all(pcm);
1445}
1446
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001447static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001448{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001449 struct snd_pcm *pcm = rtd->pcm;
1450
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001451 /*
1452 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1453 * in MMAP mode (i.e. aplay -M)
1454 */
1455 return snd_pcm_lib_preallocate_pages_for_all(
1456 pcm,
1457 SNDRV_DMA_TYPE_CONTINUOUS,
1458 snd_dma_continuous_data(GFP_KERNEL),
1459 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1460}
1461
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001462/*
1463 * alsa struct
1464 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001465
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001467 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001469 .playback = {
1470 .rates = FSI_RATES,
1471 .formats = FSI_FMTS,
1472 .channels_min = 1,
1473 .channels_max = 8,
1474 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001475 .capture = {
1476 .rates = FSI_RATES,
1477 .formats = FSI_FMTS,
1478 .channels_min = 1,
1479 .channels_max = 8,
1480 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001481 .ops = &fsi_dai_ops,
1482 },
1483 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001484 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001485 .playback = {
1486 .rates = FSI_RATES,
1487 .formats = FSI_FMTS,
1488 .channels_min = 1,
1489 .channels_max = 8,
1490 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001491 .capture = {
1492 .rates = FSI_RATES,
1493 .formats = FSI_FMTS,
1494 .channels_min = 1,
1495 .channels_max = 8,
1496 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001497 .ops = &fsi_dai_ops,
1498 },
1499};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001500
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501static struct snd_soc_platform_driver fsi_soc_platform = {
1502 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001503 .pcm_new = fsi_pcm_new,
1504 .pcm_free = fsi_pcm_free,
1505};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001506
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001507/*
1508 * platform function
1509 */
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001510static void fsi_handler_init(struct fsi_priv *fsi)
1511{
1512 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1513 fsi->playback.priv = fsi;
1514 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1515 fsi->capture.priv = fsi;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001516
1517 if (fsi->info->tx_id) {
1518 fsi->playback.slave.slave_id = fsi->info->tx_id;
1519 fsi->playback.handler = &fsi_dma_push_handler;
1520 }
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001521}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001522
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001523static int fsi_probe(struct platform_device *pdev)
1524{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001525 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001526 const struct platform_device_id *id_entry;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001527 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001528 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001529 unsigned int irq;
1530 int ret;
1531
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001532 id_entry = pdev->id_entry;
1533 if (!id_entry) {
1534 dev_err(&pdev->dev, "unknown fsi device\n");
1535 return -ENODEV;
1536 }
1537
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001538 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1539 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001540 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001541 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1542 ret = -ENODEV;
1543 goto exit;
1544 }
1545
1546 master = kzalloc(sizeof(*master), GFP_KERNEL);
1547 if (!master) {
1548 dev_err(&pdev->dev, "Could not allocate master\n");
1549 ret = -ENOMEM;
1550 goto exit;
1551 }
1552
1553 master->base = ioremap_nocache(res->start, resource_size(res));
1554 if (!master->base) {
1555 ret = -ENXIO;
1556 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1557 goto exit_kfree;
1558 }
1559
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001560 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001561 master->irq = irq;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001562 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001563 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001564
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001565 /* FSI A setting */
1566 master->fsia.base = master->base;
1567 master->fsia.master = master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001568 master->fsia.info = &info->port_a;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001569 fsi_handler_init(&master->fsia);
1570 ret = fsi_stream_probe(&master->fsia);
1571 if (ret < 0) {
1572 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1573 goto exit_iounmap;
1574 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001575
1576 /* FSI B setting */
1577 master->fsib.base = master->base + 0x40;
1578 master->fsib.master = master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001579 master->fsib.info = &info->port_b;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001580 fsi_handler_init(&master->fsib);
1581 ret = fsi_stream_probe(&master->fsib);
1582 if (ret < 0) {
1583 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1584 goto exit_fsia;
1585 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001586
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001587 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001589
Yong Zhang88e24c32011-09-22 16:59:20 +08001590 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001591 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001592 if (ret) {
1593 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001594 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001595 }
1596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001597 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001598 if (ret < 0) {
1599 dev_err(&pdev->dev, "cannot snd soc register\n");
1600 goto exit_free_irq;
1601 }
1602
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001603 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1604 ARRAY_SIZE(fsi_soc_dai));
1605 if (ret < 0) {
1606 dev_err(&pdev->dev, "cannot snd dai register\n");
1607 goto exit_snd_soc;
1608 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001609
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001610 return ret;
1611
1612exit_snd_soc:
1613 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001614exit_free_irq:
1615 free_irq(irq, master);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001616exit_fsib:
1617 fsi_stream_remove(&master->fsib);
1618exit_fsia:
1619 fsi_stream_remove(&master->fsia);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001620exit_iounmap:
1621 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001622 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001623exit_kfree:
1624 kfree(master);
1625 master = NULL;
1626exit:
1627 return ret;
1628}
1629
1630static int fsi_remove(struct platform_device *pdev)
1631{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001632 struct fsi_master *master;
1633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001635
Kuninori Morimotod985f272011-04-08 15:09:25 +09001636 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001637 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001638
Kuninori Morimotod985f272011-04-08 15:09:25 +09001639 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1640 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001641
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001642 fsi_stream_remove(&master->fsia);
1643 fsi_stream_remove(&master->fsib);
1644
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001645 iounmap(master->base);
1646 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001647
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001648 return 0;
1649}
1650
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001651static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001652 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001653 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001654{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001655 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001656 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001657
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001658 fsi_stream_stop(fsi, io);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001659 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001660}
1661
1662static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001663 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001664 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001665{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001666 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001667 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001668
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001669 fsi_hw_startup(fsi, io, dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001670
1671 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001672 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001673
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001674 fsi_stream_start(fsi, io);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001675}
1676
1677static int fsi_suspend(struct device *dev)
1678{
1679 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001680 struct fsi_priv *fsia = &master->fsia;
1681 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001682
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001683 __fsi_suspend(fsia, &fsia->playback, dev);
1684 __fsi_suspend(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001685
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001686 __fsi_suspend(fsib, &fsib->playback, dev);
1687 __fsi_suspend(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001688
1689 return 0;
1690}
1691
1692static int fsi_resume(struct device *dev)
1693{
1694 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001695 struct fsi_priv *fsia = &master->fsia;
1696 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001697
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001698 __fsi_resume(fsia, &fsia->playback, dev);
1699 __fsi_resume(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001700
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001701 __fsi_resume(fsib, &fsib->playback, dev);
1702 __fsi_resume(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001703
1704 return 0;
1705}
1706
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001707static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001708 .suspend = fsi_suspend,
1709 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001710};
1711
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001712static struct fsi_core fsi1_core = {
1713 .ver = 1,
1714
1715 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001716 .int_st = INT_ST,
1717 .iemsk = IEMSK,
1718 .imsk = IMSK,
1719};
1720
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001721static struct fsi_core fsi2_core = {
1722 .ver = 2,
1723
1724 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001725 .int_st = CPU_INT_ST,
1726 .iemsk = CPU_IEMSK,
1727 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001728 .a_mclk = A_MST_CTLR,
1729 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001730};
1731
1732static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001733 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1734 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001735 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001736};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001737MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001738
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001739static struct platform_driver fsi_driver = {
1740 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001741 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001742 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001743 },
1744 .probe = fsi_probe,
1745 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001746 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001747};
1748
Axel Lincb5e8732011-11-25 10:15:07 +08001749module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001750
1751MODULE_LICENSE("GPL");
1752MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1753MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001754MODULE_ALIAS("platform:fsi-pcm-audio");