blob: 24dbe165eda8cf9ae551565fbf09681222bebd30 [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 Morimotoc1e6f102012-02-03 00:50:09 -0800170 int sample_width; /* sample width */
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900171
172 int uerr_num;
173 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900174};
175
176struct fsi_priv {
177 void __iomem *base;
178 struct fsi_master *master;
179
180 struct fsi_stream playback;
181 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900182
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900183 u32 do_fmt;
184 u32 di_fmt;
185
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900186 int chan_num:16;
187 int clk_master:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900188 int spdif:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900189
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000190 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900191};
192
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900193struct fsi_core {
194 int ver;
195
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900196 u32 int_st;
197 u32 iemsk;
198 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900199 u32 a_mclk;
200 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900201};
202
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900203struct fsi_master {
204 void __iomem *base;
205 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900206 struct fsi_priv fsia;
207 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900208 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900209 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900210 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900211};
212
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800213static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
214
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900215/*
216 * basic read write function
217 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900218
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200219static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900220{
221 /* valid data area is 24bit */
222 data &= 0x00ffffff;
223
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100224 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900225}
226
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200227static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900228{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100229 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900230}
231
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200232static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900233{
234 u32 val = __fsi_reg_read(reg);
235
236 val &= ~mask;
237 val |= data & mask;
238
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100239 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900240}
241
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900242#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700243 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900244
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900245#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700246 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900248#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700249 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900250
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900251#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
252#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
253static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900254{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900255 u32 ret;
256 unsigned long flags;
257
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900258 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200259 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900260 spin_unlock_irqrestore(&master->lock, flags);
261
262 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900263}
264
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900265#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
266#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
267static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900268 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900269{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900270 unsigned long flags;
271
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900272 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200273 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900274 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900275}
276
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900277/*
278 * basic function
279 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900280
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900281static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900282{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900283 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900284}
285
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900286static int fsi_is_clk_master(struct fsi_priv *fsi)
287{
288 return fsi->clk_master;
289}
290
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900291static int fsi_is_port_a(struct fsi_priv *fsi)
292{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900293 return fsi->master->base == fsi->base;
294}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900295
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900296static int fsi_is_spdif(struct fsi_priv *fsi)
297{
298 return fsi->spdif;
299}
300
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800301static int fsi_is_play(struct snd_pcm_substream *substream)
302{
303 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
304}
305
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900306static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900307{
308 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900309
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000310 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900311}
312
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900313static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900314{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000315 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900316
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000317 if (dai->id == 0)
318 return &master->fsia;
319 else
320 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900321}
322
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900323static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
324{
325 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
326}
327
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900328static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
329{
330 if (!master->info)
331 return NULL;
332
333 return master->info->set_rate;
334}
335
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900336static u32 fsi_get_info_flags(struct fsi_priv *fsi)
337{
338 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900339 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900340
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900341 if (!master->info)
342 return 0;
343
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900344 return is_porta ? master->info->porta_flags :
345 master->info->portb_flags;
346}
347
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900348static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900349{
350 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900351 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900352
353 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900354 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900355 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900356 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900357
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900358 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900359}
360
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900361static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
362{
363 return frames * fsi->chan_num;
364}
365
366static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
367{
368 return samples / fsi->chan_num;
369}
370
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800371static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
372 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900373{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800374 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900375 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900376 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900377
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900378 status = is_play ?
379 fsi_reg_read(fsi, DOFF_ST) :
380 fsi_reg_read(fsi, DIFF_ST);
381
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900382 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900383
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900384 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900385}
386
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900387static void fsi_count_fifo_err(struct fsi_priv *fsi)
388{
389 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
390 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
391
392 if (ostatus & ERR_OVER)
393 fsi->playback.oerr_num++;
394
395 if (ostatus & ERR_UNDER)
396 fsi->playback.uerr_num++;
397
398 if (istatus & ERR_OVER)
399 fsi->capture.oerr_num++;
400
401 if (istatus & ERR_UNDER)
402 fsi->capture.uerr_num++;
403
404 fsi_reg_write(fsi, DOFF_ST, 0);
405 fsi_reg_write(fsi, DIFF_ST, 0);
406}
407
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900408/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800409 * fsi_stream_xx() function
410 */
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800411static inline int fsi_stream_is_play(struct fsi_priv *fsi,
412 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800413{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800414 return &fsi->playback == io;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800415}
416
417static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
418 int is_play)
419{
420 return is_play ? &fsi->playback : &fsi->capture;
421}
422
423static int fsi_stream_is_working(struct fsi_priv *fsi,
424 int is_play)
425{
426 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
427 struct fsi_master *master = fsi_get_master(fsi);
428 unsigned long flags;
429 int ret;
430
431 spin_lock_irqsave(&master->lock, flags);
432 ret = !!io->substream;
433 spin_unlock_irqrestore(&master->lock, flags);
434
435 return ret;
436}
437
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800438static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800439 int is_play,
440 struct snd_pcm_substream *substream)
441{
442 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
443 struct snd_pcm_runtime *runtime = substream->runtime;
444 struct fsi_master *master = fsi_get_master(fsi);
445 unsigned long flags;
446
447 spin_lock_irqsave(&master->lock, flags);
448 io->substream = substream;
449 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
450 io->buff_sample_pos = 0;
451 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
452 io->period_pos = 0;
453 io->sample_width = samples_to_bytes(runtime, 1);
454 io->oerr_num = -1; /* ignore 1st err */
455 io->uerr_num = -1; /* ignore 1st err */
456 spin_unlock_irqrestore(&master->lock, flags);
457}
458
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800459static void fsi_stream_quit(struct fsi_priv *fsi, int is_play)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800460{
461 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
462 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
463 struct fsi_master *master = fsi_get_master(fsi);
464 unsigned long flags;
465
466 spin_lock_irqsave(&master->lock, flags);
467
468 if (io->oerr_num > 0)
469 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
470
471 if (io->uerr_num > 0)
472 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
473
474 io->substream = NULL;
475 io->buff_sample_capa = 0;
476 io->buff_sample_pos = 0;
477 io->period_samples = 0;
478 io->period_pos = 0;
479 io->sample_width = 0;
480 io->oerr_num = 0;
481 io->uerr_num = 0;
482 spin_unlock_irqrestore(&master->lock, flags);
483}
484
485/*
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800486 * pio function
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900487 */
488
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800489static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900490{
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900491 struct snd_pcm_runtime *runtime = io->substream->runtime;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900492
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900493 return runtime->dma_area +
494 samples_to_bytes(runtime, io->buff_sample_pos);
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900495}
496
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800497static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900498{
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800499 u16 *start = (u16 *)_buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900500 int i;
501
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900502 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900503 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
504}
505
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800506static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900507{
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800508 u16 *start = (u16 *)_buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900509 int i;
510
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900511 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900512 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
513}
514
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800515static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900516{
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800517 u32 *start = (u32 *)_buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900518 int i;
519
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900520 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900521 fsi_reg_write(fsi, DODT, *(start + i));
522}
523
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800524static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900525{
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800526 u32 *start = (u32 *)_buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900527 int i;
528
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900529 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900530 *(start + i) = fsi_reg_read(fsi, DIDT);
531}
532
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900533/*
534 * irq function
535 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900536
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900537static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
538{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900539 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900540 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900541
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900542 fsi_core_mask_set(master, imsk, data, data);
543 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900544}
545
546static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
547{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900548 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900549 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900550
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900551 fsi_core_mask_set(master, imsk, data, 0);
552 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900553}
554
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900555static u32 fsi_irq_get_status(struct fsi_master *master)
556{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900557 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900558}
559
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900560static void fsi_irq_clear_status(struct fsi_priv *fsi)
561{
562 u32 data = 0;
563 struct fsi_master *master = fsi_get_master(fsi);
564
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900565 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
566 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900567
568 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900569 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900570}
571
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900572/*
573 * SPDIF master clock function
574 *
575 * These functions are used later FSI2
576 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900577static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
578{
579 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900580 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900581
582 if (master->core->ver < 2) {
583 pr_err("fsi: register access err (%s)\n", __func__);
584 return;
585 }
586
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900587 mask = BP | SE;
588 val = enable ? mask : 0;
589
590 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900591 fsi_core_mask_set(master, a_mclk, mask, val) :
592 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900593}
594
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900595/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900596 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900597 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900598static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
599 long rate, int enable)
600{
601 struct fsi_master *master = fsi_get_master(fsi);
602 set_rate_func set_rate = fsi_get_info_set_rate(master);
603 int fsi_ver = master->core->ver;
604 int ret;
605
606 ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
607 if (ret < 0) /* error */
608 return ret;
609
610 if (!enable)
611 return 0;
612
613 if (ret > 0) {
614 u32 data = 0;
615
616 switch (ret & SH_FSI_ACKMD_MASK) {
617 default:
618 /* FALL THROUGH */
619 case SH_FSI_ACKMD_512:
620 data |= (0x0 << 12);
621 break;
622 case SH_FSI_ACKMD_256:
623 data |= (0x1 << 12);
624 break;
625 case SH_FSI_ACKMD_128:
626 data |= (0x2 << 12);
627 break;
628 case SH_FSI_ACKMD_64:
629 data |= (0x3 << 12);
630 break;
631 case SH_FSI_ACKMD_32:
632 if (fsi_ver < 2)
633 dev_err(dev, "unsupported ACKMD\n");
634 else
635 data |= (0x4 << 12);
636 break;
637 }
638
639 switch (ret & SH_FSI_BPFMD_MASK) {
640 default:
641 /* FALL THROUGH */
642 case SH_FSI_BPFMD_32:
643 data |= (0x0 << 8);
644 break;
645 case SH_FSI_BPFMD_64:
646 data |= (0x1 << 8);
647 break;
648 case SH_FSI_BPFMD_128:
649 data |= (0x2 << 8);
650 break;
651 case SH_FSI_BPFMD_256:
652 data |= (0x3 << 8);
653 break;
654 case SH_FSI_BPFMD_512:
655 data |= (0x4 << 8);
656 break;
657 case SH_FSI_BPFMD_16:
658 if (fsi_ver < 2)
659 dev_err(dev, "unsupported ACKMD\n");
660 else
661 data |= (0x7 << 8);
662 break;
663 }
664
665 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
666 udelay(10);
667 ret = 0;
668 }
669
670 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900671}
672
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900673#define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
674#define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
675static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900676{
677 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900678 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900679
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900680 if (enable)
681 fsi_irq_enable(fsi, is_play);
682 else
683 fsi_irq_disable(fsi, is_play);
684
Kuninori Morimotocda828c2011-05-23 20:46:35 +0900685 if (fsi_is_clk_master(fsi))
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900686 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
687}
688
689/*
690 * ctrl function
691 */
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800692static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800693 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
694 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
695 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900696{
697 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800698 struct snd_pcm_substream *substream;
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800699 u8 *buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900700 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900701
702 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900703 !io->substream ||
704 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900705 return -EINVAL;
706
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900707 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900708 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900709 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900710
711 /* FSI FIFO has limit.
712 * So, this driver can not send periods data at a time
713 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900714 if (io->buff_sample_pos >=
715 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900716
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900717 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900718 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900719
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900720 if (0 == io->period_pos)
721 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900722 }
723
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800724 buf = fsi_pio_get_area(fsi, io);
725
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800726 switch (io->sample_width) {
727 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800728 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800729 break;
730 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800731 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800732 break;
733 default:
734 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900735 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900736
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900737 /* update buff_sample_pos */
738 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900739
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900740 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900741 snd_pcm_period_elapsed(substream);
742
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900743 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900744}
745
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900746static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900747{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800748 int is_play = 0;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800749 int sample_residues; /* samples in FSI fifo */
750 int sample_space; /* ALSA free samples space */
751 int samples;
752 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
753
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800754 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800755 sample_space = io->buff_sample_capa - io->buff_sample_pos;
756
757 samples = min(sample_residues, sample_space);
758
759 return fsi_fifo_data_ctrl(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800760 fsi_pio_pop16,
761 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800762 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900763}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900764
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900765static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900766{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800767 int is_play = 1;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800768 int sample_residues; /* ALSA residue samples */
769 int sample_space; /* FSI fifo free samples space */
770 int samples;
771 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
772
773 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
774 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800775 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800776
777 samples = min(sample_residues, sample_space);
778
779 return fsi_fifo_data_ctrl(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800780 fsi_pio_push16,
781 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800782 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900783}
784
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900785static irqreturn_t fsi_interrupt(int irq, void *data)
786{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900787 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900788 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900789
790 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900791 fsi_master_mask_set(master, SOFT_RST, IR, 0);
792 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900793
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900794 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900795 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900796 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900797 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900798 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900799 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900800 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900801 fsi_data_pop(&master->fsib);
802
803 fsi_count_fifo_err(&master->fsia);
804 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900805
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900806 fsi_irq_clear_status(&master->fsia);
807 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900808
809 return IRQ_HANDLED;
810}
811
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900812/*
813 * dai ops
814 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -0800815static void fsi_fifo_init(struct fsi_priv *fsi,
816 int is_play,
817 struct device *dev)
818{
819 struct fsi_master *master = fsi_get_master(fsi);
820 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
821 u32 shift, i;
822 int frame_capa;
823
824 /* get on-chip RAM capacity */
825 shift = fsi_master_read(master, FIFO_SZ);
826 shift >>= fsi_get_port_shift(fsi, is_play);
827 shift &= FIFO_SZ_MASK;
828 frame_capa = 256 << shift;
829 dev_dbg(dev, "fifo = %d words\n", frame_capa);
830
831 /*
832 * The maximum number of sample data varies depending
833 * on the number of channels selected for the format.
834 *
835 * FIFOs are used in 4-channel units in 3-channel mode
836 * and in 8-channel units in 5- to 7-channel mode
837 * meaning that more FIFOs than the required size of DPRAM
838 * are used.
839 *
840 * ex) if 256 words of DP-RAM is connected
841 * 1 channel: 256 (256 x 1 = 256)
842 * 2 channels: 128 (128 x 2 = 256)
843 * 3 channels: 64 ( 64 x 3 = 192)
844 * 4 channels: 64 ( 64 x 4 = 256)
845 * 5 channels: 32 ( 32 x 5 = 160)
846 * 6 channels: 32 ( 32 x 6 = 192)
847 * 7 channels: 32 ( 32 x 7 = 224)
848 * 8 channels: 32 ( 32 x 8 = 256)
849 */
850 for (i = 1; i < fsi->chan_num; i <<= 1)
851 frame_capa >>= 1;
852 dev_dbg(dev, "%d channel %d store\n",
853 fsi->chan_num, frame_capa);
854
855 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
856
857 /*
858 * set interrupt generation factor
859 * clear FIFO
860 */
861 if (is_play) {
862 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
863 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
864 } else {
865 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
866 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
867 }
868}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900869
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900870static int fsi_hw_startup(struct fsi_priv *fsi,
871 int is_play,
872 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900873{
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800874 struct fsi_master *master = fsi_get_master(fsi);
875 int fsi_ver = master->core->ver;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900876 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900877 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900878
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900879 /* clock setting */
880 if (fsi_is_clk_master(fsi))
881 data = DIMD | DOMD;
882
883 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900884
885 /* clock inversion (CKG2) */
886 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900887 if (SH_FSI_LRM_INV & flags)
888 data |= 1 << 12;
889 if (SH_FSI_BRM_INV & flags)
890 data |= 1 << 8;
891 if (SH_FSI_LRS_INV & flags)
892 data |= 1 << 4;
893 if (SH_FSI_BRS_INV & flags)
894 data |= 1 << 0;
895
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900896 fsi_reg_write(fsi, CKG2, data);
897
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900898 /* set format */
899 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
900 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
901
902 /* spdif ? */
903 if (fsi_is_spdif(fsi)) {
904 fsi_spdif_clk_ctrl(fsi, 1);
905 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
906 }
907
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800908 /*
909 * FIXME
910 *
911 * FSI driver assumed that data package is in-back.
912 * FSI2 chip can select it.
913 */
914 if (fsi_ver >= 2) {
915 fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
916 fsi_reg_write(fsi, IN_DMAC, (1 << 4));
917 }
918
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900919 /* irq clear */
920 fsi_irq_disable(fsi, is_play);
921 fsi_irq_clear_status(fsi);
922
923 /* fifo init */
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900924 fsi_fifo_init(fsi, is_play, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900925
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900926 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900927}
928
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900929static void fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900930 struct device *dev)
931{
932 if (fsi_is_clk_master(fsi))
933 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900934}
935
936static int fsi_dai_startup(struct snd_pcm_substream *substream,
937 struct snd_soc_dai *dai)
938{
939 struct fsi_priv *fsi = fsi_get_priv(substream);
940 int is_play = fsi_is_play(substream);
941
942 return fsi_hw_startup(fsi, is_play, dai->dev);
943}
944
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900945static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
946 struct snd_soc_dai *dai)
947{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900948 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900949
Kuninori Morimoto41bba152012-02-03 00:51:53 -0800950 fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000951 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900952}
953
954static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
955 struct snd_soc_dai *dai)
956{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900957 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900958 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900959 int ret = 0;
960
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900961 switch (cmd) {
962 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800963 fsi_stream_init(fsi, is_play, substream);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900964 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900965 fsi_port_start(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900966 break;
967 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900968 fsi_port_stop(fsi, is_play);
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800969 fsi_stream_quit(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900970 break;
971 }
972
973 return ret;
974}
975
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900976static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
977{
978 u32 data = 0;
979
980 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
981 case SND_SOC_DAIFMT_I2S:
982 data = CR_I2S;
983 fsi->chan_num = 2;
984 break;
985 case SND_SOC_DAIFMT_LEFT_J:
986 data = CR_PCM;
987 fsi->chan_num = 2;
988 break;
989 default:
990 return -EINVAL;
991 }
992
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900993 fsi->do_fmt = data;
994 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900995
996 return 0;
997}
998
999static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1000{
1001 struct fsi_master *master = fsi_get_master(fsi);
1002 u32 data = 0;
1003
1004 if (master->core->ver < 2)
1005 return -EINVAL;
1006
1007 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1008 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001009 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001010
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001011 fsi->do_fmt = data;
1012 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001013
1014 return 0;
1015}
1016
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001017static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1018{
1019 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001020 struct fsi_master *master = fsi_get_master(fsi);
1021 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001022 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001023 int ret;
1024
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001025 /* set master/slave audio interface */
1026 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1027 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001028 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001029 break;
1030 case SND_SOC_DAIFMT_CBS_CFS:
1031 break;
1032 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001033 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001034 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001035
1036 if (fsi_is_clk_master(fsi) && !set_rate) {
1037 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001038 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001039 }
1040
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001041 /* set format */
1042 switch (flags & SH_FSI_FMT_MASK) {
1043 case SH_FSI_FMT_DAI:
1044 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1045 break;
1046 case SH_FSI_FMT_SPDIF:
1047 ret = fsi_set_fmt_spdif(fsi);
1048 break;
1049 default:
1050 ret = -EINVAL;
1051 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001052
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001053 return ret;
1054}
1055
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001056static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1057 struct snd_pcm_hw_params *params,
1058 struct snd_soc_dai *dai)
1059{
1060 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001061 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001062 int ret;
1063
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001064 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001065 return 0;
1066
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001067 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1068 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001069 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001070
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001071 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001072
1073 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001074}
1075
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001076static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001077 .startup = fsi_dai_startup,
1078 .shutdown = fsi_dai_shutdown,
1079 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001080 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001081 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001082};
1083
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001084/*
1085 * pcm ops
1086 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001087
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001088static struct snd_pcm_hardware fsi_pcm_hardware = {
1089 .info = SNDRV_PCM_INFO_INTERLEAVED |
1090 SNDRV_PCM_INFO_MMAP |
1091 SNDRV_PCM_INFO_MMAP_VALID |
1092 SNDRV_PCM_INFO_PAUSE,
1093 .formats = FSI_FMTS,
1094 .rates = FSI_RATES,
1095 .rate_min = 8000,
1096 .rate_max = 192000,
1097 .channels_min = 1,
1098 .channels_max = 2,
1099 .buffer_bytes_max = 64 * 1024,
1100 .period_bytes_min = 32,
1101 .period_bytes_max = 8192,
1102 .periods_min = 1,
1103 .periods_max = 32,
1104 .fifo_size = 256,
1105};
1106
1107static int fsi_pcm_open(struct snd_pcm_substream *substream)
1108{
1109 struct snd_pcm_runtime *runtime = substream->runtime;
1110 int ret = 0;
1111
1112 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1113
1114 ret = snd_pcm_hw_constraint_integer(runtime,
1115 SNDRV_PCM_HW_PARAM_PERIODS);
1116
1117 return ret;
1118}
1119
1120static int fsi_hw_params(struct snd_pcm_substream *substream,
1121 struct snd_pcm_hw_params *hw_params)
1122{
1123 return snd_pcm_lib_malloc_pages(substream,
1124 params_buffer_bytes(hw_params));
1125}
1126
1127static int fsi_hw_free(struct snd_pcm_substream *substream)
1128{
1129 return snd_pcm_lib_free_pages(substream);
1130}
1131
1132static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1133{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001134 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -08001135 struct fsi_stream *io = fsi_stream_get(fsi, fsi_is_play(substream));
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001136 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001137
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001138 if (samples_pos < 0)
1139 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001140
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001141 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001142}
1143
1144static struct snd_pcm_ops fsi_pcm_ops = {
1145 .open = fsi_pcm_open,
1146 .ioctl = snd_pcm_lib_ioctl,
1147 .hw_params = fsi_hw_params,
1148 .hw_free = fsi_hw_free,
1149 .pointer = fsi_pointer,
1150};
1151
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001152/*
1153 * snd_soc_platform
1154 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001155
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001156#define PREALLOC_BUFFER (32 * 1024)
1157#define PREALLOC_BUFFER_MAX (32 * 1024)
1158
1159static void fsi_pcm_free(struct snd_pcm *pcm)
1160{
1161 snd_pcm_lib_preallocate_free_for_all(pcm);
1162}
1163
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001164static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001165{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001166 struct snd_pcm *pcm = rtd->pcm;
1167
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001168 /*
1169 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1170 * in MMAP mode (i.e. aplay -M)
1171 */
1172 return snd_pcm_lib_preallocate_pages_for_all(
1173 pcm,
1174 SNDRV_DMA_TYPE_CONTINUOUS,
1175 snd_dma_continuous_data(GFP_KERNEL),
1176 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1177}
1178
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001179/*
1180 * alsa struct
1181 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001183static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001184 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001185 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001186 .playback = {
1187 .rates = FSI_RATES,
1188 .formats = FSI_FMTS,
1189 .channels_min = 1,
1190 .channels_max = 8,
1191 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001192 .capture = {
1193 .rates = FSI_RATES,
1194 .formats = FSI_FMTS,
1195 .channels_min = 1,
1196 .channels_max = 8,
1197 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001198 .ops = &fsi_dai_ops,
1199 },
1200 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001201 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001202 .playback = {
1203 .rates = FSI_RATES,
1204 .formats = FSI_FMTS,
1205 .channels_min = 1,
1206 .channels_max = 8,
1207 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001208 .capture = {
1209 .rates = FSI_RATES,
1210 .formats = FSI_FMTS,
1211 .channels_min = 1,
1212 .channels_max = 8,
1213 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001214 .ops = &fsi_dai_ops,
1215 },
1216};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001217
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001218static struct snd_soc_platform_driver fsi_soc_platform = {
1219 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001220 .pcm_new = fsi_pcm_new,
1221 .pcm_free = fsi_pcm_free,
1222};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001223
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001224/*
1225 * platform function
1226 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001227
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001228static int fsi_probe(struct platform_device *pdev)
1229{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001230 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001231 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001232 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001233 unsigned int irq;
1234 int ret;
1235
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001236 id_entry = pdev->id_entry;
1237 if (!id_entry) {
1238 dev_err(&pdev->dev, "unknown fsi device\n");
1239 return -ENODEV;
1240 }
1241
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001242 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1243 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001244 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001245 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1246 ret = -ENODEV;
1247 goto exit;
1248 }
1249
1250 master = kzalloc(sizeof(*master), GFP_KERNEL);
1251 if (!master) {
1252 dev_err(&pdev->dev, "Could not allocate master\n");
1253 ret = -ENOMEM;
1254 goto exit;
1255 }
1256
1257 master->base = ioremap_nocache(res->start, resource_size(res));
1258 if (!master->base) {
1259 ret = -ENXIO;
1260 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1261 goto exit_kfree;
1262 }
1263
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001264 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001265 master->irq = irq;
1266 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001267 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001268 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001269
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001270 /* FSI A setting */
1271 master->fsia.base = master->base;
1272 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001273
1274 /* FSI B setting */
1275 master->fsib.base = master->base + 0x40;
1276 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001277
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001278 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001279 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001280
Yong Zhang88e24c32011-09-22 16:59:20 +08001281 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001282 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001283 if (ret) {
1284 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001285 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001286 }
1287
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001288 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001289 if (ret < 0) {
1290 dev_err(&pdev->dev, "cannot snd soc register\n");
1291 goto exit_free_irq;
1292 }
1293
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001294 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1295 ARRAY_SIZE(fsi_soc_dai));
1296 if (ret < 0) {
1297 dev_err(&pdev->dev, "cannot snd dai register\n");
1298 goto exit_snd_soc;
1299 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001300
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001301 return ret;
1302
1303exit_snd_soc:
1304 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001305exit_free_irq:
1306 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001307exit_iounmap:
1308 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001309 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001310exit_kfree:
1311 kfree(master);
1312 master = NULL;
1313exit:
1314 return ret;
1315}
1316
1317static int fsi_remove(struct platform_device *pdev)
1318{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001319 struct fsi_master *master;
1320
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001321 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001322
Kuninori Morimotod985f272011-04-08 15:09:25 +09001323 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001324 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001325
Kuninori Morimotod985f272011-04-08 15:09:25 +09001326 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1327 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001328
1329 iounmap(master->base);
1330 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001331
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001332 return 0;
1333}
1334
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001335static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001336 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001337 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001338{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001339 if (!fsi_stream_is_working(fsi, is_play))
1340 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001341
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001342 fsi_port_stop(fsi, is_play);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001343 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001344}
1345
1346static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001347 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001348 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001349{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001350 if (!fsi_stream_is_working(fsi, is_play))
1351 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001352
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001353 fsi_hw_startup(fsi, is_play, dev);
1354
1355 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001356 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001357
1358 fsi_port_start(fsi, is_play);
1359
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001360}
1361
1362static int fsi_suspend(struct device *dev)
1363{
1364 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001365 struct fsi_priv *fsia = &master->fsia;
1366 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001367
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001368 __fsi_suspend(fsia, 1, dev);
1369 __fsi_suspend(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001370
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001371 __fsi_suspend(fsib, 1, dev);
1372 __fsi_suspend(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001373
1374 return 0;
1375}
1376
1377static int fsi_resume(struct device *dev)
1378{
1379 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001380 struct fsi_priv *fsia = &master->fsia;
1381 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001382
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001383 __fsi_resume(fsia, 1, dev);
1384 __fsi_resume(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001385
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001386 __fsi_resume(fsib, 1, dev);
1387 __fsi_resume(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001388
1389 return 0;
1390}
1391
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001392static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001393 .suspend = fsi_suspend,
1394 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001395};
1396
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001397static struct fsi_core fsi1_core = {
1398 .ver = 1,
1399
1400 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001401 .int_st = INT_ST,
1402 .iemsk = IEMSK,
1403 .imsk = IMSK,
1404};
1405
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001406static struct fsi_core fsi2_core = {
1407 .ver = 2,
1408
1409 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001410 .int_st = CPU_INT_ST,
1411 .iemsk = CPU_IEMSK,
1412 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001413 .a_mclk = A_MST_CTLR,
1414 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001415};
1416
1417static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001418 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1419 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001420 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001421};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001422MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001423
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001424static struct platform_driver fsi_driver = {
1425 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001426 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001427 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001428 },
1429 .probe = fsi_probe,
1430 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001431 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001432};
1433
Axel Lincb5e8732011-11-25 10:15:07 +08001434module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001435
1436MODULE_LICENSE("GPL");
1437MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1438MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001439MODULE_ALIAS("platform:fsi-pcm-audio");