blob: 7cee22515d9de55c505eae1dd460107b1eacee3a [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 Morimoto766812e2012-05-17 17:36:47 -0700135 * bus options
136 *
137 * 0x000000BA
138 *
139 * A : sample widtht 16bit setting
140 * B : sample widtht 24bit setting
141 */
142
143#define SHIFT_16DATA 0
144#define SHIFT_24DATA 4
145
146#define PACKAGE_24BITBUS_BACK 0
147#define PACKAGE_24BITBUS_FRONT 1
148#define PACKAGE_16BITBUS_STREAM 2
149
150#define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
151#define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
152
153/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900154 * FSI driver use below type name for variable
155 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900156 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900157 * xxx_pos : position of data
158 * xxx_capa : capacity of data
159 */
160
161/*
162 * period/frame/sample image
163 *
164 * ex) PCM (2ch)
165 *
166 * period pos period pos
167 * [n] [n + 1]
168 * |<-------------------- period--------------------->|
169 * ==|============================================ ... =|==
170 * | |
171 * ||<----- frame ----->|<------ frame ----->| ... |
172 * |+--------------------+--------------------+- ... |
173 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
174 * |+--------------------+--------------------+- ... |
175 * ==|============================================ ... =|==
176 */
177
178/*
179 * FSI FIFO image
180 *
181 * | |
182 * | |
183 * | [ sample ] |
184 * | [ sample ] |
185 * | [ sample ] |
186 * | [ sample ] |
187 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900188 */
189
190/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900191 * struct
192 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900193
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800194struct fsi_stream_handler;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900195struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900196
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800197 /*
198 * these are initialized by fsi_stream_init()
199 */
200 struct snd_pcm_substream *substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900201 int fifo_sample_capa; /* sample capacity of FSI FIFO */
202 int buff_sample_capa; /* sample capacity of ALSA buffer */
203 int buff_sample_pos; /* sample position of ALSA buffer */
204 int period_samples; /* sample number / 1 period */
205 int period_pos; /* current period position */
Kuninori Morimotoc1e6f102012-02-03 00:50:09 -0800206 int sample_width; /* sample width */
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900207 int uerr_num;
208 int oerr_num;
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800209
210 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700211 * bus options
212 */
213 u32 bus_option;
214
215 /*
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800216 * thse are initialized by fsi_handler_init()
217 */
218 struct fsi_stream_handler *handler;
219 struct fsi_priv *priv;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800220
221 /*
222 * these are for DMAEngine
223 */
224 struct dma_chan *chan;
225 struct sh_dmae_slave slave; /* see fsi_handler_init() */
226 struct tasklet_struct tasklet;
227 dma_addr_t dma;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900228};
229
230struct fsi_priv {
231 void __iomem *base;
232 struct fsi_master *master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800233 struct sh_fsi_port_info *info;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900234
235 struct fsi_stream playback;
236 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900237
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -0700238 u32 fmt;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900239
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900240 int chan_num:16;
241 int clk_master:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900242 int spdif:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900243
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000244 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900245};
246
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800247struct fsi_stream_handler {
Kuninori Morimoto83344022012-02-03 00:59:02 -0800248 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
249 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800250 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io);
251 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
252 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800253 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
254 int enable);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800255};
256#define fsi_stream_handler_call(io, func, args...) \
257 (!(io) ? -ENODEV : \
258 !((io)->handler->func) ? 0 : \
259 (io)->handler->func(args))
260
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900261struct fsi_core {
262 int ver;
263
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900264 u32 int_st;
265 u32 iemsk;
266 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900267 u32 a_mclk;
268 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900269};
270
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900271struct fsi_master {
272 void __iomem *base;
273 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900274 struct fsi_priv fsia;
275 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900276 struct fsi_core *core;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900277 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900278};
279
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800280static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
281
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900282/*
283 * basic read write function
284 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900285
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200286static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900287{
288 /* valid data area is 24bit */
289 data &= 0x00ffffff;
290
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100291 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900292}
293
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200294static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900295{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100296 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900297}
298
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200299static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900300{
301 u32 val = __fsi_reg_read(reg);
302
303 val &= ~mask;
304 val |= data & mask;
305
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100306 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900307}
308
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900309#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700310 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900311
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900312#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700313 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900314
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900315#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700316 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900317
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900318#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
319#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
320static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900321{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900322 u32 ret;
323 unsigned long flags;
324
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900325 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200326 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900327 spin_unlock_irqrestore(&master->lock, flags);
328
329 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900330}
331
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900332#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
333#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
334static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900335 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900336{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900337 unsigned long flags;
338
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900339 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200340 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900341 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900342}
343
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900344/*
345 * basic function
346 */
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700347static int fsi_version(struct fsi_master *master)
348{
349 return master->core->ver;
350}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900351
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900352static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900353{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900354 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900355}
356
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900357static int fsi_is_clk_master(struct fsi_priv *fsi)
358{
359 return fsi->clk_master;
360}
361
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900362static int fsi_is_port_a(struct fsi_priv *fsi)
363{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900364 return fsi->master->base == fsi->base;
365}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900366
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900367static int fsi_is_spdif(struct fsi_priv *fsi)
368{
369 return fsi->spdif;
370}
371
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800372static int fsi_is_play(struct snd_pcm_substream *substream)
373{
374 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
375}
376
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900377static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900378{
379 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900380
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000381 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900382}
383
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900384static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900385{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000386 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900387
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000388 if (dai->id == 0)
389 return &master->fsia;
390 else
391 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900392}
393
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900394static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
395{
396 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
397}
398
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800399static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900400{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800401 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900402 return NULL;
403
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800404 return fsi->info->set_rate;
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900405}
406
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900407static u32 fsi_get_info_flags(struct fsi_priv *fsi)
408{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800409 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900410 return 0;
411
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800412 return fsi->info->flags;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900413}
414
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800415static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900416{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800417 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900418 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900419 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900420
421 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900422 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900423 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900424 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900425
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900426 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900427}
428
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900429static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
430{
431 return frames * fsi->chan_num;
432}
433
434static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
435{
436 return samples / fsi->chan_num;
437}
438
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800439static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
440 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900441{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800442 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900443 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900444 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900445
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900446 status = is_play ?
447 fsi_reg_read(fsi, DOFF_ST) :
448 fsi_reg_read(fsi, DIFF_ST);
449
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900450 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900451
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900452 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900453}
454
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900455static void fsi_count_fifo_err(struct fsi_priv *fsi)
456{
457 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
458 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
459
460 if (ostatus & ERR_OVER)
461 fsi->playback.oerr_num++;
462
463 if (ostatus & ERR_UNDER)
464 fsi->playback.uerr_num++;
465
466 if (istatus & ERR_OVER)
467 fsi->capture.oerr_num++;
468
469 if (istatus & ERR_UNDER)
470 fsi->capture.uerr_num++;
471
472 fsi_reg_write(fsi, DOFF_ST, 0);
473 fsi_reg_write(fsi, DIFF_ST, 0);
474}
475
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900476/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800477 * fsi_stream_xx() function
478 */
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800479static inline int fsi_stream_is_play(struct fsi_priv *fsi,
480 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800481{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800482 return &fsi->playback == io;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800483}
484
485static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800486 struct snd_pcm_substream *substream)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800487{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800488 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800489}
490
491static int fsi_stream_is_working(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800492 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800493{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800494 struct fsi_master *master = fsi_get_master(fsi);
495 unsigned long flags;
496 int ret;
497
498 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800499 ret = !!(io->substream && io->substream->runtime);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800500 spin_unlock_irqrestore(&master->lock, flags);
501
502 return ret;
503}
504
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800505static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
506{
507 return io->priv;
508}
509
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800510static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800511 struct fsi_stream *io,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800512 struct snd_pcm_substream *substream)
513{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800514 struct snd_pcm_runtime *runtime = substream->runtime;
515 struct fsi_master *master = fsi_get_master(fsi);
516 unsigned long flags;
517
518 spin_lock_irqsave(&master->lock, flags);
519 io->substream = substream;
520 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
521 io->buff_sample_pos = 0;
522 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
523 io->period_pos = 0;
524 io->sample_width = samples_to_bytes(runtime, 1);
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700525 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800526 io->oerr_num = -1; /* ignore 1st err */
527 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimoto83344022012-02-03 00:59:02 -0800528 fsi_stream_handler_call(io, init, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800529 spin_unlock_irqrestore(&master->lock, flags);
530}
531
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800532static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800533{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800534 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
535 struct fsi_master *master = fsi_get_master(fsi);
536 unsigned long flags;
537
538 spin_lock_irqsave(&master->lock, flags);
539
540 if (io->oerr_num > 0)
541 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
542
543 if (io->uerr_num > 0)
544 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
545
Kuninori Morimoto83344022012-02-03 00:59:02 -0800546 fsi_stream_handler_call(io, quit, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800547 io->substream = NULL;
548 io->buff_sample_capa = 0;
549 io->buff_sample_pos = 0;
550 io->period_samples = 0;
551 io->period_pos = 0;
552 io->sample_width = 0;
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700553 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800554 io->oerr_num = 0;
555 io->uerr_num = 0;
556 spin_unlock_irqrestore(&master->lock, flags);
557}
558
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800559static int fsi_stream_transfer(struct fsi_stream *io)
560{
561 struct fsi_priv *fsi = fsi_stream_to_priv(io);
562 if (!fsi)
563 return -EIO;
564
565 return fsi_stream_handler_call(io, transfer, fsi, io);
566}
567
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800568#define fsi_stream_start(fsi, io)\
569 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
570
571#define fsi_stream_stop(fsi, io)\
572 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
573
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800574static int fsi_stream_probe(struct fsi_priv *fsi)
575{
576 struct fsi_stream *io;
577 int ret1, ret2;
578
579 io = &fsi->playback;
580 ret1 = fsi_stream_handler_call(io, probe, fsi, io);
581
582 io = &fsi->capture;
583 ret2 = fsi_stream_handler_call(io, probe, fsi, io);
584
585 if (ret1 < 0)
586 return ret1;
587 if (ret2 < 0)
588 return ret2;
589
590 return 0;
591}
592
593static int fsi_stream_remove(struct fsi_priv *fsi)
594{
595 struct fsi_stream *io;
596 int ret1, ret2;
597
598 io = &fsi->playback;
599 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
600
601 io = &fsi->capture;
602 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
603
604 if (ret1 < 0)
605 return ret1;
606 if (ret2 < 0)
607 return ret2;
608
609 return 0;
610}
611
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800612/*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700613 * format/bus/dma setting
614 */
615static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
616 u32 bus, struct device *dev)
617{
618 struct fsi_master *master = fsi_get_master(fsi);
619 int is_play = fsi_stream_is_play(fsi, io);
620 u32 fmt = fsi->fmt;
621
622 if (fsi_version(master) >= 2) {
623 u32 dma = 0;
624
625 /*
626 * FSI2 needs DMA/Bus setting
627 */
628 switch (bus) {
629 case PACKAGE_24BITBUS_FRONT:
630 fmt |= CR_BWS_24;
631 dma |= VDMD_FRONT;
632 dev_dbg(dev, "24bit bus / package in front\n");
633 break;
634 case PACKAGE_16BITBUS_STREAM:
635 fmt |= CR_BWS_16;
636 dma |= VDMD_STREAM;
637 dev_dbg(dev, "16bit bus / stream mode\n");
638 break;
639 case PACKAGE_24BITBUS_BACK:
640 default:
641 fmt |= CR_BWS_24;
642 dma |= VDMD_BACK;
643 dev_dbg(dev, "24bit bus / package in back\n");
644 break;
645 }
646
647 if (is_play)
648 fsi_reg_write(fsi, OUT_DMAC, dma);
649 else
650 fsi_reg_write(fsi, IN_DMAC, dma);
651 }
652
653 if (is_play)
654 fsi_reg_write(fsi, DO_FMT, fmt);
655 else
656 fsi_reg_write(fsi, DI_FMT, fmt);
657}
658
659/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900660 * irq function
661 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900662
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800663static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900664{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800665 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900666 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900667
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900668 fsi_core_mask_set(master, imsk, data, data);
669 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900670}
671
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800672static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900673{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800674 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900675 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900676
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900677 fsi_core_mask_set(master, imsk, data, 0);
678 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900679}
680
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900681static u32 fsi_irq_get_status(struct fsi_master *master)
682{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900683 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900684}
685
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900686static void fsi_irq_clear_status(struct fsi_priv *fsi)
687{
688 u32 data = 0;
689 struct fsi_master *master = fsi_get_master(fsi);
690
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800691 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
692 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900693
694 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900695 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900696}
697
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900698/*
699 * SPDIF master clock function
700 *
701 * These functions are used later FSI2
702 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900703static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
704{
705 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900706 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900707
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900708 mask = BP | SE;
709 val = enable ? mask : 0;
710
711 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900712 fsi_core_mask_set(master, a_mclk, mask, val) :
713 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900714}
715
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900716/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900717 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900718 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900719static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
720 long rate, int enable)
721{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800722 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900723 int ret;
724
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800725 if (!set_rate)
726 return 0;
727
728 ret = set_rate(dev, rate, enable);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900729 if (ret < 0) /* error */
730 return ret;
731
732 if (!enable)
733 return 0;
734
735 if (ret > 0) {
736 u32 data = 0;
737
738 switch (ret & SH_FSI_ACKMD_MASK) {
739 default:
740 /* FALL THROUGH */
741 case SH_FSI_ACKMD_512:
742 data |= (0x0 << 12);
743 break;
744 case SH_FSI_ACKMD_256:
745 data |= (0x1 << 12);
746 break;
747 case SH_FSI_ACKMD_128:
748 data |= (0x2 << 12);
749 break;
750 case SH_FSI_ACKMD_64:
751 data |= (0x3 << 12);
752 break;
753 case SH_FSI_ACKMD_32:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700754 data |= (0x4 << 12);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900755 break;
756 }
757
758 switch (ret & SH_FSI_BPFMD_MASK) {
759 default:
760 /* FALL THROUGH */
761 case SH_FSI_BPFMD_32:
762 data |= (0x0 << 8);
763 break;
764 case SH_FSI_BPFMD_64:
765 data |= (0x1 << 8);
766 break;
767 case SH_FSI_BPFMD_128:
768 data |= (0x2 << 8);
769 break;
770 case SH_FSI_BPFMD_256:
771 data |= (0x3 << 8);
772 break;
773 case SH_FSI_BPFMD_512:
774 data |= (0x4 << 8);
775 break;
776 case SH_FSI_BPFMD_16:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700777 data |= (0x7 << 8);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900778 break;
779 }
780
781 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
782 udelay(10);
783 ret = 0;
784 }
785
786 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900787}
788
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900789/*
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800790 * pio data transfer handler
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900791 */
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800792static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
793{
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700794 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800795 int i;
796
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700797 if (enable_stream) {
798 /*
799 * stream mode
800 * see
801 * fsi_pio_push_init()
802 */
803 u32 *buf = (u32 *)_buf;
804
805 for (i = 0; i < samples / 2; i++)
806 fsi_reg_write(fsi, DODT, buf[i]);
807 } else {
808 /* normal mode */
809 u16 *buf = (u16 *)_buf;
810
811 for (i = 0; i < samples; i++)
812 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
813 }
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800814}
815
816static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
817{
818 u16 *buf = (u16 *)_buf;
819 int i;
820
821 for (i = 0; i < samples; i++)
822 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
823}
824
825static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
826{
827 u32 *buf = (u32 *)_buf;
828 int i;
829
830 for (i = 0; i < samples; i++)
831 fsi_reg_write(fsi, DODT, *(buf + i));
832}
833
834static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
835{
836 u32 *buf = (u32 *)_buf;
837 int i;
838
839 for (i = 0; i < samples; i++)
840 *(buf + i) = fsi_reg_read(fsi, DIDT);
841}
842
843static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
844{
845 struct snd_pcm_runtime *runtime = io->substream->runtime;
846
847 return runtime->dma_area +
848 samples_to_bytes(runtime, io->buff_sample_pos);
849}
850
851static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800852 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
853 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
854 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900855{
856 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800857 struct snd_pcm_substream *substream;
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800858 u8 *buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900859 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900860
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800861 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900862 return -EINVAL;
863
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900864 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900865 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900866 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900867
868 /* FSI FIFO has limit.
869 * So, this driver can not send periods data at a time
870 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900871 if (io->buff_sample_pos >=
872 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900873
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900874 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900875 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900876
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900877 if (0 == io->period_pos)
878 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900879 }
880
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800881 buf = fsi_pio_get_area(fsi, io);
882
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800883 switch (io->sample_width) {
884 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800885 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800886 break;
887 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800888 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800889 break;
890 default:
891 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900892 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900893
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900894 /* update buff_sample_pos */
895 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900896
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900897 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900898 snd_pcm_period_elapsed(substream);
899
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900900 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900901}
902
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800903static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900904{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800905 int sample_residues; /* samples in FSI fifo */
906 int sample_space; /* ALSA free samples space */
907 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800908
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800909 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800910 sample_space = io->buff_sample_capa - io->buff_sample_pos;
911
912 samples = min(sample_residues, sample_space);
913
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800914 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800915 fsi_pio_pop16,
916 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800917 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900918}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900919
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800920static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900921{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800922 int sample_residues; /* ALSA residue samples */
923 int sample_space; /* FSI fifo free samples space */
924 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800925
926 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
927 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800928 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800929
930 samples = min(sample_residues, sample_space);
931
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800932 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800933 fsi_pio_push16,
934 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800935 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900936}
937
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800938static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
939 int enable)
940{
941 struct fsi_master *master = fsi_get_master(fsi);
942 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
943
944 if (enable)
945 fsi_irq_enable(fsi, io);
946 else
947 fsi_irq_disable(fsi, io);
948
949 if (fsi_is_clk_master(fsi))
950 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
951}
952
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700953static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
954{
955 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
956
957 /*
958 * we can use 16bit stream mode
959 * when "playback" and "16bit data"
960 * and platform allows "stream mode"
961 * see
962 * fsi_pio_push16()
963 */
964 if (enable_stream)
965 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
966 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
967 else
968 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
969 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
970 return 0;
971}
972
973static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
974{
975 /*
976 * always 24bit bus, package back when "capture"
977 */
978 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
979 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
980 return 0;
981}
982
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800983static struct fsi_stream_handler fsi_pio_push_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700984 .init = fsi_pio_push_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800985 .transfer = fsi_pio_push,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800986 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800987};
988
989static struct fsi_stream_handler fsi_pio_pop_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700990 .init = fsi_pio_pop_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800991 .transfer = fsi_pio_pop,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800992 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800993};
994
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900995static irqreturn_t fsi_interrupt(int irq, void *data)
996{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900997 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900998 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900999
1000 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +09001001 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1002 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001003
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001004 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001005 fsi_stream_transfer(&master->fsia.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001006 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001007 fsi_stream_transfer(&master->fsib.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001008 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001009 fsi_stream_transfer(&master->fsia.capture);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001010 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001011 fsi_stream_transfer(&master->fsib.capture);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +09001012
1013 fsi_count_fifo_err(&master->fsia);
1014 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001015
Kuninori Morimoto48d78e52010-12-03 17:37:31 +09001016 fsi_irq_clear_status(&master->fsia);
1017 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001018
1019 return IRQ_HANDLED;
1020}
1021
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001022/*
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001023 * dma data transfer handler
1024 */
1025static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1026{
1027 struct snd_pcm_runtime *runtime = io->substream->runtime;
1028 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1029 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1030 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1031
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001032 /*
1033 * 24bit data : 24bit bus / package in back
1034 * 16bit data : 16bit bus / stream mode
1035 */
1036 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1037 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1038
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001039 io->dma = dma_map_single(dai->dev, runtime->dma_area,
1040 snd_pcm_lib_buffer_bytes(io->substream), dir);
1041 return 0;
1042}
1043
1044static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
1045{
1046 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1047 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1048 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1049
1050 dma_unmap_single(dai->dev, io->dma,
1051 snd_pcm_lib_buffer_bytes(io->substream), dir);
1052 return 0;
1053}
1054
1055static void fsi_dma_complete(void *data)
1056{
1057 struct fsi_stream *io = (struct fsi_stream *)data;
1058 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1059 struct snd_pcm_runtime *runtime = io->substream->runtime;
1060 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1061 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1062 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1063
1064 dma_sync_single_for_cpu(dai->dev, io->dma,
1065 samples_to_bytes(runtime, io->period_samples), dir);
1066
1067 io->buff_sample_pos += io->period_samples;
1068 io->period_pos++;
1069
1070 if (io->period_pos >= runtime->periods) {
1071 io->period_pos = 0;
1072 io->buff_sample_pos = 0;
1073 }
1074
1075 fsi_count_fifo_err(fsi);
1076 fsi_stream_transfer(io);
1077
1078 snd_pcm_period_elapsed(io->substream);
1079}
1080
1081static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
1082{
1083 struct snd_pcm_runtime *runtime = io->substream->runtime;
1084
1085 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
1086}
1087
1088static void fsi_dma_do_tasklet(unsigned long data)
1089{
1090 struct fsi_stream *io = (struct fsi_stream *)data;
1091 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1092 struct dma_chan *chan;
1093 struct snd_soc_dai *dai;
1094 struct dma_async_tx_descriptor *desc;
1095 struct scatterlist sg;
1096 struct snd_pcm_runtime *runtime;
1097 enum dma_data_direction dir;
1098 dma_cookie_t cookie;
1099 int is_play = fsi_stream_is_play(fsi, io);
1100 int len;
1101 dma_addr_t buf;
1102
1103 if (!fsi_stream_is_working(fsi, io))
1104 return;
1105
1106 dai = fsi_get_dai(io->substream);
1107 chan = io->chan;
1108 runtime = io->substream->runtime;
1109 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1110 len = samples_to_bytes(runtime, io->period_samples);
1111 buf = fsi_dma_get_area(io);
1112
1113 dma_sync_single_for_device(dai->dev, io->dma, len, dir);
1114
1115 sg_init_table(&sg, 1);
1116 sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
1117 len , offset_in_page(buf));
1118 sg_dma_address(&sg) = buf;
1119 sg_dma_len(&sg) = len;
1120
Paul Mundtcdf27f32012-04-17 19:13:04 -07001121 desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir,
1122 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001123 if (!desc) {
Paul Mundtcdf27f32012-04-17 19:13:04 -07001124 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001125 return;
1126 }
1127
1128 desc->callback = fsi_dma_complete;
1129 desc->callback_param = io;
1130
1131 cookie = desc->tx_submit(desc);
1132 if (cookie < 0) {
1133 dev_err(dai->dev, "tx_submit() fail\n");
1134 return;
1135 }
1136
1137 dma_async_issue_pending(chan);
1138
1139 /*
1140 * FIXME
1141 *
1142 * In DMAEngine case, codec and FSI cannot be started simultaneously
1143 * since FSI is using tasklet.
1144 * Therefore, in capture case, probably FSI FIFO will have got
1145 * overflow error in this point.
1146 * in that case, DMA cannot start transfer until error was cleared.
1147 */
1148 if (!is_play) {
1149 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1150 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1151 fsi_reg_write(fsi, DIFF_ST, 0);
1152 }
1153 }
1154}
1155
1156static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1157{
1158 struct sh_dmae_slave *slave = param;
1159
1160 chan->private = slave;
1161
1162 return true;
1163}
1164
1165static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1166{
1167 tasklet_schedule(&io->tasklet);
1168
1169 return 0;
1170}
1171
1172static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1173 int start)
1174{
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001175 u32 enable = start ? DMA_ON : 0;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001176
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001177 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001178}
1179
1180static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io)
1181{
1182 dma_cap_mask_t mask;
1183
1184 dma_cap_zero(mask);
1185 dma_cap_set(DMA_SLAVE, mask);
1186
1187 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
1188 if (!io->chan)
1189 return -EIO;
1190
1191 tasklet_init(&io->tasklet, fsi_dma_do_tasklet, (unsigned long)io);
1192
1193 return 0;
1194}
1195
1196static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1197{
1198 tasklet_kill(&io->tasklet);
1199
1200 fsi_stream_stop(fsi, io);
1201
1202 if (io->chan)
1203 dma_release_channel(io->chan);
1204
1205 io->chan = NULL;
1206 return 0;
1207}
1208
1209static struct fsi_stream_handler fsi_dma_push_handler = {
1210 .init = fsi_dma_init,
1211 .quit = fsi_dma_quit,
1212 .probe = fsi_dma_probe,
1213 .transfer = fsi_dma_transfer,
1214 .remove = fsi_dma_remove,
1215 .start_stop = fsi_dma_push_start_stop,
1216};
1217
1218/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001219 * dai ops
1220 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001221static void fsi_fifo_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001222 struct fsi_stream *io,
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001223 struct device *dev)
1224{
1225 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001226 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001227 u32 shift, i;
1228 int frame_capa;
1229
1230 /* get on-chip RAM capacity */
1231 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001232 shift >>= fsi_get_port_shift(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001233 shift &= FIFO_SZ_MASK;
1234 frame_capa = 256 << shift;
1235 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1236
1237 /*
1238 * The maximum number of sample data varies depending
1239 * on the number of channels selected for the format.
1240 *
1241 * FIFOs are used in 4-channel units in 3-channel mode
1242 * and in 8-channel units in 5- to 7-channel mode
1243 * meaning that more FIFOs than the required size of DPRAM
1244 * are used.
1245 *
1246 * ex) if 256 words of DP-RAM is connected
1247 * 1 channel: 256 (256 x 1 = 256)
1248 * 2 channels: 128 (128 x 2 = 256)
1249 * 3 channels: 64 ( 64 x 3 = 192)
1250 * 4 channels: 64 ( 64 x 4 = 256)
1251 * 5 channels: 32 ( 32 x 5 = 160)
1252 * 6 channels: 32 ( 32 x 6 = 192)
1253 * 7 channels: 32 ( 32 x 7 = 224)
1254 * 8 channels: 32 ( 32 x 8 = 256)
1255 */
1256 for (i = 1; i < fsi->chan_num; i <<= 1)
1257 frame_capa >>= 1;
1258 dev_dbg(dev, "%d channel %d store\n",
1259 fsi->chan_num, frame_capa);
1260
1261 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1262
1263 /*
1264 * set interrupt generation factor
1265 * clear FIFO
1266 */
1267 if (is_play) {
1268 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1269 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1270 } else {
1271 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1272 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1273 }
1274}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001275
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001276static int fsi_hw_startup(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001277 struct fsi_stream *io,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001278 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001279{
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001280 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001281 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001282
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001283 /* clock setting */
1284 if (fsi_is_clk_master(fsi))
1285 data = DIMD | DOMD;
1286
1287 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001288
1289 /* clock inversion (CKG2) */
1290 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +09001291 if (SH_FSI_LRM_INV & flags)
1292 data |= 1 << 12;
1293 if (SH_FSI_BRM_INV & flags)
1294 data |= 1 << 8;
1295 if (SH_FSI_LRS_INV & flags)
1296 data |= 1 << 4;
1297 if (SH_FSI_BRS_INV & flags)
1298 data |= 1 << 0;
1299
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001300 fsi_reg_write(fsi, CKG2, data);
1301
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001302 /* spdif ? */
1303 if (fsi_is_spdif(fsi)) {
1304 fsi_spdif_clk_ctrl(fsi, 1);
1305 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1306 }
1307
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001308 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001309 * get bus settings
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001310 */
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001311 data = 0;
1312 switch (io->sample_width) {
1313 case 2:
1314 data = BUSOP_GET(16, io->bus_option);
1315 break;
1316 case 4:
1317 data = BUSOP_GET(24, io->bus_option);
1318 break;
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001319 }
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001320 fsi_format_bus_setup(fsi, io, data, dev);
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001321
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001322 /* irq clear */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001323 fsi_irq_disable(fsi, io);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001324 fsi_irq_clear_status(fsi);
1325
1326 /* fifo init */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001327 fsi_fifo_init(fsi, io, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001328
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +09001329 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001330}
1331
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001332static void fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001333 struct device *dev)
1334{
1335 if (fsi_is_clk_master(fsi))
1336 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001337}
1338
1339static int fsi_dai_startup(struct snd_pcm_substream *substream,
1340 struct snd_soc_dai *dai)
1341{
1342 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001343
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001344 fsi->rate = 0;
1345
1346 return 0;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001347}
1348
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001349static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1350 struct snd_soc_dai *dai)
1351{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001352 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001353
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001354 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001355}
1356
1357static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1358 struct snd_soc_dai *dai)
1359{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001360 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001361 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001362 int ret = 0;
1363
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001364 switch (cmd) {
1365 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001366 fsi_stream_init(fsi, io, substream);
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001367 fsi_hw_startup(fsi, io, dai->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001368 ret = fsi_stream_transfer(io);
1369 if (0 == ret)
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001370 fsi_stream_start(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001371 break;
1372 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001373 fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001374 fsi_stream_stop(fsi, io);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001375 fsi_stream_quit(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001376 break;
1377 }
1378
1379 return ret;
1380}
1381
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001382static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1383{
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001384 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1385 case SND_SOC_DAIFMT_I2S:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001386 fsi->fmt = CR_I2S;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001387 fsi->chan_num = 2;
1388 break;
1389 case SND_SOC_DAIFMT_LEFT_J:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001390 fsi->fmt = CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001391 fsi->chan_num = 2;
1392 break;
1393 default:
1394 return -EINVAL;
1395 }
1396
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001397 return 0;
1398}
1399
1400static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1401{
1402 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001403
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001404 if (fsi_version(master) < 2)
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001405 return -EINVAL;
1406
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001407 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001408 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001409 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001410
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001411 return 0;
1412}
1413
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001414static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1415{
1416 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001417 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001418 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001419 int ret;
1420
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001421 /* set master/slave audio interface */
1422 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1423 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001424 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001425 break;
1426 case SND_SOC_DAIFMT_CBS_CFS:
1427 break;
1428 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001429 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001430 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001431
1432 if (fsi_is_clk_master(fsi) && !set_rate) {
1433 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001434 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001435 }
1436
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001437 /* set format */
1438 switch (flags & SH_FSI_FMT_MASK) {
1439 case SH_FSI_FMT_DAI:
1440 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1441 break;
1442 case SH_FSI_FMT_SPDIF:
1443 ret = fsi_set_fmt_spdif(fsi);
1444 break;
1445 default:
1446 ret = -EINVAL;
1447 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001448
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001449 return ret;
1450}
1451
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001452static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1453 struct snd_pcm_hw_params *params,
1454 struct snd_soc_dai *dai)
1455{
1456 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001457 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001458 int ret;
1459
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001460 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001461 return 0;
1462
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001463 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1464 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001465 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001466
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001467 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001468
1469 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001470}
1471
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001472static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001473 .startup = fsi_dai_startup,
1474 .shutdown = fsi_dai_shutdown,
1475 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001476 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001477 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001478};
1479
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001480/*
1481 * pcm ops
1482 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001483
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001484static struct snd_pcm_hardware fsi_pcm_hardware = {
1485 .info = SNDRV_PCM_INFO_INTERLEAVED |
1486 SNDRV_PCM_INFO_MMAP |
1487 SNDRV_PCM_INFO_MMAP_VALID |
1488 SNDRV_PCM_INFO_PAUSE,
1489 .formats = FSI_FMTS,
1490 .rates = FSI_RATES,
1491 .rate_min = 8000,
1492 .rate_max = 192000,
1493 .channels_min = 1,
1494 .channels_max = 2,
1495 .buffer_bytes_max = 64 * 1024,
1496 .period_bytes_min = 32,
1497 .period_bytes_max = 8192,
1498 .periods_min = 1,
1499 .periods_max = 32,
1500 .fifo_size = 256,
1501};
1502
1503static int fsi_pcm_open(struct snd_pcm_substream *substream)
1504{
1505 struct snd_pcm_runtime *runtime = substream->runtime;
1506 int ret = 0;
1507
1508 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1509
1510 ret = snd_pcm_hw_constraint_integer(runtime,
1511 SNDRV_PCM_HW_PARAM_PERIODS);
1512
1513 return ret;
1514}
1515
1516static int fsi_hw_params(struct snd_pcm_substream *substream,
1517 struct snd_pcm_hw_params *hw_params)
1518{
1519 return snd_pcm_lib_malloc_pages(substream,
1520 params_buffer_bytes(hw_params));
1521}
1522
1523static int fsi_hw_free(struct snd_pcm_substream *substream)
1524{
1525 return snd_pcm_lib_free_pages(substream);
1526}
1527
1528static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1529{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001530 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001531 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001532
Kuninori Morimoto19878772012-02-08 16:57:29 -08001533 return fsi_sample2frame(fsi, io->buff_sample_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001534}
1535
1536static struct snd_pcm_ops fsi_pcm_ops = {
1537 .open = fsi_pcm_open,
1538 .ioctl = snd_pcm_lib_ioctl,
1539 .hw_params = fsi_hw_params,
1540 .hw_free = fsi_hw_free,
1541 .pointer = fsi_pointer,
1542};
1543
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001544/*
1545 * snd_soc_platform
1546 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001547
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001548#define PREALLOC_BUFFER (32 * 1024)
1549#define PREALLOC_BUFFER_MAX (32 * 1024)
1550
1551static void fsi_pcm_free(struct snd_pcm *pcm)
1552{
1553 snd_pcm_lib_preallocate_free_for_all(pcm);
1554}
1555
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001556static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001557{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001558 struct snd_pcm *pcm = rtd->pcm;
1559
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001560 /*
1561 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1562 * in MMAP mode (i.e. aplay -M)
1563 */
1564 return snd_pcm_lib_preallocate_pages_for_all(
1565 pcm,
1566 SNDRV_DMA_TYPE_CONTINUOUS,
1567 snd_dma_continuous_data(GFP_KERNEL),
1568 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1569}
1570
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001571/*
1572 * alsa struct
1573 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001574
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001575static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001576 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001578 .playback = {
1579 .rates = FSI_RATES,
1580 .formats = FSI_FMTS,
1581 .channels_min = 1,
1582 .channels_max = 8,
1583 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001584 .capture = {
1585 .rates = FSI_RATES,
1586 .formats = FSI_FMTS,
1587 .channels_min = 1,
1588 .channels_max = 8,
1589 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001590 .ops = &fsi_dai_ops,
1591 },
1592 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001593 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001594 .playback = {
1595 .rates = FSI_RATES,
1596 .formats = FSI_FMTS,
1597 .channels_min = 1,
1598 .channels_max = 8,
1599 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001600 .capture = {
1601 .rates = FSI_RATES,
1602 .formats = FSI_FMTS,
1603 .channels_min = 1,
1604 .channels_max = 8,
1605 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001606 .ops = &fsi_dai_ops,
1607 },
1608};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610static struct snd_soc_platform_driver fsi_soc_platform = {
1611 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001612 .pcm_new = fsi_pcm_new,
1613 .pcm_free = fsi_pcm_free,
1614};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001615
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001616/*
1617 * platform function
1618 */
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001619static void fsi_handler_init(struct fsi_priv *fsi)
1620{
1621 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1622 fsi->playback.priv = fsi;
1623 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1624 fsi->capture.priv = fsi;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001625
1626 if (fsi->info->tx_id) {
1627 fsi->playback.slave.slave_id = fsi->info->tx_id;
1628 fsi->playback.handler = &fsi_dma_push_handler;
1629 }
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001630}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001631
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001632static int fsi_probe(struct platform_device *pdev)
1633{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001634 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001635 const struct platform_device_id *id_entry;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001636 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001637 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001638 unsigned int irq;
1639 int ret;
1640
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001641 id_entry = pdev->id_entry;
1642 if (!id_entry) {
1643 dev_err(&pdev->dev, "unknown fsi device\n");
1644 return -ENODEV;
1645 }
1646
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001647 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1648 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001649 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001650 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1651 ret = -ENODEV;
1652 goto exit;
1653 }
1654
1655 master = kzalloc(sizeof(*master), GFP_KERNEL);
1656 if (!master) {
1657 dev_err(&pdev->dev, "Could not allocate master\n");
1658 ret = -ENOMEM;
1659 goto exit;
1660 }
1661
1662 master->base = ioremap_nocache(res->start, resource_size(res));
1663 if (!master->base) {
1664 ret = -ENXIO;
1665 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1666 goto exit_kfree;
1667 }
1668
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001669 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001670 master->irq = irq;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001671 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001672 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001673
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001674 /* FSI A setting */
1675 master->fsia.base = master->base;
1676 master->fsia.master = master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001677 master->fsia.info = &info->port_a;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001678 fsi_handler_init(&master->fsia);
1679 ret = fsi_stream_probe(&master->fsia);
1680 if (ret < 0) {
1681 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1682 goto exit_iounmap;
1683 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001684
1685 /* FSI B setting */
1686 master->fsib.base = master->base + 0x40;
1687 master->fsib.master = master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001688 master->fsib.info = &info->port_b;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001689 fsi_handler_init(&master->fsib);
1690 ret = fsi_stream_probe(&master->fsib);
1691 if (ret < 0) {
1692 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1693 goto exit_fsia;
1694 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001695
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001696 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001698
Yong Zhang88e24c32011-09-22 16:59:20 +08001699 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001700 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001701 if (ret) {
1702 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001703 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001704 }
1705
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001706 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001707 if (ret < 0) {
1708 dev_err(&pdev->dev, "cannot snd soc register\n");
1709 goto exit_free_irq;
1710 }
1711
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001712 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1713 ARRAY_SIZE(fsi_soc_dai));
1714 if (ret < 0) {
1715 dev_err(&pdev->dev, "cannot snd dai register\n");
1716 goto exit_snd_soc;
1717 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001718
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001719 return ret;
1720
1721exit_snd_soc:
1722 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001723exit_free_irq:
1724 free_irq(irq, master);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001725exit_fsib:
1726 fsi_stream_remove(&master->fsib);
1727exit_fsia:
1728 fsi_stream_remove(&master->fsia);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001729exit_iounmap:
1730 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001731 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001732exit_kfree:
1733 kfree(master);
1734 master = NULL;
1735exit:
1736 return ret;
1737}
1738
1739static int fsi_remove(struct platform_device *pdev)
1740{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001741 struct fsi_master *master;
1742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001743 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001744
Kuninori Morimotod985f272011-04-08 15:09:25 +09001745 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001746 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001747
Kuninori Morimotod985f272011-04-08 15:09:25 +09001748 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1749 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001750
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001751 fsi_stream_remove(&master->fsia);
1752 fsi_stream_remove(&master->fsib);
1753
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001754 iounmap(master->base);
1755 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001756
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001757 return 0;
1758}
1759
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001760static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001761 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001762 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001763{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001764 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001765 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001766
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001767 fsi_stream_stop(fsi, io);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001768 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001769}
1770
1771static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001772 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001773 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001774{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001775 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001776 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001777
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001778 fsi_hw_startup(fsi, io, dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001779
1780 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001781 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001782
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001783 fsi_stream_start(fsi, io);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001784}
1785
1786static int fsi_suspend(struct device *dev)
1787{
1788 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001789 struct fsi_priv *fsia = &master->fsia;
1790 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001791
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001792 __fsi_suspend(fsia, &fsia->playback, dev);
1793 __fsi_suspend(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001794
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001795 __fsi_suspend(fsib, &fsib->playback, dev);
1796 __fsi_suspend(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001797
1798 return 0;
1799}
1800
1801static int fsi_resume(struct device *dev)
1802{
1803 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001804 struct fsi_priv *fsia = &master->fsia;
1805 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001806
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001807 __fsi_resume(fsia, &fsia->playback, dev);
1808 __fsi_resume(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001809
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001810 __fsi_resume(fsib, &fsib->playback, dev);
1811 __fsi_resume(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001812
1813 return 0;
1814}
1815
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001816static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001817 .suspend = fsi_suspend,
1818 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001819};
1820
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001821static struct fsi_core fsi1_core = {
1822 .ver = 1,
1823
1824 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001825 .int_st = INT_ST,
1826 .iemsk = IEMSK,
1827 .imsk = IMSK,
1828};
1829
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001830static struct fsi_core fsi2_core = {
1831 .ver = 2,
1832
1833 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001834 .int_st = CPU_INT_ST,
1835 .iemsk = CPU_IEMSK,
1836 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001837 .a_mclk = A_MST_CTLR,
1838 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001839};
1840
1841static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001842 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1843 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001844 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001845};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001846MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001847
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001848static struct platform_driver fsi_driver = {
1849 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001851 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001852 },
1853 .probe = fsi_probe,
1854 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001855 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001856};
1857
Axel Lincb5e8732011-11-25 10:15:07 +08001858module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001859
1860MODULE_LICENSE("GPL");
1861MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1862MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001863MODULE_ALIAS("platform:fsi-pcm-audio");