blob: cbb5643794b38aaff12f969ee96c9441f2a373dd [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 Morimotoc8fe2572010-09-17 13:48:17 +0900213/*
214 * basic read write function
215 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900216
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200217static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900218{
219 /* valid data area is 24bit */
220 data &= 0x00ffffff;
221
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100222 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900223}
224
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200225static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900226{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100227 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900228}
229
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200230static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900231{
232 u32 val = __fsi_reg_read(reg);
233
234 val &= ~mask;
235 val |= data & mask;
236
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100237 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900238}
239
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900240#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700241 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900242
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900243#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700244 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900245
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900246#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700247 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900248
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900249#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
250#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
251static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900252{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900253 u32 ret;
254 unsigned long flags;
255
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900256 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200257 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900258 spin_unlock_irqrestore(&master->lock, flags);
259
260 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900261}
262
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900263#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
264#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
265static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900266 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900267{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900268 unsigned long flags;
269
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900270 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200271 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900272 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900273}
274
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900275/*
276 * basic function
277 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900278
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900279static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900280{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900281 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900282}
283
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900284static int fsi_is_clk_master(struct fsi_priv *fsi)
285{
286 return fsi->clk_master;
287}
288
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900289static int fsi_is_port_a(struct fsi_priv *fsi)
290{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900291 return fsi->master->base == fsi->base;
292}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900293
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900294static int fsi_is_spdif(struct fsi_priv *fsi)
295{
296 return fsi->spdif;
297}
298
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900299static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900300{
301 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900302
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000303 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900304}
305
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900306static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900307{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000308 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900309
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000310 if (dai->id == 0)
311 return &master->fsia;
312 else
313 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900314}
315
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900316static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
317{
318 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
319}
320
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900321static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
322{
323 if (!master->info)
324 return NULL;
325
326 return master->info->set_rate;
327}
328
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900329static u32 fsi_get_info_flags(struct fsi_priv *fsi)
330{
331 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900332 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900333
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900334 if (!master->info)
335 return 0;
336
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900337 return is_porta ? master->info->porta_flags :
338 master->info->portb_flags;
339}
340
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900341static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900342{
343 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900344 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900345
346 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900347 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900348 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900349 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900350
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900351 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900352}
353
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900354static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
355{
356 return frames * fsi->chan_num;
357}
358
359static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
360{
361 return samples / fsi->chan_num;
362}
363
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900364static int fsi_get_current_fifo_samples(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900365{
366 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900367 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900368
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900369 status = is_play ?
370 fsi_reg_read(fsi, DOFF_ST) :
371 fsi_reg_read(fsi, DIFF_ST);
372
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900373 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900374
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900375 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900376}
377
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900378static void fsi_count_fifo_err(struct fsi_priv *fsi)
379{
380 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
381 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
382
383 if (ostatus & ERR_OVER)
384 fsi->playback.oerr_num++;
385
386 if (ostatus & ERR_UNDER)
387 fsi->playback.uerr_num++;
388
389 if (istatus & ERR_OVER)
390 fsi->capture.oerr_num++;
391
392 if (istatus & ERR_UNDER)
393 fsi->capture.uerr_num++;
394
395 fsi_reg_write(fsi, DOFF_ST, 0);
396 fsi_reg_write(fsi, DIFF_ST, 0);
397}
398
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900399/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800400 * fsi_stream_xx() function
401 */
402#define fsi_is_play(substream) fsi_stream_is_play(substream->stream)
403static inline int fsi_stream_is_play(int stream)
404{
405 return stream == SNDRV_PCM_STREAM_PLAYBACK;
406}
407
408static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
409 int is_play)
410{
411 return is_play ? &fsi->playback : &fsi->capture;
412}
413
414static int fsi_stream_is_working(struct fsi_priv *fsi,
415 int is_play)
416{
417 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
418 struct fsi_master *master = fsi_get_master(fsi);
419 unsigned long flags;
420 int ret;
421
422 spin_lock_irqsave(&master->lock, flags);
423 ret = !!io->substream;
424 spin_unlock_irqrestore(&master->lock, flags);
425
426 return ret;
427}
428
429static void fsi_stream_push(struct fsi_priv *fsi,
430 int is_play,
431 struct snd_pcm_substream *substream)
432{
433 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
434 struct snd_pcm_runtime *runtime = substream->runtime;
435 struct fsi_master *master = fsi_get_master(fsi);
436 unsigned long flags;
437
438 spin_lock_irqsave(&master->lock, flags);
439 io->substream = substream;
440 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
441 io->buff_sample_pos = 0;
442 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
443 io->period_pos = 0;
444 io->sample_width = samples_to_bytes(runtime, 1);
445 io->oerr_num = -1; /* ignore 1st err */
446 io->uerr_num = -1; /* ignore 1st err */
447 spin_unlock_irqrestore(&master->lock, flags);
448}
449
450static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
451{
452 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
453 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
454 struct fsi_master *master = fsi_get_master(fsi);
455 unsigned long flags;
456
457 spin_lock_irqsave(&master->lock, flags);
458
459 if (io->oerr_num > 0)
460 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
461
462 if (io->uerr_num > 0)
463 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
464
465 io->substream = NULL;
466 io->buff_sample_capa = 0;
467 io->buff_sample_pos = 0;
468 io->period_samples = 0;
469 io->period_pos = 0;
470 io->sample_width = 0;
471 io->oerr_num = 0;
472 io->uerr_num = 0;
473 spin_unlock_irqrestore(&master->lock, flags);
474}
475
476/*
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900477 * dma function
478 */
479
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900480static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900481{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900482 int is_play = fsi_stream_is_play(stream);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800483 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900484 struct snd_pcm_runtime *runtime = io->substream->runtime;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900485
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900486 return runtime->dma_area +
487 samples_to_bytes(runtime, io->buff_sample_pos);
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900488}
489
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900490static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900491{
492 u16 *start;
493 int i;
494
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900495 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900496
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900497 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900498 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
499}
500
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900501static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900502{
503 u16 *start;
504 int i;
505
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900506 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
507
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900508
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900509 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900510 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
511}
512
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900513static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900514{
515 u32 *start;
516 int i;
517
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900518 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
519
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900520
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900521 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900522 fsi_reg_write(fsi, DODT, *(start + i));
523}
524
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900525static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900526{
527 u32 *start;
528 int i;
529
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900530 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900531
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900532 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900533 *(start + i) = fsi_reg_read(fsi, DIDT);
534}
535
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900536/*
537 * irq function
538 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900539
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900540static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
541{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900542 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900543 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900544
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900545 fsi_core_mask_set(master, imsk, data, data);
546 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900547}
548
549static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
550{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900551 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900552 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900553
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900554 fsi_core_mask_set(master, imsk, data, 0);
555 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900556}
557
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900558static u32 fsi_irq_get_status(struct fsi_master *master)
559{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900560 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900561}
562
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900563static void fsi_irq_clear_status(struct fsi_priv *fsi)
564{
565 u32 data = 0;
566 struct fsi_master *master = fsi_get_master(fsi);
567
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900568 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
569 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900570
571 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900572 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900573}
574
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900575/*
576 * SPDIF master clock function
577 *
578 * These functions are used later FSI2
579 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900580static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
581{
582 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900583 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900584
585 if (master->core->ver < 2) {
586 pr_err("fsi: register access err (%s)\n", __func__);
587 return;
588 }
589
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900590 mask = BP | SE;
591 val = enable ? mask : 0;
592
593 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900594 fsi_core_mask_set(master, a_mclk, mask, val) :
595 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900596}
597
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900598/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900599 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900600 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900601static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
602 long rate, int enable)
603{
604 struct fsi_master *master = fsi_get_master(fsi);
605 set_rate_func set_rate = fsi_get_info_set_rate(master);
606 int fsi_ver = master->core->ver;
607 int ret;
608
609 ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
610 if (ret < 0) /* error */
611 return ret;
612
613 if (!enable)
614 return 0;
615
616 if (ret > 0) {
617 u32 data = 0;
618
619 switch (ret & SH_FSI_ACKMD_MASK) {
620 default:
621 /* FALL THROUGH */
622 case SH_FSI_ACKMD_512:
623 data |= (0x0 << 12);
624 break;
625 case SH_FSI_ACKMD_256:
626 data |= (0x1 << 12);
627 break;
628 case SH_FSI_ACKMD_128:
629 data |= (0x2 << 12);
630 break;
631 case SH_FSI_ACKMD_64:
632 data |= (0x3 << 12);
633 break;
634 case SH_FSI_ACKMD_32:
635 if (fsi_ver < 2)
636 dev_err(dev, "unsupported ACKMD\n");
637 else
638 data |= (0x4 << 12);
639 break;
640 }
641
642 switch (ret & SH_FSI_BPFMD_MASK) {
643 default:
644 /* FALL THROUGH */
645 case SH_FSI_BPFMD_32:
646 data |= (0x0 << 8);
647 break;
648 case SH_FSI_BPFMD_64:
649 data |= (0x1 << 8);
650 break;
651 case SH_FSI_BPFMD_128:
652 data |= (0x2 << 8);
653 break;
654 case SH_FSI_BPFMD_256:
655 data |= (0x3 << 8);
656 break;
657 case SH_FSI_BPFMD_512:
658 data |= (0x4 << 8);
659 break;
660 case SH_FSI_BPFMD_16:
661 if (fsi_ver < 2)
662 dev_err(dev, "unsupported ACKMD\n");
663 else
664 data |= (0x7 << 8);
665 break;
666 }
667
668 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
669 udelay(10);
670 ret = 0;
671 }
672
673 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900674}
675
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900676#define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
677#define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
678static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900679{
680 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900681 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900682
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900683 if (enable)
684 fsi_irq_enable(fsi, is_play);
685 else
686 fsi_irq_disable(fsi, is_play);
687
Kuninori Morimotocda828c2011-05-23 20:46:35 +0900688 if (fsi_is_clk_master(fsi))
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900689 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
690}
691
692/*
693 * ctrl function
694 */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900695static void fsi_fifo_init(struct fsi_priv *fsi,
696 int is_play,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900697 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900698{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900699 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800700 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900701 u32 shift, i;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900702 int frame_capa;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900703
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900704 /* get on-chip RAM capacity */
705 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900706 shift >>= fsi_get_port_shift(fsi, is_play);
707 shift &= FIFO_SZ_MASK;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900708 frame_capa = 256 << shift;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900709 dev_dbg(dev, "fifo = %d words\n", frame_capa);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900710
711 /*
712 * The maximum number of sample data varies depending
713 * on the number of channels selected for the format.
714 *
715 * FIFOs are used in 4-channel units in 3-channel mode
716 * and in 8-channel units in 5- to 7-channel mode
717 * meaning that more FIFOs than the required size of DPRAM
718 * are used.
719 *
720 * ex) if 256 words of DP-RAM is connected
721 * 1 channel: 256 (256 x 1 = 256)
722 * 2 channels: 128 (128 x 2 = 256)
723 * 3 channels: 64 ( 64 x 3 = 192)
724 * 4 channels: 64 ( 64 x 4 = 256)
725 * 5 channels: 32 ( 32 x 5 = 160)
726 * 6 channels: 32 ( 32 x 6 = 192)
727 * 7 channels: 32 ( 32 x 7 = 224)
728 * 8 channels: 32 ( 32 x 8 = 256)
729 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900730 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900731 frame_capa >>= 1;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900732 dev_dbg(dev, "%d channel %d store\n",
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900733 fsi->chan_num, frame_capa);
734
735 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900736
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900737 /*
738 * set interrupt generation factor
739 * clear FIFO
740 */
741 if (is_play) {
742 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
743 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
744 } else {
745 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
746 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
747 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900748}
749
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800750static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, struct fsi_stream *io,
751 void (*run16)(struct fsi_priv *fsi, int size),
752 void (*run32)(struct fsi_priv *fsi, int size),
753 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900754{
755 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800756 struct snd_pcm_substream *substream;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900757 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900758
759 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900760 !io->substream ||
761 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900762 return -EINVAL;
763
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900764 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900765 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900766 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900767
768 /* FSI FIFO has limit.
769 * So, this driver can not send periods data at a time
770 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900771 if (io->buff_sample_pos >=
772 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900773
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900774 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900775 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900776
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900777 if (0 == io->period_pos)
778 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900779 }
780
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800781 switch (io->sample_width) {
782 case 2:
783 run16(fsi, samples);
784 break;
785 case 4:
786 run32(fsi, samples);
787 break;
788 default:
789 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900790 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900791
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900792 /* update buff_sample_pos */
793 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900794
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900795 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900796 snd_pcm_period_elapsed(substream);
797
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900798 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900799}
800
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900801static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900802{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800803 int is_play = fsi_stream_is_play(SNDRV_PCM_STREAM_CAPTURE);
804 int sample_residues; /* samples in FSI fifo */
805 int sample_space; /* ALSA free samples space */
806 int samples;
807 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
808
809 sample_residues = fsi_get_current_fifo_samples(fsi, is_play);
810 sample_space = io->buff_sample_capa - io->buff_sample_pos;
811
812 samples = min(sample_residues, sample_space);
813
814 return fsi_fifo_data_ctrl(fsi, io,
815 fsi_dma_soft_pop16,
816 fsi_dma_soft_pop32,
817 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900818}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900819
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900820static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900821{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800822 int is_play = fsi_stream_is_play(SNDRV_PCM_STREAM_PLAYBACK);
823 int sample_residues; /* ALSA residue samples */
824 int sample_space; /* FSI fifo free samples space */
825 int samples;
826 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
827
828 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
829 sample_space = io->fifo_sample_capa -
830 fsi_get_current_fifo_samples(fsi, is_play);
831
832 samples = min(sample_residues, sample_space);
833
834 return fsi_fifo_data_ctrl(fsi, io,
835 fsi_dma_soft_push16,
836 fsi_dma_soft_push32,
837 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900838}
839
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900840static irqreturn_t fsi_interrupt(int irq, void *data)
841{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900842 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900843 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900844
845 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900846 fsi_master_mask_set(master, SOFT_RST, IR, 0);
847 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900848
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900849 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900850 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900851 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900852 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900853 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900854 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900855 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900856 fsi_data_pop(&master->fsib);
857
858 fsi_count_fifo_err(&master->fsia);
859 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900860
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900861 fsi_irq_clear_status(&master->fsia);
862 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900863
864 return IRQ_HANDLED;
865}
866
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900867/*
868 * dai ops
869 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900870
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900871static int fsi_hw_startup(struct fsi_priv *fsi,
872 int is_play,
873 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900874{
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800875 struct fsi_master *master = fsi_get_master(fsi);
876 int fsi_ver = master->core->ver;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900877 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900878 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900879
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900880 /* clock setting */
881 if (fsi_is_clk_master(fsi))
882 data = DIMD | DOMD;
883
884 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900885
886 /* clock inversion (CKG2) */
887 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900888 if (SH_FSI_LRM_INV & flags)
889 data |= 1 << 12;
890 if (SH_FSI_BRM_INV & flags)
891 data |= 1 << 8;
892 if (SH_FSI_LRS_INV & flags)
893 data |= 1 << 4;
894 if (SH_FSI_BRS_INV & flags)
895 data |= 1 << 0;
896
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900897 fsi_reg_write(fsi, CKG2, data);
898
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900899 /* set format */
900 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
901 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
902
903 /* spdif ? */
904 if (fsi_is_spdif(fsi)) {
905 fsi_spdif_clk_ctrl(fsi, 1);
906 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
907 }
908
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800909 /*
910 * FIXME
911 *
912 * FSI driver assumed that data package is in-back.
913 * FSI2 chip can select it.
914 */
915 if (fsi_ver >= 2) {
916 fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
917 fsi_reg_write(fsi, IN_DMAC, (1 << 4));
918 }
919
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900920 /* irq clear */
921 fsi_irq_disable(fsi, is_play);
922 fsi_irq_clear_status(fsi);
923
924 /* fifo init */
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900925 fsi_fifo_init(fsi, is_play, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900926
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900927 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900928}
929
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900930static void fsi_hw_shutdown(struct fsi_priv *fsi,
931 int is_play,
932 struct device *dev)
933{
934 if (fsi_is_clk_master(fsi))
935 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900936}
937
938static int fsi_dai_startup(struct snd_pcm_substream *substream,
939 struct snd_soc_dai *dai)
940{
941 struct fsi_priv *fsi = fsi_get_priv(substream);
942 int is_play = fsi_is_play(substream);
943
944 return fsi_hw_startup(fsi, is_play, dai->dev);
945}
946
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900947static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
948 struct snd_soc_dai *dai)
949{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900950 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900951 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900952
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900953 fsi_hw_shutdown(fsi, is_play, dai->dev);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000954 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900955}
956
957static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
958 struct snd_soc_dai *dai)
959{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900960 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900961 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900962 int ret = 0;
963
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900964 switch (cmd) {
965 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900966 fsi_stream_push(fsi, is_play, substream);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900967 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900968 fsi_port_start(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900969 break;
970 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900971 fsi_port_stop(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900972 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900973 break;
974 }
975
976 return ret;
977}
978
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900979static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
980{
981 u32 data = 0;
982
983 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
984 case SND_SOC_DAIFMT_I2S:
985 data = CR_I2S;
986 fsi->chan_num = 2;
987 break;
988 case SND_SOC_DAIFMT_LEFT_J:
989 data = CR_PCM;
990 fsi->chan_num = 2;
991 break;
992 default:
993 return -EINVAL;
994 }
995
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900996 fsi->do_fmt = data;
997 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900998
999 return 0;
1000}
1001
1002static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1003{
1004 struct fsi_master *master = fsi_get_master(fsi);
1005 u32 data = 0;
1006
1007 if (master->core->ver < 2)
1008 return -EINVAL;
1009
1010 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1011 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001012 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001013
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001014 fsi->do_fmt = data;
1015 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001016
1017 return 0;
1018}
1019
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001020static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1021{
1022 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001023 struct fsi_master *master = fsi_get_master(fsi);
1024 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001025 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001026 int ret;
1027
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001028 /* set master/slave audio interface */
1029 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1030 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001031 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001032 break;
1033 case SND_SOC_DAIFMT_CBS_CFS:
1034 break;
1035 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001036 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001037 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001038
1039 if (fsi_is_clk_master(fsi) && !set_rate) {
1040 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001041 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001042 }
1043
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001044 /* set format */
1045 switch (flags & SH_FSI_FMT_MASK) {
1046 case SH_FSI_FMT_DAI:
1047 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1048 break;
1049 case SH_FSI_FMT_SPDIF:
1050 ret = fsi_set_fmt_spdif(fsi);
1051 break;
1052 default:
1053 ret = -EINVAL;
1054 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001055
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001056 return ret;
1057}
1058
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001059static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1060 struct snd_pcm_hw_params *params,
1061 struct snd_soc_dai *dai)
1062{
1063 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001064 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001065 int ret;
1066
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001067 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001068 return 0;
1069
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001070 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1071 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001072 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001073
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001074 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001075
1076 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001077}
1078
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001079static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001080 .startup = fsi_dai_startup,
1081 .shutdown = fsi_dai_shutdown,
1082 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001083 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001084 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001085};
1086
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001087/*
1088 * pcm ops
1089 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001090
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001091static struct snd_pcm_hardware fsi_pcm_hardware = {
1092 .info = SNDRV_PCM_INFO_INTERLEAVED |
1093 SNDRV_PCM_INFO_MMAP |
1094 SNDRV_PCM_INFO_MMAP_VALID |
1095 SNDRV_PCM_INFO_PAUSE,
1096 .formats = FSI_FMTS,
1097 .rates = FSI_RATES,
1098 .rate_min = 8000,
1099 .rate_max = 192000,
1100 .channels_min = 1,
1101 .channels_max = 2,
1102 .buffer_bytes_max = 64 * 1024,
1103 .period_bytes_min = 32,
1104 .period_bytes_max = 8192,
1105 .periods_min = 1,
1106 .periods_max = 32,
1107 .fifo_size = 256,
1108};
1109
1110static int fsi_pcm_open(struct snd_pcm_substream *substream)
1111{
1112 struct snd_pcm_runtime *runtime = substream->runtime;
1113 int ret = 0;
1114
1115 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1116
1117 ret = snd_pcm_hw_constraint_integer(runtime,
1118 SNDRV_PCM_HW_PARAM_PERIODS);
1119
1120 return ret;
1121}
1122
1123static int fsi_hw_params(struct snd_pcm_substream *substream,
1124 struct snd_pcm_hw_params *hw_params)
1125{
1126 return snd_pcm_lib_malloc_pages(substream,
1127 params_buffer_bytes(hw_params));
1128}
1129
1130static int fsi_hw_free(struct snd_pcm_substream *substream)
1131{
1132 return snd_pcm_lib_free_pages(substream);
1133}
1134
1135static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1136{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001137 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -08001138 struct fsi_stream *io = fsi_stream_get(fsi, fsi_is_play(substream));
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001139 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001140
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001141 if (samples_pos < 0)
1142 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001143
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001144 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001145}
1146
1147static struct snd_pcm_ops fsi_pcm_ops = {
1148 .open = fsi_pcm_open,
1149 .ioctl = snd_pcm_lib_ioctl,
1150 .hw_params = fsi_hw_params,
1151 .hw_free = fsi_hw_free,
1152 .pointer = fsi_pointer,
1153};
1154
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001155/*
1156 * snd_soc_platform
1157 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001158
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001159#define PREALLOC_BUFFER (32 * 1024)
1160#define PREALLOC_BUFFER_MAX (32 * 1024)
1161
1162static void fsi_pcm_free(struct snd_pcm *pcm)
1163{
1164 snd_pcm_lib_preallocate_free_for_all(pcm);
1165}
1166
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001167static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001168{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001169 struct snd_pcm *pcm = rtd->pcm;
1170
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001171 /*
1172 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1173 * in MMAP mode (i.e. aplay -M)
1174 */
1175 return snd_pcm_lib_preallocate_pages_for_all(
1176 pcm,
1177 SNDRV_DMA_TYPE_CONTINUOUS,
1178 snd_dma_continuous_data(GFP_KERNEL),
1179 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1180}
1181
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001182/*
1183 * alsa struct
1184 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001185
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001186static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001187 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001189 .playback = {
1190 .rates = FSI_RATES,
1191 .formats = FSI_FMTS,
1192 .channels_min = 1,
1193 .channels_max = 8,
1194 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001195 .capture = {
1196 .rates = FSI_RATES,
1197 .formats = FSI_FMTS,
1198 .channels_min = 1,
1199 .channels_max = 8,
1200 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001201 .ops = &fsi_dai_ops,
1202 },
1203 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204 .name = "fsib-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};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001220
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001221static struct snd_soc_platform_driver fsi_soc_platform = {
1222 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001223 .pcm_new = fsi_pcm_new,
1224 .pcm_free = fsi_pcm_free,
1225};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001226
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001227/*
1228 * platform function
1229 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001230
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001231static int fsi_probe(struct platform_device *pdev)
1232{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001233 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001234 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001235 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001236 unsigned int irq;
1237 int ret;
1238
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001239 id_entry = pdev->id_entry;
1240 if (!id_entry) {
1241 dev_err(&pdev->dev, "unknown fsi device\n");
1242 return -ENODEV;
1243 }
1244
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001245 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1246 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001247 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001248 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1249 ret = -ENODEV;
1250 goto exit;
1251 }
1252
1253 master = kzalloc(sizeof(*master), GFP_KERNEL);
1254 if (!master) {
1255 dev_err(&pdev->dev, "Could not allocate master\n");
1256 ret = -ENOMEM;
1257 goto exit;
1258 }
1259
1260 master->base = ioremap_nocache(res->start, resource_size(res));
1261 if (!master->base) {
1262 ret = -ENXIO;
1263 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1264 goto exit_kfree;
1265 }
1266
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001267 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001268 master->irq = irq;
1269 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001270 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001271 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001272
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001273 /* FSI A setting */
1274 master->fsia.base = master->base;
1275 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001276
1277 /* FSI B setting */
1278 master->fsib.base = master->base + 0x40;
1279 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001280
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001281 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001282 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001283
Yong Zhang88e24c32011-09-22 16:59:20 +08001284 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001285 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001286 if (ret) {
1287 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001288 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001289 }
1290
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001291 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001292 if (ret < 0) {
1293 dev_err(&pdev->dev, "cannot snd soc register\n");
1294 goto exit_free_irq;
1295 }
1296
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001297 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1298 ARRAY_SIZE(fsi_soc_dai));
1299 if (ret < 0) {
1300 dev_err(&pdev->dev, "cannot snd dai register\n");
1301 goto exit_snd_soc;
1302 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001303
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001304 return ret;
1305
1306exit_snd_soc:
1307 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001308exit_free_irq:
1309 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001310exit_iounmap:
1311 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001312 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001313exit_kfree:
1314 kfree(master);
1315 master = NULL;
1316exit:
1317 return ret;
1318}
1319
1320static int fsi_remove(struct platform_device *pdev)
1321{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001322 struct fsi_master *master;
1323
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001324 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001325
Kuninori Morimotod985f272011-04-08 15:09:25 +09001326 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001327 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001328
Kuninori Morimotod985f272011-04-08 15:09:25 +09001329 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1330 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001331
1332 iounmap(master->base);
1333 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001334
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001335 return 0;
1336}
1337
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001338static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001339 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001340 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001341{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001342 if (!fsi_stream_is_working(fsi, is_play))
1343 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001344
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001345 fsi_port_stop(fsi, is_play);
1346 fsi_hw_shutdown(fsi, is_play, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001347}
1348
1349static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001350 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001351 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001352{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001353 if (!fsi_stream_is_working(fsi, is_play))
1354 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001355
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001356 fsi_hw_startup(fsi, is_play, dev);
1357
1358 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001359 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001360
1361 fsi_port_start(fsi, is_play);
1362
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001363}
1364
1365static int fsi_suspend(struct device *dev)
1366{
1367 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001368 struct fsi_priv *fsia = &master->fsia;
1369 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001370
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001371 __fsi_suspend(fsia, 1, dev);
1372 __fsi_suspend(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001373
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001374 __fsi_suspend(fsib, 1, dev);
1375 __fsi_suspend(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001376
1377 return 0;
1378}
1379
1380static int fsi_resume(struct device *dev)
1381{
1382 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001383 struct fsi_priv *fsia = &master->fsia;
1384 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001385
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001386 __fsi_resume(fsia, 1, dev);
1387 __fsi_resume(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001388
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001389 __fsi_resume(fsib, 1, dev);
1390 __fsi_resume(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001391
1392 return 0;
1393}
1394
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001395static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001396 .suspend = fsi_suspend,
1397 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001398};
1399
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001400static struct fsi_core fsi1_core = {
1401 .ver = 1,
1402
1403 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001404 .int_st = INT_ST,
1405 .iemsk = IEMSK,
1406 .imsk = IMSK,
1407};
1408
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001409static struct fsi_core fsi2_core = {
1410 .ver = 2,
1411
1412 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001413 .int_st = CPU_INT_ST,
1414 .iemsk = CPU_IEMSK,
1415 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001416 .a_mclk = A_MST_CTLR,
1417 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001418};
1419
1420static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001421 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1422 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001423 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001424};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001425MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001426
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001427static struct platform_driver fsi_driver = {
1428 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001429 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001430 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001431 },
1432 .probe = fsi_probe,
1433 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001434 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001435};
1436
Axel Lincb5e8732011-11-25 10:15:07 +08001437module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001438
1439MODULE_LICENSE("GPL");
1440MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1441MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001442MODULE_ALIAS("platform:fsi-pcm-audio");