blob: 22037f1c76fb6a82d6adfc6d680fe94c0bc317fb [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 Morimotoab6f6d82012-11-05 18:30:38 -080024#include <sound/pcm_params.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090025#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090026
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090027/* PortA/PortB register */
28#define REG_DO_FMT 0x0000
29#define REG_DOFF_CTL 0x0004
30#define REG_DOFF_ST 0x0008
31#define REG_DI_FMT 0x000C
32#define REG_DIFF_CTL 0x0010
33#define REG_DIFF_ST 0x0014
34#define REG_CKG1 0x0018
35#define REG_CKG2 0x001C
36#define REG_DIDT 0x0020
37#define REG_DODT 0x0024
38#define REG_MUTE_ST 0x0028
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080039#define REG_OUT_DMAC 0x002C
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090040#define REG_OUT_SEL 0x0030
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080041#define REG_IN_DMAC 0x0038
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090042
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090043/* master register */
44#define MST_CLK_RST 0x0210
45#define MST_SOFT_RST 0x0214
46#define MST_FIFO_SZ 0x0218
47
48/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090049#define A_MST_CTLR 0x0180
50#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090051#define CPU_INT_ST 0x01F4
52#define CPU_IEMSK 0x01F8
53#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090054#define INT_ST 0x0200
55#define IEMSK 0x0204
56#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090057
58/* DO_FMT */
59/* DI_FMT */
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080060#define CR_BWS_MASK (0x3 << 20) /* FSI2 */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090061#define CR_BWS_24 (0x0 << 20) /* FSI2 */
62#define CR_BWS_16 (0x1 << 20) /* FSI2 */
63#define CR_BWS_20 (0x2 << 20) /* FSI2 */
64
65#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
66#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
67#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
68
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090069#define CR_MONO (0x0 << 4)
70#define CR_MONO_D (0x1 << 4)
71#define CR_PCM (0x2 << 4)
72#define CR_I2S (0x3 << 4)
73#define CR_TDM (0x4 << 4)
74#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090075
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080076/* OUT_DMAC */
77/* IN_DMAC */
78#define VDMD_MASK (0x3 << 4)
79#define VDMD_FRONT (0x0 << 4) /* Package in front */
80#define VDMD_BACK (0x1 << 4) /* Package in back */
81#define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
82
83#define DMA_ON (0x1 << 0)
84
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090085/* DOFF_CTL */
86/* DIFF_CTL */
87#define IRQ_HALF 0x00100000
88#define FIFO_CLR 0x00000001
89
90/* DOFF_ST */
91#define ERR_OVER 0x00000010
92#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090093#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090094
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090095/* CKG1 */
96#define ACKMD_MASK 0x00007000
97#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090098#define DIMD (1 << 4)
99#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900100
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900101/* A/B MST_CTLR */
102#define BP (1 << 4) /* Fix the signal of Biphase output */
103#define SE (1 << 0) /* Fix the master clock */
104
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900105/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900106#define CRB (1 << 4)
107#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900108
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900109/* IO SHIFT / MACRO */
110#define BI_SHIFT 12
111#define BO_SHIFT 8
112#define AI_SHIFT 4
113#define AO_SHIFT 0
114#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900115
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900116/* SOFT_RST */
117#define PBSR (1 << 12) /* Port B Software Reset */
118#define PASR (1 << 8) /* Port A Software Reset */
119#define IR (1 << 4) /* Interrupt Reset */
120#define FSISR (1 << 0) /* Software Reset */
121
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900122/* OUT_SEL (FSI2) */
123#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
124 /* 1: Biphase and serial */
125
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900126/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900127#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900128
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900129#define FSI_RATES SNDRV_PCM_RATE_8000_96000
130
131#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
132
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800133typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900134
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900135/*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700136 * bus options
137 *
138 * 0x000000BA
139 *
140 * A : sample widtht 16bit setting
141 * B : sample widtht 24bit setting
142 */
143
144#define SHIFT_16DATA 0
145#define SHIFT_24DATA 4
146
147#define PACKAGE_24BITBUS_BACK 0
148#define PACKAGE_24BITBUS_FRONT 1
149#define PACKAGE_16BITBUS_STREAM 2
150
151#define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
152#define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
153
154/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900155 * FSI driver use below type name for variable
156 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900157 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900158 * xxx_pos : position of data
159 * xxx_capa : capacity of data
160 */
161
162/*
163 * period/frame/sample image
164 *
165 * ex) PCM (2ch)
166 *
167 * period pos period pos
168 * [n] [n + 1]
169 * |<-------------------- period--------------------->|
170 * ==|============================================ ... =|==
171 * | |
172 * ||<----- frame ----->|<------ frame ----->| ... |
173 * |+--------------------+--------------------+- ... |
174 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
175 * |+--------------------+--------------------+- ... |
176 * ==|============================================ ... =|==
177 */
178
179/*
180 * FSI FIFO image
181 *
182 * | |
183 * | |
184 * | [ sample ] |
185 * | [ sample ] |
186 * | [ sample ] |
187 * | [ sample ] |
188 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900189 */
190
191/*
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800192 * FSI clock
193 *
194 * FSIxCLK [CPG] (ick) -------> |
195 * |-> FSI_DIV (div)-> FSI2
196 * FSIxCK [external] (xck) ---> |
197 */
198
199/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900200 * struct
201 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900202
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800203struct fsi_stream_handler;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900204struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900205
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800206 /*
207 * these are initialized by fsi_stream_init()
208 */
209 struct snd_pcm_substream *substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900210 int fifo_sample_capa; /* sample capacity of FSI FIFO */
211 int buff_sample_capa; /* sample capacity of ALSA buffer */
212 int buff_sample_pos; /* sample position of ALSA buffer */
213 int period_samples; /* sample number / 1 period */
214 int period_pos; /* current period position */
Kuninori Morimotoc1e6f102012-02-03 00:50:09 -0800215 int sample_width; /* sample width */
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900216 int uerr_num;
217 int oerr_num;
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800218
219 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700220 * bus options
221 */
222 u32 bus_option;
223
224 /*
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800225 * thse are initialized by fsi_handler_init()
226 */
227 struct fsi_stream_handler *handler;
228 struct fsi_priv *priv;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800229
230 /*
231 * these are for DMAEngine
232 */
233 struct dma_chan *chan;
234 struct sh_dmae_slave slave; /* see fsi_handler_init() */
235 struct tasklet_struct tasklet;
236 dma_addr_t dma;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900237};
238
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800239struct fsi_clk {
240 /* see [FSI clock] */
241 struct clk *own;
242 struct clk *xck;
243 struct clk *ick;
244 struct clk *div;
245 int (*set_rate)(struct device *dev,
246 struct fsi_priv *fsi,
247 unsigned long rate);
248
249 unsigned long rate;
250 unsigned int count;
251};
252
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900253struct fsi_priv {
254 void __iomem *base;
255 struct fsi_master *master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800256 struct sh_fsi_port_info *info;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900257
258 struct fsi_stream playback;
259 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900260
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800261 struct fsi_clk clock;
262
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -0700263 u32 fmt;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900264
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900265 int chan_num:16;
266 int clk_master:1;
Kuninori Morimotoab6340c2012-11-16 01:17:18 -0800267 int clk_cpg:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900268 int spdif:1;
Kuninori Morimoto2522acd2012-11-16 01:17:30 -0800269 int enable_stream:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900270
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000271 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900272};
273
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800274struct fsi_stream_handler {
Kuninori Morimoto83344022012-02-03 00:59:02 -0800275 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
276 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700277 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800278 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
279 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800280 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
281 int enable);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800282};
283#define fsi_stream_handler_call(io, func, args...) \
284 (!(io) ? -ENODEV : \
285 !((io)->handler->func) ? 0 : \
286 (io)->handler->func(args))
287
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900288struct fsi_core {
289 int ver;
290
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900291 u32 int_st;
292 u32 iemsk;
293 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900294 u32 a_mclk;
295 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900296};
297
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900298struct fsi_master {
299 void __iomem *base;
300 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900301 struct fsi_priv fsia;
302 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900303 struct fsi_core *core;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900304 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900305};
306
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800307static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
308
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900309/*
310 * basic read write function
311 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900312
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200313static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900314{
315 /* valid data area is 24bit */
316 data &= 0x00ffffff;
317
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100318 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900319}
320
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200321static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900322{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100323 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900324}
325
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200326static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900327{
328 u32 val = __fsi_reg_read(reg);
329
330 val &= ~mask;
331 val |= data & mask;
332
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100333 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900334}
335
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900336#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700337 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900338
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900339#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700340 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900341
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900342#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700343 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900344
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900345#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
346#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
347static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900348{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900349 u32 ret;
350 unsigned long flags;
351
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900352 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200353 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900354 spin_unlock_irqrestore(&master->lock, flags);
355
356 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900357}
358
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900359#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
360#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
361static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900362 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900363{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900364 unsigned long flags;
365
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900366 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200367 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900368 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900369}
370
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900371/*
372 * basic function
373 */
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700374static int fsi_version(struct fsi_master *master)
375{
376 return master->core->ver;
377}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900378
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900379static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900380{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900381 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900382}
383
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900384static int fsi_is_clk_master(struct fsi_priv *fsi)
385{
386 return fsi->clk_master;
387}
388
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900389static int fsi_is_port_a(struct fsi_priv *fsi)
390{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900391 return fsi->master->base == fsi->base;
392}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900393
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900394static int fsi_is_spdif(struct fsi_priv *fsi)
395{
396 return fsi->spdif;
397}
398
Kuninori Morimoto2522acd2012-11-16 01:17:30 -0800399static int fsi_is_enable_stream(struct fsi_priv *fsi)
400{
401 return fsi->enable_stream;
402}
403
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800404static int fsi_is_play(struct snd_pcm_substream *substream)
405{
406 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
407}
408
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900409static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900410{
411 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900412
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000413 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900414}
415
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900416static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900417{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900419
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000420 if (dai->id == 0)
421 return &master->fsia;
422 else
423 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900424}
425
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900426static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
427{
428 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
429}
430
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800431static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900432{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800433 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900434 return NULL;
435
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800436 return fsi->info->set_rate;
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900437}
438
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900439static u32 fsi_get_info_flags(struct fsi_priv *fsi)
440{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800441 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900442 return 0;
443
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800444 return fsi->info->flags;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900445}
446
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800447static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900448{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800449 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900450 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900451 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900452
453 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900454 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900455 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900456 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900457
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900458 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900459}
460
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900461static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
462{
463 return frames * fsi->chan_num;
464}
465
466static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
467{
468 return samples / fsi->chan_num;
469}
470
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800471static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
472 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900473{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800474 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900475 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900476 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900477
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900478 status = is_play ?
479 fsi_reg_read(fsi, DOFF_ST) :
480 fsi_reg_read(fsi, DIFF_ST);
481
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900482 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900483
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900484 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900485}
486
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900487static void fsi_count_fifo_err(struct fsi_priv *fsi)
488{
489 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
490 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
491
492 if (ostatus & ERR_OVER)
493 fsi->playback.oerr_num++;
494
495 if (ostatus & ERR_UNDER)
496 fsi->playback.uerr_num++;
497
498 if (istatus & ERR_OVER)
499 fsi->capture.oerr_num++;
500
501 if (istatus & ERR_UNDER)
502 fsi->capture.uerr_num++;
503
504 fsi_reg_write(fsi, DOFF_ST, 0);
505 fsi_reg_write(fsi, DIFF_ST, 0);
506}
507
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900508/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800509 * fsi_stream_xx() function
510 */
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800511static inline int fsi_stream_is_play(struct fsi_priv *fsi,
512 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800513{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800514 return &fsi->playback == io;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800515}
516
517static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800518 struct snd_pcm_substream *substream)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800519{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800520 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800521}
522
523static int fsi_stream_is_working(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800524 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800525{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800526 struct fsi_master *master = fsi_get_master(fsi);
527 unsigned long flags;
528 int ret;
529
530 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800531 ret = !!(io->substream && io->substream->runtime);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800532 spin_unlock_irqrestore(&master->lock, flags);
533
534 return ret;
535}
536
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800537static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
538{
539 return io->priv;
540}
541
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800542static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800543 struct fsi_stream *io,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800544 struct snd_pcm_substream *substream)
545{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800546 struct snd_pcm_runtime *runtime = substream->runtime;
547 struct fsi_master *master = fsi_get_master(fsi);
548 unsigned long flags;
549
550 spin_lock_irqsave(&master->lock, flags);
551 io->substream = substream;
552 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
553 io->buff_sample_pos = 0;
554 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
555 io->period_pos = 0;
556 io->sample_width = samples_to_bytes(runtime, 1);
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700557 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800558 io->oerr_num = -1; /* ignore 1st err */
559 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimoto83344022012-02-03 00:59:02 -0800560 fsi_stream_handler_call(io, init, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800561 spin_unlock_irqrestore(&master->lock, flags);
562}
563
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800564static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800565{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800566 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
567 struct fsi_master *master = fsi_get_master(fsi);
568 unsigned long flags;
569
570 spin_lock_irqsave(&master->lock, flags);
571
572 if (io->oerr_num > 0)
573 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
574
575 if (io->uerr_num > 0)
576 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
577
Kuninori Morimoto83344022012-02-03 00:59:02 -0800578 fsi_stream_handler_call(io, quit, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800579 io->substream = NULL;
580 io->buff_sample_capa = 0;
581 io->buff_sample_pos = 0;
582 io->period_samples = 0;
583 io->period_pos = 0;
584 io->sample_width = 0;
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700585 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800586 io->oerr_num = 0;
587 io->uerr_num = 0;
588 spin_unlock_irqrestore(&master->lock, flags);
589}
590
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800591static int fsi_stream_transfer(struct fsi_stream *io)
592{
593 struct fsi_priv *fsi = fsi_stream_to_priv(io);
594 if (!fsi)
595 return -EIO;
596
597 return fsi_stream_handler_call(io, transfer, fsi, io);
598}
599
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800600#define fsi_stream_start(fsi, io)\
601 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
602
603#define fsi_stream_stop(fsi, io)\
604 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
605
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700606static int fsi_stream_probe(struct fsi_priv *fsi, struct device *dev)
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800607{
608 struct fsi_stream *io;
609 int ret1, ret2;
610
611 io = &fsi->playback;
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700612 ret1 = fsi_stream_handler_call(io, probe, fsi, io, dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800613
614 io = &fsi->capture;
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700615 ret2 = fsi_stream_handler_call(io, probe, fsi, io, dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800616
617 if (ret1 < 0)
618 return ret1;
619 if (ret2 < 0)
620 return ret2;
621
622 return 0;
623}
624
625static int fsi_stream_remove(struct fsi_priv *fsi)
626{
627 struct fsi_stream *io;
628 int ret1, ret2;
629
630 io = &fsi->playback;
631 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
632
633 io = &fsi->capture;
634 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
635
636 if (ret1 < 0)
637 return ret1;
638 if (ret2 < 0)
639 return ret2;
640
641 return 0;
642}
643
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800644/*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700645 * format/bus/dma setting
646 */
647static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
648 u32 bus, struct device *dev)
649{
650 struct fsi_master *master = fsi_get_master(fsi);
651 int is_play = fsi_stream_is_play(fsi, io);
652 u32 fmt = fsi->fmt;
653
654 if (fsi_version(master) >= 2) {
655 u32 dma = 0;
656
657 /*
658 * FSI2 needs DMA/Bus setting
659 */
660 switch (bus) {
661 case PACKAGE_24BITBUS_FRONT:
662 fmt |= CR_BWS_24;
663 dma |= VDMD_FRONT;
664 dev_dbg(dev, "24bit bus / package in front\n");
665 break;
666 case PACKAGE_16BITBUS_STREAM:
667 fmt |= CR_BWS_16;
668 dma |= VDMD_STREAM;
669 dev_dbg(dev, "16bit bus / stream mode\n");
670 break;
671 case PACKAGE_24BITBUS_BACK:
672 default:
673 fmt |= CR_BWS_24;
674 dma |= VDMD_BACK;
675 dev_dbg(dev, "24bit bus / package in back\n");
676 break;
677 }
678
679 if (is_play)
680 fsi_reg_write(fsi, OUT_DMAC, dma);
681 else
682 fsi_reg_write(fsi, IN_DMAC, dma);
683 }
684
685 if (is_play)
686 fsi_reg_write(fsi, DO_FMT, fmt);
687 else
688 fsi_reg_write(fsi, DI_FMT, fmt);
689}
690
691/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900692 * irq function
693 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900694
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800695static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900696{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800697 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900698 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900699
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900700 fsi_core_mask_set(master, imsk, data, data);
701 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900702}
703
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800704static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900705{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800706 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900707 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900708
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900709 fsi_core_mask_set(master, imsk, data, 0);
710 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900711}
712
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900713static u32 fsi_irq_get_status(struct fsi_master *master)
714{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900715 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900716}
717
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900718static void fsi_irq_clear_status(struct fsi_priv *fsi)
719{
720 u32 data = 0;
721 struct fsi_master *master = fsi_get_master(fsi);
722
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800723 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
724 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900725
726 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900727 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900728}
729
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900730/*
731 * SPDIF master clock function
732 *
733 * These functions are used later FSI2
734 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900735static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
736{
737 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900738 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900739
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900740 mask = BP | SE;
741 val = enable ? mask : 0;
742
743 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900744 fsi_core_mask_set(master, a_mclk, mask, val) :
745 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900746}
747
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900748/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900749 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900750 */
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800751static int fsi_clk_init(struct device *dev,
752 struct fsi_priv *fsi,
753 int xck,
754 int ick,
755 int div,
756 int (*set_rate)(struct device *dev,
757 struct fsi_priv *fsi,
758 unsigned long rate))
759{
760 struct fsi_clk *clock = &fsi->clock;
761 int is_porta = fsi_is_port_a(fsi);
762
763 clock->xck = NULL;
764 clock->ick = NULL;
765 clock->div = NULL;
766 clock->rate = 0;
767 clock->count = 0;
768 clock->set_rate = set_rate;
769
770 clock->own = devm_clk_get(dev, NULL);
771 if (IS_ERR(clock->own))
772 return -EINVAL;
773
774 /* external clock */
775 if (xck) {
776 clock->xck = devm_clk_get(dev, is_porta ? "xcka" : "xckb");
777 if (IS_ERR(clock->xck)) {
778 dev_err(dev, "can't get xck clock\n");
779 return -EINVAL;
780 }
781 if (clock->xck == clock->own) {
782 dev_err(dev, "cpu doesn't support xck clock\n");
783 return -EINVAL;
784 }
785 }
786
787 /* FSIACLK/FSIBCLK */
788 if (ick) {
789 clock->ick = devm_clk_get(dev, is_porta ? "icka" : "ickb");
790 if (IS_ERR(clock->ick)) {
791 dev_err(dev, "can't get ick clock\n");
792 return -EINVAL;
793 }
794 if (clock->ick == clock->own) {
795 dev_err(dev, "cpu doesn't support ick clock\n");
796 return -EINVAL;
797 }
798 }
799
800 /* FSI-DIV */
801 if (div) {
802 clock->div = devm_clk_get(dev, is_porta ? "diva" : "divb");
803 if (IS_ERR(clock->div)) {
804 dev_err(dev, "can't get div clock\n");
805 return -EINVAL;
806 }
807 if (clock->div == clock->own) {
808 dev_err(dev, "cpu doens't support div clock\n");
809 return -EINVAL;
810 }
811 }
812
813 return 0;
814}
815
816#define fsi_clk_invalid(fsi) fsi_clk_valid(fsi, 0)
817static void fsi_clk_valid(struct fsi_priv *fsi, unsigned long rate)
818{
819 fsi->clock.rate = rate;
820}
821
822static int fsi_clk_is_valid(struct fsi_priv *fsi)
823{
824 return fsi->clock.set_rate &&
825 fsi->clock.rate;
826}
827
828static int fsi_clk_enable(struct device *dev,
829 struct fsi_priv *fsi,
830 unsigned long rate)
831{
832 struct fsi_clk *clock = &fsi->clock;
833 int ret = -EINVAL;
834
835 if (!fsi_clk_is_valid(fsi))
836 return ret;
837
838 if (0 == clock->count) {
839 ret = clock->set_rate(dev, fsi, rate);
840 if (ret < 0) {
841 fsi_clk_invalid(fsi);
842 return ret;
843 }
844
845 if (clock->xck)
846 clk_enable(clock->xck);
847 if (clock->ick)
848 clk_enable(clock->ick);
849 if (clock->div)
850 clk_enable(clock->div);
851
852 clock->count++;
853 }
854
855 return ret;
856}
857
858static int fsi_clk_disable(struct device *dev,
859 struct fsi_priv *fsi)
860{
861 struct fsi_clk *clock = &fsi->clock;
862
863 if (!fsi_clk_is_valid(fsi))
864 return -EINVAL;
865
866 if (1 == clock->count--) {
867 if (clock->xck)
868 clk_disable(clock->xck);
869 if (clock->ick)
870 clk_disable(clock->ick);
871 if (clock->div)
872 clk_disable(clock->div);
873 }
874
875 return 0;
876}
877
878static int fsi_clk_set_ackbpf(struct device *dev,
879 struct fsi_priv *fsi,
880 int ackmd, int bpfmd)
881{
882 u32 data = 0;
883
884 /* check ackmd/bpfmd relationship */
885 if (bpfmd > ackmd) {
886 dev_err(dev, "unsupported rate (%d/%d)\n", ackmd, bpfmd);
887 return -EINVAL;
888 }
889
890 /* ACKMD */
891 switch (ackmd) {
892 case 512:
893 data |= (0x0 << 12);
894 break;
895 case 256:
896 data |= (0x1 << 12);
897 break;
898 case 128:
899 data |= (0x2 << 12);
900 break;
901 case 64:
902 data |= (0x3 << 12);
903 break;
904 case 32:
905 data |= (0x4 << 12);
906 break;
907 default:
908 dev_err(dev, "unsupported ackmd (%d)\n", ackmd);
909 return -EINVAL;
910 }
911
912 /* BPFMD */
913 switch (bpfmd) {
914 case 32:
915 data |= (0x0 << 8);
916 break;
917 case 64:
918 data |= (0x1 << 8);
919 break;
920 case 128:
921 data |= (0x2 << 8);
922 break;
923 case 256:
924 data |= (0x3 << 8);
925 break;
926 case 512:
927 data |= (0x4 << 8);
928 break;
929 case 16:
930 data |= (0x7 << 8);
931 break;
932 default:
933 dev_err(dev, "unsupported bpfmd (%d)\n", bpfmd);
934 return -EINVAL;
935 }
936
937 dev_dbg(dev, "ACKMD/BPFMD = %d/%d\n", ackmd, bpfmd);
938
939 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
940 udelay(10);
941
942 return 0;
943}
944
945static int fsi_clk_set_rate_external(struct device *dev,
946 struct fsi_priv *fsi,
947 unsigned long rate)
948{
949 struct clk *xck = fsi->clock.xck;
950 struct clk *ick = fsi->clock.ick;
951 unsigned long xrate;
952 int ackmd, bpfmd;
953 int ret = 0;
954
955 /* check clock rate */
956 xrate = clk_get_rate(xck);
957 if (xrate % rate) {
958 dev_err(dev, "unsupported clock rate\n");
959 return -EINVAL;
960 }
961
962 clk_set_parent(ick, xck);
963 clk_set_rate(ick, xrate);
964
965 bpfmd = fsi->chan_num * 32;
966 ackmd = xrate / rate;
967
968 dev_dbg(dev, "external/rate = %ld/%ld\n", xrate, rate);
969
970 ret = fsi_clk_set_ackbpf(dev, fsi, ackmd, bpfmd);
971 if (ret < 0)
972 dev_err(dev, "%s failed", __func__);
973
974 return ret;
975}
976
977static int fsi_clk_set_rate_cpg(struct device *dev,
978 struct fsi_priv *fsi,
979 unsigned long rate)
980{
981 struct clk *ick = fsi->clock.ick;
982 struct clk *div = fsi->clock.div;
983 unsigned long target = 0; /* 12288000 or 11289600 */
984 unsigned long actual, cout;
985 unsigned long diff, min;
986 unsigned long best_cout, best_act;
987 int adj;
988 int ackmd, bpfmd;
989 int ret = -EINVAL;
990
991 if (!(12288000 % rate))
992 target = 12288000;
993 if (!(11289600 % rate))
994 target = 11289600;
995 if (!target) {
996 dev_err(dev, "unsupported rate\n");
997 return ret;
998 }
999
1000 bpfmd = fsi->chan_num * 32;
1001 ackmd = target / rate;
1002 ret = fsi_clk_set_ackbpf(dev, fsi, ackmd, bpfmd);
1003 if (ret < 0) {
1004 dev_err(dev, "%s failed", __func__);
1005 return ret;
1006 }
1007
1008 /*
1009 * The clock flow is
1010 *
1011 * [CPG] = cout => [FSI_DIV] = audio => [FSI] => [codec]
1012 *
1013 * But, it needs to find best match of CPG and FSI_DIV
1014 * combination, since it is difficult to generate correct
1015 * frequency of audio clock from ick clock only.
1016 * Because ick is created from its parent clock.
1017 *
1018 * target = rate x [512/256/128/64]fs
1019 * cout = round(target x adjustment)
1020 * actual = cout / adjustment (by FSI-DIV) ~= target
1021 * audio = actual
1022 */
1023 min = ~0;
1024 best_cout = 0;
1025 best_act = 0;
1026 for (adj = 1; adj < 0xffff; adj++) {
1027
1028 cout = target * adj;
1029 if (cout > 100000000) /* max clock = 100MHz */
1030 break;
1031
1032 /* cout/actual audio clock */
1033 cout = clk_round_rate(ick, cout);
1034 actual = cout / adj;
1035
1036 /* find best frequency */
1037 diff = abs(actual - target);
1038 if (diff < min) {
1039 min = diff;
1040 best_cout = cout;
1041 best_act = actual;
1042 }
1043 }
1044
1045 ret = clk_set_rate(ick, best_cout);
1046 if (ret < 0) {
1047 dev_err(dev, "ick clock failed\n");
1048 return -EIO;
1049 }
1050
1051 ret = clk_set_rate(div, clk_round_rate(div, best_act));
1052 if (ret < 0) {
1053 dev_err(dev, "div clock failed\n");
1054 return -EIO;
1055 }
1056
1057 dev_dbg(dev, "ick/div = %ld/%ld\n",
1058 clk_get_rate(ick), clk_get_rate(div));
1059
1060 return ret;
1061}
1062
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001063static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
1064 long rate, int enable)
1065{
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001066 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001067 int ret;
1068
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001069 /*
1070 * CAUTION
1071 *
1072 * set_rate will be deleted
1073 */
1074 if (!set_rate) {
1075 if (enable)
1076 return fsi_clk_enable(dev, fsi, rate);
1077 else
1078 return fsi_clk_disable(dev, fsi);
1079 }
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001080
1081 ret = set_rate(dev, rate, enable);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001082 if (ret < 0) /* error */
1083 return ret;
1084
1085 if (!enable)
1086 return 0;
1087
1088 if (ret > 0) {
1089 u32 data = 0;
1090
1091 switch (ret & SH_FSI_ACKMD_MASK) {
1092 default:
1093 /* FALL THROUGH */
1094 case SH_FSI_ACKMD_512:
1095 data |= (0x0 << 12);
1096 break;
1097 case SH_FSI_ACKMD_256:
1098 data |= (0x1 << 12);
1099 break;
1100 case SH_FSI_ACKMD_128:
1101 data |= (0x2 << 12);
1102 break;
1103 case SH_FSI_ACKMD_64:
1104 data |= (0x3 << 12);
1105 break;
1106 case SH_FSI_ACKMD_32:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001107 data |= (0x4 << 12);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001108 break;
1109 }
1110
1111 switch (ret & SH_FSI_BPFMD_MASK) {
1112 default:
1113 /* FALL THROUGH */
1114 case SH_FSI_BPFMD_32:
1115 data |= (0x0 << 8);
1116 break;
1117 case SH_FSI_BPFMD_64:
1118 data |= (0x1 << 8);
1119 break;
1120 case SH_FSI_BPFMD_128:
1121 data |= (0x2 << 8);
1122 break;
1123 case SH_FSI_BPFMD_256:
1124 data |= (0x3 << 8);
1125 break;
1126 case SH_FSI_BPFMD_512:
1127 data |= (0x4 << 8);
1128 break;
1129 case SH_FSI_BPFMD_16:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001130 data |= (0x7 << 8);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001131 break;
1132 }
1133
1134 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
1135 udelay(10);
1136 ret = 0;
1137 }
1138
1139 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001140}
1141
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001142/*
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001143 * pio data transfer handler
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001144 */
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001145static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
1146{
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001147 int i;
1148
Kuninori Morimoto2522acd2012-11-16 01:17:30 -08001149 if (fsi_is_enable_stream(fsi)) {
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001150 /*
1151 * stream mode
1152 * see
1153 * fsi_pio_push_init()
1154 */
1155 u32 *buf = (u32 *)_buf;
1156
1157 for (i = 0; i < samples / 2; i++)
1158 fsi_reg_write(fsi, DODT, buf[i]);
1159 } else {
1160 /* normal mode */
1161 u16 *buf = (u16 *)_buf;
1162
1163 for (i = 0; i < samples; i++)
1164 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
1165 }
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001166}
1167
1168static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
1169{
1170 u16 *buf = (u16 *)_buf;
1171 int i;
1172
1173 for (i = 0; i < samples; i++)
1174 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
1175}
1176
1177static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
1178{
1179 u32 *buf = (u32 *)_buf;
1180 int i;
1181
1182 for (i = 0; i < samples; i++)
1183 fsi_reg_write(fsi, DODT, *(buf + i));
1184}
1185
1186static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
1187{
1188 u32 *buf = (u32 *)_buf;
1189 int i;
1190
1191 for (i = 0; i < samples; i++)
1192 *(buf + i) = fsi_reg_read(fsi, DIDT);
1193}
1194
1195static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
1196{
1197 struct snd_pcm_runtime *runtime = io->substream->runtime;
1198
1199 return runtime->dma_area +
1200 samples_to_bytes(runtime, io->buff_sample_pos);
1201}
1202
1203static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001204 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
1205 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
1206 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001207{
1208 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001209 struct snd_pcm_substream *substream;
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001210 u8 *buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +09001211 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001212
Kuninori Morimoto97df8182012-02-03 00:57:40 -08001213 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001214 return -EINVAL;
1215
Kuninori Morimoto1c418d12009-12-28 14:09:05 +09001216 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001217 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +09001218 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001219
1220 /* FSI FIFO has limit.
1221 * So, this driver can not send periods data at a time
1222 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001223 if (io->buff_sample_pos >=
1224 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001225
Kuninori Morimoto1c418d12009-12-28 14:09:05 +09001226 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001227 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001228
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001229 if (0 == io->period_pos)
1230 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001231 }
1232
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001233 buf = fsi_pio_get_area(fsi, io);
1234
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001235 switch (io->sample_width) {
1236 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001237 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001238 break;
1239 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001240 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001241 break;
1242 default:
1243 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001244 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001245
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001246 /* update buff_sample_pos */
1247 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +09001248
Kuninori Morimoto1c418d12009-12-28 14:09:05 +09001249 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001250 snd_pcm_period_elapsed(substream);
1251
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +09001252 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001253}
1254
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001255static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001256{
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001257 int sample_residues; /* samples in FSI fifo */
1258 int sample_space; /* ALSA free samples space */
1259 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001260
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -08001261 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001262 sample_space = io->buff_sample_capa - io->buff_sample_pos;
1263
1264 samples = min(sample_residues, sample_space);
1265
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001266 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -08001267 fsi_pio_pop16,
1268 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001269 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +09001270}
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001271
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001272static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotod8b33532010-09-17 13:49:05 +09001273{
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001274 int sample_residues; /* ALSA residue samples */
1275 int sample_space; /* FSI fifo free samples space */
1276 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001277
1278 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
1279 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -08001280 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001281
1282 samples = min(sample_residues, sample_space);
1283
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001284 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -08001285 fsi_pio_push16,
1286 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001287 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001288}
1289
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001290static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1291 int enable)
1292{
1293 struct fsi_master *master = fsi_get_master(fsi);
1294 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
1295
1296 if (enable)
1297 fsi_irq_enable(fsi, io);
1298 else
1299 fsi_irq_disable(fsi, io);
1300
1301 if (fsi_is_clk_master(fsi))
1302 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
1303}
1304
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001305static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
1306{
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001307 /*
1308 * we can use 16bit stream mode
1309 * when "playback" and "16bit data"
1310 * and platform allows "stream mode"
1311 * see
1312 * fsi_pio_push16()
1313 */
Kuninori Morimoto2522acd2012-11-16 01:17:30 -08001314 if (fsi_is_enable_stream(fsi))
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001315 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1316 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1317 else
1318 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1319 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
1320 return 0;
1321}
1322
1323static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
1324{
1325 /*
1326 * always 24bit bus, package back when "capture"
1327 */
1328 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1329 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
1330 return 0;
1331}
1332
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001333static struct fsi_stream_handler fsi_pio_push_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001334 .init = fsi_pio_push_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001335 .transfer = fsi_pio_push,
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001336 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001337};
1338
1339static struct fsi_stream_handler fsi_pio_pop_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001340 .init = fsi_pio_pop_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001341 .transfer = fsi_pio_pop,
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001342 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001343};
1344
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001345static irqreturn_t fsi_interrupt(int irq, void *data)
1346{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001347 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001348 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001349
1350 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +09001351 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1352 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001353
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001354 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001355 fsi_stream_transfer(&master->fsia.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001356 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001357 fsi_stream_transfer(&master->fsib.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001358 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001359 fsi_stream_transfer(&master->fsia.capture);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001360 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001361 fsi_stream_transfer(&master->fsib.capture);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +09001362
1363 fsi_count_fifo_err(&master->fsia);
1364 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001365
Kuninori Morimoto48d78e52010-12-03 17:37:31 +09001366 fsi_irq_clear_status(&master->fsia);
1367 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001368
1369 return IRQ_HANDLED;
1370}
1371
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001372/*
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001373 * dma data transfer handler
1374 */
1375static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1376{
1377 struct snd_pcm_runtime *runtime = io->substream->runtime;
1378 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1379 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1380 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1381
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001382 /*
1383 * 24bit data : 24bit bus / package in back
1384 * 16bit data : 16bit bus / stream mode
1385 */
1386 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1387 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1388
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001389 io->dma = dma_map_single(dai->dev, runtime->dma_area,
1390 snd_pcm_lib_buffer_bytes(io->substream), dir);
1391 return 0;
1392}
1393
1394static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
1395{
1396 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1397 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1398 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1399
1400 dma_unmap_single(dai->dev, io->dma,
1401 snd_pcm_lib_buffer_bytes(io->substream), dir);
1402 return 0;
1403}
1404
Kuninori Morimoto4a1b09b2012-05-28 23:27:49 -07001405static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
1406{
1407 struct snd_pcm_runtime *runtime = io->substream->runtime;
1408
1409 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
1410}
1411
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001412static void fsi_dma_complete(void *data)
1413{
1414 struct fsi_stream *io = (struct fsi_stream *)data;
1415 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1416 struct snd_pcm_runtime *runtime = io->substream->runtime;
1417 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1418 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1419 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1420
Kuninori Morimoto4a1b09b2012-05-28 23:27:49 -07001421 dma_sync_single_for_cpu(dai->dev, fsi_dma_get_area(io),
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001422 samples_to_bytes(runtime, io->period_samples), dir);
1423
1424 io->buff_sample_pos += io->period_samples;
1425 io->period_pos++;
1426
1427 if (io->period_pos >= runtime->periods) {
1428 io->period_pos = 0;
1429 io->buff_sample_pos = 0;
1430 }
1431
1432 fsi_count_fifo_err(fsi);
1433 fsi_stream_transfer(io);
1434
1435 snd_pcm_period_elapsed(io->substream);
1436}
1437
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001438static void fsi_dma_do_tasklet(unsigned long data)
1439{
1440 struct fsi_stream *io = (struct fsi_stream *)data;
1441 struct fsi_priv *fsi = fsi_stream_to_priv(io);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001442 struct snd_soc_dai *dai;
1443 struct dma_async_tx_descriptor *desc;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001444 struct snd_pcm_runtime *runtime;
1445 enum dma_data_direction dir;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001446 int is_play = fsi_stream_is_play(fsi, io);
1447 int len;
1448 dma_addr_t buf;
1449
1450 if (!fsi_stream_is_working(fsi, io))
1451 return;
1452
1453 dai = fsi_get_dai(io->substream);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001454 runtime = io->substream->runtime;
1455 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1456 len = samples_to_bytes(runtime, io->period_samples);
1457 buf = fsi_dma_get_area(io);
1458
Kuninori Morimoto4a1b09b2012-05-28 23:27:49 -07001459 dma_sync_single_for_device(dai->dev, buf, len, dir);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001460
Kuninori Morimoto5514efd2012-05-28 23:29:36 -07001461 desc = dmaengine_prep_slave_single(io->chan, buf, len, dir,
1462 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001463 if (!desc) {
Paul Mundtcdf27f32012-04-17 19:13:04 -07001464 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001465 return;
1466 }
1467
1468 desc->callback = fsi_dma_complete;
1469 desc->callback_param = io;
1470
Kuninori Morimoto5514efd2012-05-28 23:29:36 -07001471 if (dmaengine_submit(desc) < 0) {
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001472 dev_err(dai->dev, "tx_submit() fail\n");
1473 return;
1474 }
1475
Kuninori Morimoto5514efd2012-05-28 23:29:36 -07001476 dma_async_issue_pending(io->chan);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001477
1478 /*
1479 * FIXME
1480 *
1481 * In DMAEngine case, codec and FSI cannot be started simultaneously
1482 * since FSI is using tasklet.
1483 * Therefore, in capture case, probably FSI FIFO will have got
1484 * overflow error in this point.
1485 * in that case, DMA cannot start transfer until error was cleared.
1486 */
1487 if (!is_play) {
1488 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1489 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1490 fsi_reg_write(fsi, DIFF_ST, 0);
1491 }
1492 }
1493}
1494
1495static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1496{
1497 struct sh_dmae_slave *slave = param;
1498
1499 chan->private = slave;
1500
1501 return true;
1502}
1503
1504static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1505{
1506 tasklet_schedule(&io->tasklet);
1507
1508 return 0;
1509}
1510
1511static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1512 int start)
1513{
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001514 struct fsi_master *master = fsi_get_master(fsi);
1515 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001516 u32 enable = start ? DMA_ON : 0;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001517
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001518 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001519
Kuninori Morimotofbe42f62012-05-28 23:28:22 -07001520 dmaengine_terminate_all(io->chan);
1521
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001522 if (fsi_is_clk_master(fsi))
1523 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001524}
1525
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001526static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev)
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001527{
1528 dma_cap_mask_t mask;
1529
1530 dma_cap_zero(mask);
1531 dma_cap_set(DMA_SLAVE, mask);
1532
1533 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001534 if (!io->chan) {
1535
1536 /* switch to PIO handler */
1537 if (fsi_stream_is_play(fsi, io))
1538 fsi->playback.handler = &fsi_pio_push_handler;
1539 else
1540 fsi->capture.handler = &fsi_pio_pop_handler;
1541
1542 dev_info(dev, "switch handler (dma => pio)\n");
1543
1544 /* probe again */
1545 return fsi_stream_probe(fsi, dev);
1546 }
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001547
1548 tasklet_init(&io->tasklet, fsi_dma_do_tasklet, (unsigned long)io);
1549
1550 return 0;
1551}
1552
1553static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1554{
1555 tasklet_kill(&io->tasklet);
1556
1557 fsi_stream_stop(fsi, io);
1558
1559 if (io->chan)
1560 dma_release_channel(io->chan);
1561
1562 io->chan = NULL;
1563 return 0;
1564}
1565
1566static struct fsi_stream_handler fsi_dma_push_handler = {
1567 .init = fsi_dma_init,
1568 .quit = fsi_dma_quit,
1569 .probe = fsi_dma_probe,
1570 .transfer = fsi_dma_transfer,
1571 .remove = fsi_dma_remove,
1572 .start_stop = fsi_dma_push_start_stop,
1573};
1574
1575/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001576 * dai ops
1577 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001578static void fsi_fifo_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001579 struct fsi_stream *io,
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001580 struct device *dev)
1581{
1582 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001583 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001584 u32 shift, i;
1585 int frame_capa;
1586
1587 /* get on-chip RAM capacity */
1588 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001589 shift >>= fsi_get_port_shift(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001590 shift &= FIFO_SZ_MASK;
1591 frame_capa = 256 << shift;
1592 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1593
1594 /*
1595 * The maximum number of sample data varies depending
1596 * on the number of channels selected for the format.
1597 *
1598 * FIFOs are used in 4-channel units in 3-channel mode
1599 * and in 8-channel units in 5- to 7-channel mode
1600 * meaning that more FIFOs than the required size of DPRAM
1601 * are used.
1602 *
1603 * ex) if 256 words of DP-RAM is connected
1604 * 1 channel: 256 (256 x 1 = 256)
1605 * 2 channels: 128 (128 x 2 = 256)
1606 * 3 channels: 64 ( 64 x 3 = 192)
1607 * 4 channels: 64 ( 64 x 4 = 256)
1608 * 5 channels: 32 ( 32 x 5 = 160)
1609 * 6 channels: 32 ( 32 x 6 = 192)
1610 * 7 channels: 32 ( 32 x 7 = 224)
1611 * 8 channels: 32 ( 32 x 8 = 256)
1612 */
1613 for (i = 1; i < fsi->chan_num; i <<= 1)
1614 frame_capa >>= 1;
1615 dev_dbg(dev, "%d channel %d store\n",
1616 fsi->chan_num, frame_capa);
1617
1618 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1619
1620 /*
1621 * set interrupt generation factor
1622 * clear FIFO
1623 */
1624 if (is_play) {
1625 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1626 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1627 } else {
1628 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1629 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1630 }
1631}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001632
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001633static int fsi_hw_startup(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001634 struct fsi_stream *io,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001635 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001636{
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001637 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001638 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001639
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001640 /* clock setting */
1641 if (fsi_is_clk_master(fsi))
1642 data = DIMD | DOMD;
1643
1644 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001645
1646 /* clock inversion (CKG2) */
1647 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +09001648 if (SH_FSI_LRM_INV & flags)
1649 data |= 1 << 12;
1650 if (SH_FSI_BRM_INV & flags)
1651 data |= 1 << 8;
1652 if (SH_FSI_LRS_INV & flags)
1653 data |= 1 << 4;
1654 if (SH_FSI_BRS_INV & flags)
1655 data |= 1 << 0;
1656
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001657 fsi_reg_write(fsi, CKG2, data);
1658
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001659 /* spdif ? */
1660 if (fsi_is_spdif(fsi)) {
1661 fsi_spdif_clk_ctrl(fsi, 1);
1662 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1663 }
1664
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001665 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001666 * get bus settings
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001667 */
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001668 data = 0;
1669 switch (io->sample_width) {
1670 case 2:
1671 data = BUSOP_GET(16, io->bus_option);
1672 break;
1673 case 4:
1674 data = BUSOP_GET(24, io->bus_option);
1675 break;
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001676 }
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001677 fsi_format_bus_setup(fsi, io, data, dev);
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001678
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001679 /* irq clear */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001680 fsi_irq_disable(fsi, io);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001681 fsi_irq_clear_status(fsi);
1682
1683 /* fifo init */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001684 fsi_fifo_init(fsi, io, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001685
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001686 /* start master clock */
1687 if (fsi_is_clk_master(fsi))
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001688 return fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001689
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +09001690 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001691}
1692
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001693static int fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001694 struct device *dev)
1695{
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001696 /* stop master clock */
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001697 if (fsi_is_clk_master(fsi))
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001698 return fsi_set_master_clk(dev, fsi, fsi->rate, 0);
1699
1700 return 0;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001701}
1702
1703static int fsi_dai_startup(struct snd_pcm_substream *substream,
1704 struct snd_soc_dai *dai)
1705{
1706 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001707
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001708 fsi_clk_invalid(fsi);
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001709 fsi->rate = 0;
1710
1711 return 0;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001712}
1713
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001714static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1715 struct snd_soc_dai *dai)
1716{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001717 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001718
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001719 fsi_clk_invalid(fsi);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001720 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001721}
1722
1723static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1724 struct snd_soc_dai *dai)
1725{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001726 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001727 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001728 int ret = 0;
1729
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001730 switch (cmd) {
1731 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001732 fsi_stream_init(fsi, io, substream);
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001733 if (!ret)
1734 ret = fsi_hw_startup(fsi, io, dai->dev);
1735 if (!ret)
1736 ret = fsi_stream_transfer(io);
1737 if (!ret)
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001738 fsi_stream_start(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001739 break;
1740 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001741 if (!ret)
1742 ret = fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001743 fsi_stream_stop(fsi, io);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001744 fsi_stream_quit(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001745 break;
1746 }
1747
1748 return ret;
1749}
1750
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001751static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1752{
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001753 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1754 case SND_SOC_DAIFMT_I2S:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001755 fsi->fmt = CR_I2S;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001756 fsi->chan_num = 2;
1757 break;
1758 case SND_SOC_DAIFMT_LEFT_J:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001759 fsi->fmt = CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001760 fsi->chan_num = 2;
1761 break;
1762 default:
1763 return -EINVAL;
1764 }
1765
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001766 return 0;
1767}
1768
1769static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1770{
1771 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001772
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001773 if (fsi_version(master) < 2)
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001774 return -EINVAL;
1775
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001776 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001777 fsi->chan_num = 2;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001778
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001779 return 0;
1780}
1781
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001782static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1783{
1784 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001785 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001786 int ret;
1787
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001788 /* set master/slave audio interface */
1789 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1790 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001791 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001792 break;
1793 case SND_SOC_DAIFMT_CBS_CFS:
1794 break;
1795 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001796 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001797 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001798
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001799 if (fsi_is_clk_master(fsi)) {
1800 /*
1801 * CAUTION
1802 *
1803 * set_rate will be deleted
1804 */
1805 if (set_rate)
1806 dev_warn(dai->dev, "set_rate will be removed soon\n");
1807
Kuninori Morimotoab6340c2012-11-16 01:17:18 -08001808 if (fsi->clk_cpg)
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001809 fsi_clk_init(dai->dev, fsi, 0, 1, 1,
1810 fsi_clk_set_rate_cpg);
Kuninori Morimotoab6340c2012-11-16 01:17:18 -08001811 else
1812 fsi_clk_init(dai->dev, fsi, 1, 1, 0,
1813 fsi_clk_set_rate_external);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001814 }
1815
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001816 /* set format */
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001817 if (fsi_is_spdif(fsi))
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001818 ret = fsi_set_fmt_spdif(fsi);
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001819 else
1820 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001821
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001822 return ret;
1823}
1824
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001825static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1826 struct snd_pcm_hw_params *params,
1827 struct snd_soc_dai *dai)
1828{
1829 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001830
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001831 if (fsi_is_clk_master(fsi)) {
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001832 fsi->rate = params_rate(params);
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001833 fsi_clk_valid(fsi, fsi->rate);
1834 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001835
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001836 return 0;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001837}
1838
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001839static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001840 .startup = fsi_dai_startup,
1841 .shutdown = fsi_dai_shutdown,
1842 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001843 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001844 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001845};
1846
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001847/*
1848 * pcm ops
1849 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001850
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001851static struct snd_pcm_hardware fsi_pcm_hardware = {
1852 .info = SNDRV_PCM_INFO_INTERLEAVED |
1853 SNDRV_PCM_INFO_MMAP |
1854 SNDRV_PCM_INFO_MMAP_VALID |
1855 SNDRV_PCM_INFO_PAUSE,
1856 .formats = FSI_FMTS,
1857 .rates = FSI_RATES,
1858 .rate_min = 8000,
1859 .rate_max = 192000,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001860 .channels_min = 2,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001861 .channels_max = 2,
1862 .buffer_bytes_max = 64 * 1024,
1863 .period_bytes_min = 32,
1864 .period_bytes_max = 8192,
1865 .periods_min = 1,
1866 .periods_max = 32,
1867 .fifo_size = 256,
1868};
1869
1870static int fsi_pcm_open(struct snd_pcm_substream *substream)
1871{
1872 struct snd_pcm_runtime *runtime = substream->runtime;
1873 int ret = 0;
1874
1875 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1876
1877 ret = snd_pcm_hw_constraint_integer(runtime,
1878 SNDRV_PCM_HW_PARAM_PERIODS);
1879
1880 return ret;
1881}
1882
1883static int fsi_hw_params(struct snd_pcm_substream *substream,
1884 struct snd_pcm_hw_params *hw_params)
1885{
1886 return snd_pcm_lib_malloc_pages(substream,
1887 params_buffer_bytes(hw_params));
1888}
1889
1890static int fsi_hw_free(struct snd_pcm_substream *substream)
1891{
1892 return snd_pcm_lib_free_pages(substream);
1893}
1894
1895static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1896{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001897 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001898 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001899
Kuninori Morimoto19878772012-02-08 16:57:29 -08001900 return fsi_sample2frame(fsi, io->buff_sample_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001901}
1902
1903static struct snd_pcm_ops fsi_pcm_ops = {
1904 .open = fsi_pcm_open,
1905 .ioctl = snd_pcm_lib_ioctl,
1906 .hw_params = fsi_hw_params,
1907 .hw_free = fsi_hw_free,
1908 .pointer = fsi_pointer,
1909};
1910
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001911/*
1912 * snd_soc_platform
1913 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001914
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001915#define PREALLOC_BUFFER (32 * 1024)
1916#define PREALLOC_BUFFER_MAX (32 * 1024)
1917
1918static void fsi_pcm_free(struct snd_pcm *pcm)
1919{
1920 snd_pcm_lib_preallocate_free_for_all(pcm);
1921}
1922
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001923static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001924{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001925 struct snd_pcm *pcm = rtd->pcm;
1926
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001927 /*
1928 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1929 * in MMAP mode (i.e. aplay -M)
1930 */
1931 return snd_pcm_lib_preallocate_pages_for_all(
1932 pcm,
1933 SNDRV_DMA_TYPE_CONTINUOUS,
1934 snd_dma_continuous_data(GFP_KERNEL),
1935 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1936}
1937
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001938/*
1939 * alsa struct
1940 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001941
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001942static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001943 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001944 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001945 .playback = {
1946 .rates = FSI_RATES,
1947 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001948 .channels_min = 2,
1949 .channels_max = 2,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001950 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001951 .capture = {
1952 .rates = FSI_RATES,
1953 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001954 .channels_min = 2,
1955 .channels_max = 2,
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001956 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001957 .ops = &fsi_dai_ops,
1958 },
1959 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001961 .playback = {
1962 .rates = FSI_RATES,
1963 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001964 .channels_min = 2,
1965 .channels_max = 2,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001966 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001967 .capture = {
1968 .rates = FSI_RATES,
1969 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001970 .channels_min = 2,
1971 .channels_max = 2,
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001972 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001973 .ops = &fsi_dai_ops,
1974 },
1975};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001976
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001977static struct snd_soc_platform_driver fsi_soc_platform = {
1978 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001979 .pcm_new = fsi_pcm_new,
1980 .pcm_free = fsi_pcm_free,
1981};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001982
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001983/*
1984 * platform function
1985 */
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001986static void fsi_port_info_init(struct fsi_priv *fsi,
1987 struct sh_fsi_port_info *info)
1988{
1989 if (info->flags & SH_FSI_FMT_SPDIF)
1990 fsi->spdif = 1;
Kuninori Morimotoab6340c2012-11-16 01:17:18 -08001991
1992 if (info->flags & SH_FSI_CLK_CPG)
1993 fsi->clk_cpg = 1;
Kuninori Morimoto2522acd2012-11-16 01:17:30 -08001994
1995 if (info->flags & SH_FSI_ENABLE_STREAM_MODE)
1996 fsi->enable_stream = 1;
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001997}
1998
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08001999static void fsi_handler_init(struct fsi_priv *fsi,
2000 struct sh_fsi_port_info *info)
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002001{
2002 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
2003 fsi->playback.priv = fsi;
2004 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
2005 fsi->capture.priv = fsi;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08002006
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002007 if (info->tx_id) {
2008 fsi->playback.slave.shdma_slave.slave_id = info->tx_id;
Guennadi Liakhovetskib8373142012-05-09 17:09:20 +02002009 fsi->playback.handler = &fsi_dma_push_handler;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08002010 }
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002011}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002012
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002013static int fsi_probe(struct platform_device *pdev)
2014{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09002015 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002016 const struct platform_device_id *id_entry;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08002017 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002018 struct sh_fsi_port_info nul_info, *pinfo;
Kuninori Morimoto40f91182012-11-16 01:16:22 -08002019 struct fsi_priv *fsi;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002020 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002021 unsigned int irq;
2022 int ret;
2023
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002024 nul_info.flags = 0;
2025 nul_info.tx_id = 0;
2026 nul_info.rx_id = 0;
2027
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002028 id_entry = pdev->id_entry;
2029 if (!id_entry) {
2030 dev_err(&pdev->dev, "unknown fsi device\n");
2031 return -ENODEV;
2032 }
2033
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002034 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2035 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01002036 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002037 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002038 return -ENODEV;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002039 }
2040
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002041 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002042 if (!master) {
2043 dev_err(&pdev->dev, "Could not allocate master\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002044 return -ENOMEM;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002045 }
2046
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002047 master->base = devm_ioremap_nocache(&pdev->dev,
2048 res->start, resource_size(res));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002049 if (!master->base) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002050 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002051 return -ENXIO;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002052 }
2053
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09002054 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002055 master->irq = irq;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09002056 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09002057 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002058
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09002059 /* FSI A setting */
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002060 pinfo = (info) ? &info->port_a : &nul_info;
Kuninori Morimoto40f91182012-11-16 01:16:22 -08002061 fsi = &master->fsia;
2062 fsi->base = master->base;
2063 fsi->master = master;
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002064 fsi->info = pinfo;
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08002065 fsi_port_info_init(fsi, pinfo);
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002066 fsi_handler_init(fsi, pinfo);
Kuninori Morimoto40f91182012-11-16 01:16:22 -08002067 ret = fsi_stream_probe(fsi, &pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002068 if (ret < 0) {
2069 dev_err(&pdev->dev, "FSIA stream probe failed\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002070 return ret;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002071 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09002072
2073 /* FSI B setting */
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002074 pinfo = (info) ? &info->port_b : &nul_info;
Kuninori Morimoto40f91182012-11-16 01:16:22 -08002075 fsi = &master->fsib;
2076 fsi->base = master->base + 0x40;
2077 fsi->master = master;
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002078 fsi->info = pinfo;
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08002079 fsi_port_info_init(fsi, pinfo);
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08002080 fsi_handler_init(fsi, pinfo);
Kuninori Morimoto40f91182012-11-16 01:16:22 -08002081 ret = fsi_stream_probe(fsi, &pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002082 if (ret < 0) {
2083 dev_err(&pdev->dev, "FSIB stream probe failed\n");
2084 goto exit_fsia;
2085 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09002086
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002087 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002088 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002089
Kuninori Morimoto1ddd8282012-10-02 23:22:57 -07002090 ret = devm_request_irq(&pdev->dev, irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002091 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002092 if (ret) {
2093 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002094 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002095 }
2096
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002097 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002098 if (ret < 0) {
2099 dev_err(&pdev->dev, "cannot snd soc register\n");
Kuninori Morimoto1ddd8282012-10-02 23:22:57 -07002100 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002101 }
2102
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09002103 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
2104 ARRAY_SIZE(fsi_soc_dai));
2105 if (ret < 0) {
2106 dev_err(&pdev->dev, "cannot snd dai register\n");
2107 goto exit_snd_soc;
2108 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002109
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09002110 return ret;
2111
2112exit_snd_soc:
2113 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002114exit_fsib:
Kuninori Morimotoc35e0052012-09-10 02:13:52 -07002115 pm_runtime_disable(&pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002116 fsi_stream_remove(&master->fsib);
2117exit_fsia:
2118 fsi_stream_remove(&master->fsia);
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002119
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002120 return ret;
2121}
2122
2123static int fsi_remove(struct platform_device *pdev)
2124{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09002125 struct fsi_master *master;
2126
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002127 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09002128
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002129 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002130
Kuninori Morimotod985f272011-04-08 15:09:25 +09002131 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
2132 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002133
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002134 fsi_stream_remove(&master->fsia);
2135 fsi_stream_remove(&master->fsib);
2136
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002137 return 0;
2138}
2139
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002140static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002141 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09002142 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002143{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002144 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002145 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002146
Kuninori Morimoto180346e2012-02-03 00:57:25 -08002147 fsi_stream_stop(fsi, io);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08002148 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002149}
2150
2151static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002152 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09002153 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002154{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002155 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002156 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002157
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002158 fsi_hw_startup(fsi, io, dev);
Kuninori Morimoto180346e2012-02-03 00:57:25 -08002159 fsi_stream_start(fsi, io);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002160}
2161
2162static int fsi_suspend(struct device *dev)
2163{
2164 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002165 struct fsi_priv *fsia = &master->fsia;
2166 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002167
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002168 __fsi_suspend(fsia, &fsia->playback, dev);
2169 __fsi_suspend(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002170
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002171 __fsi_suspend(fsib, &fsib->playback, dev);
2172 __fsi_suspend(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002173
2174 return 0;
2175}
2176
2177static int fsi_resume(struct device *dev)
2178{
2179 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002180 struct fsi_priv *fsia = &master->fsia;
2181 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002182
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002183 __fsi_resume(fsia, &fsia->playback, dev);
2184 __fsi_resume(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002185
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002186 __fsi_resume(fsib, &fsib->playback, dev);
2187 __fsi_resume(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002188
2189 return 0;
2190}
2191
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002192static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002193 .suspend = fsi_suspend,
2194 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002195};
2196
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09002197static struct fsi_core fsi1_core = {
2198 .ver = 1,
2199
2200 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002201 .int_st = INT_ST,
2202 .iemsk = IEMSK,
2203 .imsk = IMSK,
2204};
2205
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09002206static struct fsi_core fsi2_core = {
2207 .ver = 2,
2208
2209 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002210 .int_st = CPU_INT_ST,
2211 .iemsk = CPU_IEMSK,
2212 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09002213 .a_mclk = A_MST_CTLR,
2214 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002215};
2216
2217static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09002218 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
2219 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02002220 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002221};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03002222MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002223
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002224static struct platform_driver fsi_driver = {
2225 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002226 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002227 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002228 },
2229 .probe = fsi_probe,
2230 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002231 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002232};
2233
Axel Lincb5e8732011-11-25 10:15:07 +08002234module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002235
2236MODULE_LICENSE("GPL");
2237MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
2238MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02002239MODULE_ALIAS("platform:fsi-pcm-audio");