blob: 3241e5bdd54bd6f6210b0fb074d2078318296684 [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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-12-17 12:55:22 +0900420
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900421 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-12-17 12:55:22 +0900846static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900847{
Kuninori Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-12-17 12:55:22 +0900851static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900852{
Kuninori Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto1ec9bc32010-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 Morimoto9478e0b2011-05-23 20:46:07 +0900896 /* clock setting */
897 if (fsi_is_clk_master(fsi))
898 data = DIMD | DOMD;
899
900 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900901
902 /* clock inversion (CKG2) */
903 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900904 if (SH_FSI_LRM_INV & flags)
905 data |= 1 << 12;
906 if (SH_FSI_BRM_INV & flags)
907 data |= 1 << 8;
908 if (SH_FSI_LRS_INV & flags)
909 data |= 1 << 4;
910 if (SH_FSI_BRS_INV & flags)
911 data |= 1 << 0;
912
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900913 fsi_reg_write(fsi, CKG2, data);
914
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900915 /* set format */
916 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
917 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
918
919 /* spdif ? */
920 if (fsi_is_spdif(fsi)) {
921 fsi_spdif_clk_ctrl(fsi, 1);
922 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
923 }
924
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800925 /*
926 * FIXME
927 *
928 * FSI driver assumed that data package is in-back.
929 * FSI2 chip can select it.
930 */
931 if (fsi_ver >= 2) {
932 fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
933 fsi_reg_write(fsi, IN_DMAC, (1 << 4));
934 }
935
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900936 /* irq clear */
937 fsi_irq_disable(fsi, is_play);
938 fsi_irq_clear_status(fsi);
939
940 /* fifo init */
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900941 fsi_fifo_init(fsi, is_play, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900942
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900943 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900944}
945
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900946static void fsi_hw_shutdown(struct fsi_priv *fsi,
947 int is_play,
948 struct device *dev)
949{
950 if (fsi_is_clk_master(fsi))
951 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900952}
953
954static int fsi_dai_startup(struct snd_pcm_substream *substream,
955 struct snd_soc_dai *dai)
956{
957 struct fsi_priv *fsi = fsi_get_priv(substream);
958 int is_play = fsi_is_play(substream);
959
960 return fsi_hw_startup(fsi, is_play, dai->dev);
961}
962
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900963static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
964 struct snd_soc_dai *dai)
965{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900966 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900967 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900968
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900969 fsi_hw_shutdown(fsi, is_play, dai->dev);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000970 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900971}
972
973static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
974 struct snd_soc_dai *dai)
975{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900976 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900977 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900978 int ret = 0;
979
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900980 switch (cmd) {
981 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900982 fsi_stream_push(fsi, is_play, substream);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900983 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900984 fsi_port_start(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900985 break;
986 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900987 fsi_port_stop(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900988 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900989 break;
990 }
991
992 return ret;
993}
994
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900995static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
996{
997 u32 data = 0;
998
999 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1000 case SND_SOC_DAIFMT_I2S:
1001 data = CR_I2S;
1002 fsi->chan_num = 2;
1003 break;
1004 case SND_SOC_DAIFMT_LEFT_J:
1005 data = CR_PCM;
1006 fsi->chan_num = 2;
1007 break;
1008 default:
1009 return -EINVAL;
1010 }
1011
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001012 fsi->do_fmt = data;
1013 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001014
1015 return 0;
1016}
1017
1018static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1019{
1020 struct fsi_master *master = fsi_get_master(fsi);
1021 u32 data = 0;
1022
1023 if (master->core->ver < 2)
1024 return -EINVAL;
1025
1026 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1027 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001028 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001029
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001030 fsi->do_fmt = data;
1031 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001032
1033 return 0;
1034}
1035
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001036static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1037{
1038 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001039 struct fsi_master *master = fsi_get_master(fsi);
1040 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001041 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001042 int ret;
1043
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001044 /* set master/slave audio interface */
1045 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1046 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001047 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001048 break;
1049 case SND_SOC_DAIFMT_CBS_CFS:
1050 break;
1051 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001052 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001053 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001054
1055 if (fsi_is_clk_master(fsi) && !set_rate) {
1056 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001057 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001058 }
1059
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001060 /* set format */
1061 switch (flags & SH_FSI_FMT_MASK) {
1062 case SH_FSI_FMT_DAI:
1063 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1064 break;
1065 case SH_FSI_FMT_SPDIF:
1066 ret = fsi_set_fmt_spdif(fsi);
1067 break;
1068 default:
1069 ret = -EINVAL;
1070 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001071
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001072 return ret;
1073}
1074
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001075static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1076 struct snd_pcm_hw_params *params,
1077 struct snd_soc_dai *dai)
1078{
1079 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001080 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001081 int ret;
1082
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001083 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001084 return 0;
1085
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001086 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1087 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001088 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001089
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001090 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001091
1092 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001093}
1094
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001095static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001096 .startup = fsi_dai_startup,
1097 .shutdown = fsi_dai_shutdown,
1098 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001099 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001100 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001101};
1102
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001103/*
1104 * pcm ops
1105 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001106
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001107static struct snd_pcm_hardware fsi_pcm_hardware = {
1108 .info = SNDRV_PCM_INFO_INTERLEAVED |
1109 SNDRV_PCM_INFO_MMAP |
1110 SNDRV_PCM_INFO_MMAP_VALID |
1111 SNDRV_PCM_INFO_PAUSE,
1112 .formats = FSI_FMTS,
1113 .rates = FSI_RATES,
1114 .rate_min = 8000,
1115 .rate_max = 192000,
1116 .channels_min = 1,
1117 .channels_max = 2,
1118 .buffer_bytes_max = 64 * 1024,
1119 .period_bytes_min = 32,
1120 .period_bytes_max = 8192,
1121 .periods_min = 1,
1122 .periods_max = 32,
1123 .fifo_size = 256,
1124};
1125
1126static int fsi_pcm_open(struct snd_pcm_substream *substream)
1127{
1128 struct snd_pcm_runtime *runtime = substream->runtime;
1129 int ret = 0;
1130
1131 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1132
1133 ret = snd_pcm_hw_constraint_integer(runtime,
1134 SNDRV_PCM_HW_PARAM_PERIODS);
1135
1136 return ret;
1137}
1138
1139static int fsi_hw_params(struct snd_pcm_substream *substream,
1140 struct snd_pcm_hw_params *hw_params)
1141{
1142 return snd_pcm_lib_malloc_pages(substream,
1143 params_buffer_bytes(hw_params));
1144}
1145
1146static int fsi_hw_free(struct snd_pcm_substream *substream)
1147{
1148 return snd_pcm_lib_free_pages(substream);
1149}
1150
1151static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1152{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001153 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001154 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001155 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001156
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001157 if (samples_pos < 0)
1158 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001159
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001160 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001161}
1162
1163static struct snd_pcm_ops fsi_pcm_ops = {
1164 .open = fsi_pcm_open,
1165 .ioctl = snd_pcm_lib_ioctl,
1166 .hw_params = fsi_hw_params,
1167 .hw_free = fsi_hw_free,
1168 .pointer = fsi_pointer,
1169};
1170
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001171/*
1172 * snd_soc_platform
1173 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001174
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001175#define PREALLOC_BUFFER (32 * 1024)
1176#define PREALLOC_BUFFER_MAX (32 * 1024)
1177
1178static void fsi_pcm_free(struct snd_pcm *pcm)
1179{
1180 snd_pcm_lib_preallocate_free_for_all(pcm);
1181}
1182
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001183static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001184{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001185 struct snd_pcm *pcm = rtd->pcm;
1186
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001187 /*
1188 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1189 * in MMAP mode (i.e. aplay -M)
1190 */
1191 return snd_pcm_lib_preallocate_pages_for_all(
1192 pcm,
1193 SNDRV_DMA_TYPE_CONTINUOUS,
1194 snd_dma_continuous_data(GFP_KERNEL),
1195 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1196}
1197
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001198/*
1199 * alsa struct
1200 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001201
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001202static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001203 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001205 .playback = {
1206 .rates = FSI_RATES,
1207 .formats = FSI_FMTS,
1208 .channels_min = 1,
1209 .channels_max = 8,
1210 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001211 .capture = {
1212 .rates = FSI_RATES,
1213 .formats = FSI_FMTS,
1214 .channels_min = 1,
1215 .channels_max = 8,
1216 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001217 .ops = &fsi_dai_ops,
1218 },
1219 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001220 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001221 .playback = {
1222 .rates = FSI_RATES,
1223 .formats = FSI_FMTS,
1224 .channels_min = 1,
1225 .channels_max = 8,
1226 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001227 .capture = {
1228 .rates = FSI_RATES,
1229 .formats = FSI_FMTS,
1230 .channels_min = 1,
1231 .channels_max = 8,
1232 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001233 .ops = &fsi_dai_ops,
1234 },
1235};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237static struct snd_soc_platform_driver fsi_soc_platform = {
1238 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001239 .pcm_new = fsi_pcm_new,
1240 .pcm_free = fsi_pcm_free,
1241};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001242
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001243/*
1244 * platform function
1245 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001246
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001247static int fsi_probe(struct platform_device *pdev)
1248{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001249 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001250 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001251 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001252 unsigned int irq;
1253 int ret;
1254
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001255 id_entry = pdev->id_entry;
1256 if (!id_entry) {
1257 dev_err(&pdev->dev, "unknown fsi device\n");
1258 return -ENODEV;
1259 }
1260
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001261 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1262 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001263 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001264 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1265 ret = -ENODEV;
1266 goto exit;
1267 }
1268
1269 master = kzalloc(sizeof(*master), GFP_KERNEL);
1270 if (!master) {
1271 dev_err(&pdev->dev, "Could not allocate master\n");
1272 ret = -ENOMEM;
1273 goto exit;
1274 }
1275
1276 master->base = ioremap_nocache(res->start, resource_size(res));
1277 if (!master->base) {
1278 ret = -ENXIO;
1279 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1280 goto exit_kfree;
1281 }
1282
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001283 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001284 master->irq = irq;
1285 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001286 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001287 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001288
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001289 /* FSI A setting */
1290 master->fsia.base = master->base;
1291 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001292
1293 /* FSI B setting */
1294 master->fsib.base = master->base + 0x40;
1295 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001296
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001297 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001299
Yong Zhang88e24c32011-09-22 16:59:20 +08001300 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001301 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001302 if (ret) {
1303 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001304 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001305 }
1306
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001307 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001308 if (ret < 0) {
1309 dev_err(&pdev->dev, "cannot snd soc register\n");
1310 goto exit_free_irq;
1311 }
1312
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001313 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1314 ARRAY_SIZE(fsi_soc_dai));
1315 if (ret < 0) {
1316 dev_err(&pdev->dev, "cannot snd dai register\n");
1317 goto exit_snd_soc;
1318 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001319
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001320 return ret;
1321
1322exit_snd_soc:
1323 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001324exit_free_irq:
1325 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001326exit_iounmap:
1327 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001328 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001329exit_kfree:
1330 kfree(master);
1331 master = NULL;
1332exit:
1333 return ret;
1334}
1335
1336static int fsi_remove(struct platform_device *pdev)
1337{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001338 struct fsi_master *master;
1339
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001340 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001341
Kuninori Morimotod985f272011-04-08 15:09:25 +09001342 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001343 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001344
Kuninori Morimotod985f272011-04-08 15:09:25 +09001345 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1346 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001347
1348 iounmap(master->base);
1349 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001350
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001351 return 0;
1352}
1353
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001354static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001355 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001356 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001357{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001358 if (!fsi_stream_is_working(fsi, is_play))
1359 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001360
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001361 fsi_port_stop(fsi, is_play);
1362 fsi_hw_shutdown(fsi, is_play, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001363}
1364
1365static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001366 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001367 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001368{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001369 if (!fsi_stream_is_working(fsi, is_play))
1370 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001371
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001372 fsi_hw_startup(fsi, is_play, dev);
1373
1374 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001375 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001376
1377 fsi_port_start(fsi, is_play);
1378
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001379}
1380
1381static int fsi_suspend(struct device *dev)
1382{
1383 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001384 struct fsi_priv *fsia = &master->fsia;
1385 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001386
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001387 __fsi_suspend(fsia, 1, dev);
1388 __fsi_suspend(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001389
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001390 __fsi_suspend(fsib, 1, dev);
1391 __fsi_suspend(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001392
1393 return 0;
1394}
1395
1396static int fsi_resume(struct device *dev)
1397{
1398 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001399 struct fsi_priv *fsia = &master->fsia;
1400 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001401
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001402 __fsi_resume(fsia, 1, dev);
1403 __fsi_resume(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001404
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001405 __fsi_resume(fsib, 1, dev);
1406 __fsi_resume(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001407
1408 return 0;
1409}
1410
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001411static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001412 .suspend = fsi_suspend,
1413 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001414};
1415
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001416static struct fsi_core fsi1_core = {
1417 .ver = 1,
1418
1419 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001420 .int_st = INT_ST,
1421 .iemsk = IEMSK,
1422 .imsk = IMSK,
1423};
1424
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001425static struct fsi_core fsi2_core = {
1426 .ver = 2,
1427
1428 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001429 .int_st = CPU_INT_ST,
1430 .iemsk = CPU_IEMSK,
1431 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001432 .a_mclk = A_MST_CTLR,
1433 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001434};
1435
1436static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001437 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1438 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001439 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001440};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001441MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001442
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001443static struct platform_driver fsi_driver = {
1444 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001445 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001446 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001447 },
1448 .probe = fsi_probe,
1449 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001450 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001451};
1452
Axel Lincb5e8732011-11-25 10:15:07 +08001453module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001454
1455MODULE_LICENSE("GPL");
1456MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1457MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001458MODULE_ALIAS("platform:fsi-pcm-audio");