blob: 99ed61024166143480dc3882b664f36ca11e7d72 [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 Morimoto785d1c42009-11-30 20:24:48 +090016#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090017#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040019#include <linux/module.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090020#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090021#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090022
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090023/* PortA/PortB register */
24#define REG_DO_FMT 0x0000
25#define REG_DOFF_CTL 0x0004
26#define REG_DOFF_ST 0x0008
27#define REG_DI_FMT 0x000C
28#define REG_DIFF_CTL 0x0010
29#define REG_DIFF_ST 0x0014
30#define REG_CKG1 0x0018
31#define REG_CKG2 0x001C
32#define REG_DIDT 0x0020
33#define REG_DODT 0x0024
34#define REG_MUTE_ST 0x0028
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080035#define REG_OUT_DMAC 0x002C
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090036#define REG_OUT_SEL 0x0030
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080037#define REG_IN_DMAC 0x0038
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090038
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090039/* master register */
40#define MST_CLK_RST 0x0210
41#define MST_SOFT_RST 0x0214
42#define MST_FIFO_SZ 0x0218
43
44/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090045#define A_MST_CTLR 0x0180
46#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090047#define CPU_INT_ST 0x01F4
48#define CPU_IEMSK 0x01F8
49#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090050#define INT_ST 0x0200
51#define IEMSK 0x0204
52#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090053
54/* DO_FMT */
55/* DI_FMT */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090056#define CR_BWS_24 (0x0 << 20) /* FSI2 */
57#define CR_BWS_16 (0x1 << 20) /* FSI2 */
58#define CR_BWS_20 (0x2 << 20) /* FSI2 */
59
60#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
61#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
62#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
63
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090064#define CR_MONO (0x0 << 4)
65#define CR_MONO_D (0x1 << 4)
66#define CR_PCM (0x2 << 4)
67#define CR_I2S (0x3 << 4)
68#define CR_TDM (0x4 << 4)
69#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090070
71/* DOFF_CTL */
72/* DIFF_CTL */
73#define IRQ_HALF 0x00100000
74#define FIFO_CLR 0x00000001
75
76/* DOFF_ST */
77#define ERR_OVER 0x00000010
78#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090079#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090080
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090081/* CKG1 */
82#define ACKMD_MASK 0x00007000
83#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090084#define DIMD (1 << 4)
85#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090086
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090087/* A/B MST_CTLR */
88#define BP (1 << 4) /* Fix the signal of Biphase output */
89#define SE (1 << 0) /* Fix the master clock */
90
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090091/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +090092#define CRB (1 << 4)
93#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090094
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090095/* IO SHIFT / MACRO */
96#define BI_SHIFT 12
97#define BO_SHIFT 8
98#define AI_SHIFT 4
99#define AO_SHIFT 0
100#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900101
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900102/* SOFT_RST */
103#define PBSR (1 << 12) /* Port B Software Reset */
104#define PASR (1 << 8) /* Port A Software Reset */
105#define IR (1 << 4) /* Interrupt Reset */
106#define FSISR (1 << 0) /* Software Reset */
107
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900108/* OUT_SEL (FSI2) */
109#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
110 /* 1: Biphase and serial */
111
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900112/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900113#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900114
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900115#define FSI_RATES SNDRV_PCM_RATE_8000_96000
116
117#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
118
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900119typedef int (*set_rate_func)(struct device *dev, int is_porta, int rate, int enable);
120
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900121/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900122 * FSI driver use below type name for variable
123 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900124 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900125 * xxx_pos : position of data
126 * xxx_capa : capacity of data
127 */
128
129/*
130 * period/frame/sample image
131 *
132 * ex) PCM (2ch)
133 *
134 * period pos period pos
135 * [n] [n + 1]
136 * |<-------------------- period--------------------->|
137 * ==|============================================ ... =|==
138 * | |
139 * ||<----- frame ----->|<------ frame ----->| ... |
140 * |+--------------------+--------------------+- ... |
141 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
142 * |+--------------------+--------------------+- ... |
143 * ==|============================================ ... =|==
144 */
145
146/*
147 * FSI FIFO image
148 *
149 * | |
150 * | |
151 * | [ sample ] |
152 * | [ sample ] |
153 * | [ sample ] |
154 * | [ sample ] |
155 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900156 */
157
158/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900159 * struct
160 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900161
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900162struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900163 struct snd_pcm_substream *substream;
164
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900165 int fifo_sample_capa; /* sample capacity of FSI FIFO */
166 int buff_sample_capa; /* sample capacity of ALSA buffer */
167 int buff_sample_pos; /* sample position of ALSA buffer */
168 int period_samples; /* sample number / 1 period */
169 int period_pos; /* current period position */
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900170
171 int uerr_num;
172 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900173};
174
175struct fsi_priv {
176 void __iomem *base;
177 struct fsi_master *master;
178
179 struct fsi_stream playback;
180 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900181
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900182 u32 do_fmt;
183 u32 di_fmt;
184
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900185 int chan_num:16;
186 int clk_master:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900187 int spdif:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900188
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000189 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900190};
191
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900192struct fsi_core {
193 int ver;
194
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900195 u32 int_st;
196 u32 iemsk;
197 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900198 u32 a_mclk;
199 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900200};
201
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900202struct fsi_master {
203 void __iomem *base;
204 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900205 struct fsi_priv fsia;
206 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900207 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900208 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900209 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900210};
211
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900212/*
213 * basic read write function
214 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900215
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200216static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900217{
218 /* valid data area is 24bit */
219 data &= 0x00ffffff;
220
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100221 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900222}
223
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200224static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900225{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100226 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900227}
228
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200229static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900230{
231 u32 val = __fsi_reg_read(reg);
232
233 val &= ~mask;
234 val |= data & mask;
235
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100236 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900237}
238
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900239#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700240 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900241
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900242#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700243 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900244
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900245#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700246 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900248#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
249#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
250static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900251{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900252 u32 ret;
253 unsigned long flags;
254
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900255 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200256 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900257 spin_unlock_irqrestore(&master->lock, flags);
258
259 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900260}
261
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900262#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
263#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
264static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900265 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900266{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900267 unsigned long flags;
268
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900269 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200270 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900271 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900272}
273
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900274/*
275 * basic function
276 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900277
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900278static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900279{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900280 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900281}
282
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900283static int fsi_is_clk_master(struct fsi_priv *fsi)
284{
285 return fsi->clk_master;
286}
287
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900288static int fsi_is_port_a(struct fsi_priv *fsi)
289{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900290 return fsi->master->base == fsi->base;
291}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900292
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900293static int fsi_is_spdif(struct fsi_priv *fsi)
294{
295 return fsi->spdif;
296}
297
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900298static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900299{
300 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900301
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000302 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900303}
304
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900305static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900306{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900308
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000309 if (dai->id == 0)
310 return &master->fsia;
311 else
312 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900313}
314
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900315static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
316{
317 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
318}
319
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900320static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
321{
322 if (!master->info)
323 return NULL;
324
325 return master->info->set_rate;
326}
327
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900328static u32 fsi_get_info_flags(struct fsi_priv *fsi)
329{
330 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900331 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900332
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900333 if (!master->info)
334 return 0;
335
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900336 return is_porta ? master->info->porta_flags :
337 master->info->portb_flags;
338}
339
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900340static inline int fsi_stream_is_play(int stream)
341{
342 return stream == SNDRV_PCM_STREAM_PLAYBACK;
343}
344
Kuninori Morimoto00545782010-10-12 18:30:14 +0900345static inline int fsi_is_play(struct snd_pcm_substream *substream)
346{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900347 return fsi_stream_is_play(substream->stream);
348}
349
350static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
351 int is_play)
352{
353 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900354}
355
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900356static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900357{
358 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900359 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900360
361 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900362 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900363 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900364 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900365
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900366 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900367}
368
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900369static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
370{
371 return frames * fsi->chan_num;
372}
373
374static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
375{
376 return samples / fsi->chan_num;
377}
378
Kuninori Morimotocda828c2011-05-23 20:46:35 +0900379static int fsi_stream_is_working(struct fsi_priv *fsi,
380 int is_play)
381{
382 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
383 struct fsi_master *master = fsi_get_master(fsi);
384 unsigned long flags;
385 int ret;
386
387 spin_lock_irqsave(&master->lock, flags);
388 ret = !!io->substream;
389 spin_unlock_irqrestore(&master->lock, flags);
390
391 return ret;
392}
393
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900394static void fsi_stream_push(struct fsi_priv *fsi,
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900395 int is_play,
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900396 struct snd_pcm_substream *substream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900397{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900398 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900399 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900400 struct fsi_master *master = fsi_get_master(fsi);
401 unsigned long flags;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900402
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900403 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900404 io->substream = substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900405 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
406 io->buff_sample_pos = 0;
407 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
408 io->period_pos = 0;
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900409 io->oerr_num = -1; /* ignore 1st err */
410 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900411 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900412}
413
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900414static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900415{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900416 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900417 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900418 struct fsi_master *master = fsi_get_master(fsi);
419 unsigned long flags;
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900420
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900421 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900422
423 if (io->oerr_num > 0)
424 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
425
426 if (io->uerr_num > 0)
427 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900428
429 io->substream = NULL;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900430 io->buff_sample_capa = 0;
431 io->buff_sample_pos = 0;
432 io->period_samples = 0;
433 io->period_pos = 0;
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900434 io->oerr_num = 0;
435 io->uerr_num = 0;
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900436 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900437}
438
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900439static int fsi_get_current_fifo_samples(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900440{
441 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900442 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900443
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900444 status = is_play ?
445 fsi_reg_read(fsi, DOFF_ST) :
446 fsi_reg_read(fsi, DIFF_ST);
447
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900448 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900449
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900450 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900451}
452
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900453static void fsi_count_fifo_err(struct fsi_priv *fsi)
454{
455 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
456 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
457
458 if (ostatus & ERR_OVER)
459 fsi->playback.oerr_num++;
460
461 if (ostatus & ERR_UNDER)
462 fsi->playback.uerr_num++;
463
464 if (istatus & ERR_OVER)
465 fsi->capture.oerr_num++;
466
467 if (istatus & ERR_UNDER)
468 fsi->capture.uerr_num++;
469
470 fsi_reg_write(fsi, DOFF_ST, 0);
471 fsi_reg_write(fsi, DIFF_ST, 0);
472}
473
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900474/*
475 * dma function
476 */
477
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900478static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900479{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900480 int is_play = fsi_stream_is_play(stream);
481 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900482 struct snd_pcm_runtime *runtime = io->substream->runtime;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900483
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900484 return runtime->dma_area +
485 samples_to_bytes(runtime, io->buff_sample_pos);
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900486}
487
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900488static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900489{
490 u16 *start;
491 int i;
492
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900493 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900494
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900495 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900496 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
497}
498
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900499static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900500{
501 u16 *start;
502 int i;
503
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900504 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
505
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900506
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900507 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900508 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
509}
510
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900511static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900512{
513 u32 *start;
514 int i;
515
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900516 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
517
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900518
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900519 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900520 fsi_reg_write(fsi, DODT, *(start + i));
521}
522
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900523static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900524{
525 u32 *start;
526 int i;
527
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900528 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900529
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900530 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900531 *(start + i) = fsi_reg_read(fsi, DIDT);
532}
533
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900534/*
535 * irq function
536 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900537
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900538static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
539{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900540 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900541 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900542
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900543 fsi_core_mask_set(master, imsk, data, data);
544 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900545}
546
547static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
548{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900549 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900550 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900551
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900552 fsi_core_mask_set(master, imsk, data, 0);
553 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900554}
555
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900556static u32 fsi_irq_get_status(struct fsi_master *master)
557{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900558 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900559}
560
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900561static void fsi_irq_clear_status(struct fsi_priv *fsi)
562{
563 u32 data = 0;
564 struct fsi_master *master = fsi_get_master(fsi);
565
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900566 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
567 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900568
569 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900570 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900571}
572
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900573/*
574 * SPDIF master clock function
575 *
576 * These functions are used later FSI2
577 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900578static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
579{
580 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900581 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900582
583 if (master->core->ver < 2) {
584 pr_err("fsi: register access err (%s)\n", __func__);
585 return;
586 }
587
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900588 mask = BP | SE;
589 val = enable ? mask : 0;
590
591 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900592 fsi_core_mask_set(master, a_mclk, mask, val) :
593 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900594}
595
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900596/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900597 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900598 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900599static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
600 long rate, int enable)
601{
602 struct fsi_master *master = fsi_get_master(fsi);
603 set_rate_func set_rate = fsi_get_info_set_rate(master);
604 int fsi_ver = master->core->ver;
605 int ret;
606
607 ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
608 if (ret < 0) /* error */
609 return ret;
610
611 if (!enable)
612 return 0;
613
614 if (ret > 0) {
615 u32 data = 0;
616
617 switch (ret & SH_FSI_ACKMD_MASK) {
618 default:
619 /* FALL THROUGH */
620 case SH_FSI_ACKMD_512:
621 data |= (0x0 << 12);
622 break;
623 case SH_FSI_ACKMD_256:
624 data |= (0x1 << 12);
625 break;
626 case SH_FSI_ACKMD_128:
627 data |= (0x2 << 12);
628 break;
629 case SH_FSI_ACKMD_64:
630 data |= (0x3 << 12);
631 break;
632 case SH_FSI_ACKMD_32:
633 if (fsi_ver < 2)
634 dev_err(dev, "unsupported ACKMD\n");
635 else
636 data |= (0x4 << 12);
637 break;
638 }
639
640 switch (ret & SH_FSI_BPFMD_MASK) {
641 default:
642 /* FALL THROUGH */
643 case SH_FSI_BPFMD_32:
644 data |= (0x0 << 8);
645 break;
646 case SH_FSI_BPFMD_64:
647 data |= (0x1 << 8);
648 break;
649 case SH_FSI_BPFMD_128:
650 data |= (0x2 << 8);
651 break;
652 case SH_FSI_BPFMD_256:
653 data |= (0x3 << 8);
654 break;
655 case SH_FSI_BPFMD_512:
656 data |= (0x4 << 8);
657 break;
658 case SH_FSI_BPFMD_16:
659 if (fsi_ver < 2)
660 dev_err(dev, "unsupported ACKMD\n");
661 else
662 data |= (0x7 << 8);
663 break;
664 }
665
666 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
667 udelay(10);
668 ret = 0;
669 }
670
671 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900672}
673
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900674#define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
675#define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
676static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900677{
678 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900679 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900680
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900681 if (enable)
682 fsi_irq_enable(fsi, is_play);
683 else
684 fsi_irq_disable(fsi, is_play);
685
Kuninori Morimotocda828c2011-05-23 20:46:35 +0900686 if (fsi_is_clk_master(fsi))
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900687 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
688}
689
690/*
691 * ctrl function
692 */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900693static void fsi_fifo_init(struct fsi_priv *fsi,
694 int is_play,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900695 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900696{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900697 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900698 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900699 u32 shift, i;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900700 int frame_capa;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900701
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900702 /* get on-chip RAM capacity */
703 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900704 shift >>= fsi_get_port_shift(fsi, is_play);
705 shift &= FIFO_SZ_MASK;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900706 frame_capa = 256 << shift;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900707 dev_dbg(dev, "fifo = %d words\n", frame_capa);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900708
709 /*
710 * The maximum number of sample data varies depending
711 * on the number of channels selected for the format.
712 *
713 * FIFOs are used in 4-channel units in 3-channel mode
714 * and in 8-channel units in 5- to 7-channel mode
715 * meaning that more FIFOs than the required size of DPRAM
716 * are used.
717 *
718 * ex) if 256 words of DP-RAM is connected
719 * 1 channel: 256 (256 x 1 = 256)
720 * 2 channels: 128 (128 x 2 = 256)
721 * 3 channels: 64 ( 64 x 3 = 192)
722 * 4 channels: 64 ( 64 x 4 = 256)
723 * 5 channels: 32 ( 32 x 5 = 160)
724 * 6 channels: 32 ( 32 x 6 = 192)
725 * 7 channels: 32 ( 32 x 7 = 224)
726 * 8 channels: 32 ( 32 x 8 = 256)
727 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900728 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900729 frame_capa >>= 1;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900730 dev_dbg(dev, "%d channel %d store\n",
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900731 fsi->chan_num, frame_capa);
732
733 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900734
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900735 /*
736 * set interrupt generation factor
737 * clear FIFO
738 */
739 if (is_play) {
740 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
741 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
742 } else {
743 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
744 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
745 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900746}
747
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900748static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900749{
750 struct snd_pcm_runtime *runtime;
751 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900752 int is_play = fsi_stream_is_play(stream);
753 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900754 int sample_residues;
755 int sample_width;
756 int samples;
757 int samples_max;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900758 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900759 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900760
761 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900762 !io->substream ||
763 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900764 return -EINVAL;
765
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900766 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900767 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900768 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900769
770 /* FSI FIFO has limit.
771 * So, this driver can not send periods data at a time
772 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900773 if (io->buff_sample_pos >=
774 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900775
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900776 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900777 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900778
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900779 if (0 == io->period_pos)
780 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900781 }
782
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900783 /* get 1 sample data width */
784 sample_width = samples_to_bytes(runtime, 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900785
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900786 /* get number of residue samples */
787 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900788
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900789 if (is_play) {
790 /*
791 * for play-back
792 *
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900793 * samples_max : number of FSI fifo free samples space
794 * samples : number of ALSA residue samples
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900795 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900796 samples_max = io->fifo_sample_capa;
797 samples_max -= fsi_get_current_fifo_samples(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900798
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900799 samples = sample_residues;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900800
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900801 switch (sample_width) {
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900802 case 2:
803 fn = fsi_dma_soft_push16;
804 break;
805 case 4:
806 fn = fsi_dma_soft_push32;
807 break;
808 default:
809 return -EINVAL;
810 }
811 } else {
812 /*
813 * for capture
814 *
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900815 * samples_max : number of ALSA free samples space
816 * samples : number of samples in FSI fifo
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900817 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900818 samples_max = sample_residues;
819 samples = fsi_get_current_fifo_samples(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900820
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900821 switch (sample_width) {
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900822 case 2:
823 fn = fsi_dma_soft_pop16;
824 break;
825 case 4:
826 fn = fsi_dma_soft_pop32;
827 break;
828 default:
829 return -EINVAL;
830 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900831 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900832
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900833 samples = min(samples, samples_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900834
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900835 fn(fsi, samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900836
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900837 /* update buff_sample_pos */
838 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900839
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900840 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900841 snd_pcm_period_elapsed(substream);
842
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900843 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900844}
845
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900846static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900847{
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900848 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900849}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900850
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900851static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900852{
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900853 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900854}
855
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900856static irqreturn_t fsi_interrupt(int irq, void *data)
857{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900858 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900859 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900860
861 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900862 fsi_master_mask_set(master, SOFT_RST, IR, 0);
863 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900864
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900865 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900866 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900867 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900868 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900869 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900870 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900871 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900872 fsi_data_pop(&master->fsib);
873
874 fsi_count_fifo_err(&master->fsia);
875 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900876
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900877 fsi_irq_clear_status(&master->fsia);
878 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900879
880 return IRQ_HANDLED;
881}
882
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900883/*
884 * dai ops
885 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900886
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900887static int fsi_hw_startup(struct fsi_priv *fsi,
888 int is_play,
889 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900890{
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800891 struct fsi_master *master = fsi_get_master(fsi);
892 int fsi_ver = master->core->ver;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900893 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900894 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900895
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900896 pm_runtime_get_sync(dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900897
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900898 /* clock setting */
899 if (fsi_is_clk_master(fsi))
900 data = DIMD | DOMD;
901
902 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900903
904 /* clock inversion (CKG2) */
905 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900906 if (SH_FSI_LRM_INV & flags)
907 data |= 1 << 12;
908 if (SH_FSI_BRM_INV & flags)
909 data |= 1 << 8;
910 if (SH_FSI_LRS_INV & flags)
911 data |= 1 << 4;
912 if (SH_FSI_BRS_INV & flags)
913 data |= 1 << 0;
914
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900915 fsi_reg_write(fsi, CKG2, data);
916
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900917 /* set format */
918 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
919 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
920
921 /* spdif ? */
922 if (fsi_is_spdif(fsi)) {
923 fsi_spdif_clk_ctrl(fsi, 1);
924 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
925 }
926
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800927 /*
928 * FIXME
929 *
930 * FSI driver assumed that data package is in-back.
931 * FSI2 chip can select it.
932 */
933 if (fsi_ver >= 2) {
934 fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
935 fsi_reg_write(fsi, IN_DMAC, (1 << 4));
936 }
937
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900938 /* irq clear */
939 fsi_irq_disable(fsi, is_play);
940 fsi_irq_clear_status(fsi);
941
942 /* fifo init */
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900943 fsi_fifo_init(fsi, is_play, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900944
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900945 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900946}
947
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900948static void fsi_hw_shutdown(struct fsi_priv *fsi,
949 int is_play,
950 struct device *dev)
951{
952 if (fsi_is_clk_master(fsi))
953 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
954
955 pm_runtime_put_sync(dev);
956}
957
958static int fsi_dai_startup(struct snd_pcm_substream *substream,
959 struct snd_soc_dai *dai)
960{
961 struct fsi_priv *fsi = fsi_get_priv(substream);
962 int is_play = fsi_is_play(substream);
963
964 return fsi_hw_startup(fsi, is_play, dai->dev);
965}
966
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900967static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
968 struct snd_soc_dai *dai)
969{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900970 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900971 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900972
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900973 fsi_hw_shutdown(fsi, is_play, dai->dev);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000974 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900975}
976
977static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
978 struct snd_soc_dai *dai)
979{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900980 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900981 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900982 int ret = 0;
983
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900984 switch (cmd) {
985 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900986 fsi_stream_push(fsi, is_play, substream);
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900987 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900988 fsi_port_start(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900989 break;
990 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900991 fsi_port_stop(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900992 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900993 break;
994 }
995
996 return ret;
997}
998
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900999static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1000{
1001 u32 data = 0;
1002
1003 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1004 case SND_SOC_DAIFMT_I2S:
1005 data = CR_I2S;
1006 fsi->chan_num = 2;
1007 break;
1008 case SND_SOC_DAIFMT_LEFT_J:
1009 data = CR_PCM;
1010 fsi->chan_num = 2;
1011 break;
1012 default:
1013 return -EINVAL;
1014 }
1015
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001016 fsi->do_fmt = data;
1017 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001018
1019 return 0;
1020}
1021
1022static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1023{
1024 struct fsi_master *master = fsi_get_master(fsi);
1025 u32 data = 0;
1026
1027 if (master->core->ver < 2)
1028 return -EINVAL;
1029
1030 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1031 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001032 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001033
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001034 fsi->do_fmt = data;
1035 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001036
1037 return 0;
1038}
1039
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001040static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1041{
1042 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001043 struct fsi_master *master = fsi_get_master(fsi);
1044 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001045 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001046 int ret;
1047
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001048 /* set master/slave audio interface */
1049 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1050 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001051 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001052 break;
1053 case SND_SOC_DAIFMT_CBS_CFS:
1054 break;
1055 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001056 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001057 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001058
1059 if (fsi_is_clk_master(fsi) && !set_rate) {
1060 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001061 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001062 }
1063
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001064 /* set format */
1065 switch (flags & SH_FSI_FMT_MASK) {
1066 case SH_FSI_FMT_DAI:
1067 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1068 break;
1069 case SH_FSI_FMT_SPDIF:
1070 ret = fsi_set_fmt_spdif(fsi);
1071 break;
1072 default:
1073 ret = -EINVAL;
1074 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001075
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001076 return ret;
1077}
1078
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001079static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1080 struct snd_pcm_hw_params *params,
1081 struct snd_soc_dai *dai)
1082{
1083 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001084 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001085 int ret;
1086
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001087 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001088 return 0;
1089
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001090 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1091 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001092 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001093
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001094 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001095
1096 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001097}
1098
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001099static struct snd_soc_dai_ops fsi_dai_ops = {
1100 .startup = fsi_dai_startup,
1101 .shutdown = fsi_dai_shutdown,
1102 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001103 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001104 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001105};
1106
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001107/*
1108 * pcm ops
1109 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001110
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001111static struct snd_pcm_hardware fsi_pcm_hardware = {
1112 .info = SNDRV_PCM_INFO_INTERLEAVED |
1113 SNDRV_PCM_INFO_MMAP |
1114 SNDRV_PCM_INFO_MMAP_VALID |
1115 SNDRV_PCM_INFO_PAUSE,
1116 .formats = FSI_FMTS,
1117 .rates = FSI_RATES,
1118 .rate_min = 8000,
1119 .rate_max = 192000,
1120 .channels_min = 1,
1121 .channels_max = 2,
1122 .buffer_bytes_max = 64 * 1024,
1123 .period_bytes_min = 32,
1124 .period_bytes_max = 8192,
1125 .periods_min = 1,
1126 .periods_max = 32,
1127 .fifo_size = 256,
1128};
1129
1130static int fsi_pcm_open(struct snd_pcm_substream *substream)
1131{
1132 struct snd_pcm_runtime *runtime = substream->runtime;
1133 int ret = 0;
1134
1135 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1136
1137 ret = snd_pcm_hw_constraint_integer(runtime,
1138 SNDRV_PCM_HW_PARAM_PERIODS);
1139
1140 return ret;
1141}
1142
1143static int fsi_hw_params(struct snd_pcm_substream *substream,
1144 struct snd_pcm_hw_params *hw_params)
1145{
1146 return snd_pcm_lib_malloc_pages(substream,
1147 params_buffer_bytes(hw_params));
1148}
1149
1150static int fsi_hw_free(struct snd_pcm_substream *substream)
1151{
1152 return snd_pcm_lib_free_pages(substream);
1153}
1154
1155static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1156{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001157 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001158 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001159 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001160
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001161 if (samples_pos < 0)
1162 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001163
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001164 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001165}
1166
1167static struct snd_pcm_ops fsi_pcm_ops = {
1168 .open = fsi_pcm_open,
1169 .ioctl = snd_pcm_lib_ioctl,
1170 .hw_params = fsi_hw_params,
1171 .hw_free = fsi_hw_free,
1172 .pointer = fsi_pointer,
1173};
1174
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001175/*
1176 * snd_soc_platform
1177 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001178
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001179#define PREALLOC_BUFFER (32 * 1024)
1180#define PREALLOC_BUFFER_MAX (32 * 1024)
1181
1182static void fsi_pcm_free(struct snd_pcm *pcm)
1183{
1184 snd_pcm_lib_preallocate_free_for_all(pcm);
1185}
1186
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001187static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001188{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001189 struct snd_pcm *pcm = rtd->pcm;
1190
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001191 /*
1192 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1193 * in MMAP mode (i.e. aplay -M)
1194 */
1195 return snd_pcm_lib_preallocate_pages_for_all(
1196 pcm,
1197 SNDRV_DMA_TYPE_CONTINUOUS,
1198 snd_dma_continuous_data(GFP_KERNEL),
1199 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1200}
1201
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001202/*
1203 * alsa struct
1204 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001205
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001207 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001208 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001209 .playback = {
1210 .rates = FSI_RATES,
1211 .formats = FSI_FMTS,
1212 .channels_min = 1,
1213 .channels_max = 8,
1214 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001215 .capture = {
1216 .rates = FSI_RATES,
1217 .formats = FSI_FMTS,
1218 .channels_min = 1,
1219 .channels_max = 8,
1220 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001221 .ops = &fsi_dai_ops,
1222 },
1223 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001225 .playback = {
1226 .rates = FSI_RATES,
1227 .formats = FSI_FMTS,
1228 .channels_min = 1,
1229 .channels_max = 8,
1230 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001231 .capture = {
1232 .rates = FSI_RATES,
1233 .formats = FSI_FMTS,
1234 .channels_min = 1,
1235 .channels_max = 8,
1236 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001237 .ops = &fsi_dai_ops,
1238 },
1239};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001240
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001241static struct snd_soc_platform_driver fsi_soc_platform = {
1242 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001243 .pcm_new = fsi_pcm_new,
1244 .pcm_free = fsi_pcm_free,
1245};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001246
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001247/*
1248 * platform function
1249 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001250
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001251static int fsi_probe(struct platform_device *pdev)
1252{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001253 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001254 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001255 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001256 unsigned int irq;
1257 int ret;
1258
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001259 id_entry = pdev->id_entry;
1260 if (!id_entry) {
1261 dev_err(&pdev->dev, "unknown fsi device\n");
1262 return -ENODEV;
1263 }
1264
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001265 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1266 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001267 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001268 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1269 ret = -ENODEV;
1270 goto exit;
1271 }
1272
1273 master = kzalloc(sizeof(*master), GFP_KERNEL);
1274 if (!master) {
1275 dev_err(&pdev->dev, "Could not allocate master\n");
1276 ret = -ENOMEM;
1277 goto exit;
1278 }
1279
1280 master->base = ioremap_nocache(res->start, resource_size(res));
1281 if (!master->base) {
1282 ret = -ENXIO;
1283 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1284 goto exit_kfree;
1285 }
1286
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001287 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001288 master->irq = irq;
1289 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001290 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001291 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001292
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001293 /* FSI A setting */
1294 master->fsia.base = master->base;
1295 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001296
1297 /* FSI B setting */
1298 master->fsib.base = master->base + 0x40;
1299 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001300
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001301 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001302 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001303
Yong Zhang88e24c32011-09-22 16:59:20 +08001304 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001305 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001306 if (ret) {
1307 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001308 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001309 }
1310
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001311 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001312 if (ret < 0) {
1313 dev_err(&pdev->dev, "cannot snd soc register\n");
1314 goto exit_free_irq;
1315 }
1316
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001317 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1318 ARRAY_SIZE(fsi_soc_dai));
1319 if (ret < 0) {
1320 dev_err(&pdev->dev, "cannot snd dai register\n");
1321 goto exit_snd_soc;
1322 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001323
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001324 return ret;
1325
1326exit_snd_soc:
1327 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001328exit_free_irq:
1329 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001330exit_iounmap:
1331 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001332 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001333exit_kfree:
1334 kfree(master);
1335 master = NULL;
1336exit:
1337 return ret;
1338}
1339
1340static int fsi_remove(struct platform_device *pdev)
1341{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001342 struct fsi_master *master;
1343
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001344 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001345
Kuninori Morimotod985f272011-04-08 15:09:25 +09001346 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001347 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001348
Kuninori Morimotod985f272011-04-08 15:09:25 +09001349 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1350 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001351
1352 iounmap(master->base);
1353 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001354
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001355 return 0;
1356}
1357
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001358static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001359 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001360 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001361{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001362 if (!fsi_stream_is_working(fsi, is_play))
1363 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001364
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001365 fsi_port_stop(fsi, is_play);
1366 fsi_hw_shutdown(fsi, is_play, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001367}
1368
1369static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001370 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001371 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001372{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001373 if (!fsi_stream_is_working(fsi, is_play))
1374 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001375
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001376 fsi_hw_startup(fsi, is_play, dev);
1377
1378 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001379 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001380
1381 fsi_port_start(fsi, is_play);
1382
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001383}
1384
1385static int fsi_suspend(struct device *dev)
1386{
1387 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001388 struct fsi_priv *fsia = &master->fsia;
1389 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001390
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001391 __fsi_suspend(fsia, 1, dev);
1392 __fsi_suspend(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001393
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001394 __fsi_suspend(fsib, 1, dev);
1395 __fsi_suspend(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001396
1397 return 0;
1398}
1399
1400static int fsi_resume(struct device *dev)
1401{
1402 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001403 struct fsi_priv *fsia = &master->fsia;
1404 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001405
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001406 __fsi_resume(fsia, 1, dev);
1407 __fsi_resume(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001408
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001409 __fsi_resume(fsib, 1, dev);
1410 __fsi_resume(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001411
1412 return 0;
1413}
1414
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001415static int fsi_runtime_nop(struct device *dev)
1416{
1417 /* Runtime PM callback shared between ->runtime_suspend()
1418 * and ->runtime_resume(). Simply returns success.
1419 *
1420 * This driver re-initializes all registers after
1421 * pm_runtime_get_sync() anyway so there is no need
1422 * to save and restore registers here.
1423 */
1424 return 0;
1425}
1426
1427static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001428 .suspend = fsi_suspend,
1429 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001430 .runtime_suspend = fsi_runtime_nop,
1431 .runtime_resume = fsi_runtime_nop,
1432};
1433
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001434static struct fsi_core fsi1_core = {
1435 .ver = 1,
1436
1437 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001438 .int_st = INT_ST,
1439 .iemsk = IEMSK,
1440 .imsk = IMSK,
1441};
1442
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001443static struct fsi_core fsi2_core = {
1444 .ver = 2,
1445
1446 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001447 .int_st = CPU_INT_ST,
1448 .iemsk = CPU_IEMSK,
1449 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001450 .a_mclk = A_MST_CTLR,
1451 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001452};
1453
1454static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001455 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1456 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001457 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001458};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001459MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001460
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001461static struct platform_driver fsi_driver = {
1462 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001464 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001465 },
1466 .probe = fsi_probe,
1467 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001468 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001469};
1470
1471static int __init fsi_mobile_init(void)
1472{
1473 return platform_driver_register(&fsi_driver);
1474}
1475
1476static void __exit fsi_mobile_exit(void)
1477{
1478 platform_driver_unregister(&fsi_driver);
1479}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001480
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001481module_init(fsi_mobile_init);
1482module_exit(fsi_mobile_exit);
1483
1484MODULE_LICENSE("GPL");
1485MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1486MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001487MODULE_ALIAS("platform:fsi-pcm-audio");