blob: 507c02b1c80933e03ba63891447f9e69cd044460 [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>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090019#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090020#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090021
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090022/* PortA/PortB register */
23#define REG_DO_FMT 0x0000
24#define REG_DOFF_CTL 0x0004
25#define REG_DOFF_ST 0x0008
26#define REG_DI_FMT 0x000C
27#define REG_DIFF_CTL 0x0010
28#define REG_DIFF_ST 0x0014
29#define REG_CKG1 0x0018
30#define REG_CKG2 0x001C
31#define REG_DIDT 0x0020
32#define REG_DODT 0x0024
33#define REG_MUTE_ST 0x0028
34#define REG_OUT_SEL 0x0030
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090035
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090036/* master register */
37#define MST_CLK_RST 0x0210
38#define MST_SOFT_RST 0x0214
39#define MST_FIFO_SZ 0x0218
40
41/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090042#define A_MST_CTLR 0x0180
43#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090044#define CPU_INT_ST 0x01F4
45#define CPU_IEMSK 0x01F8
46#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090047#define INT_ST 0x0200
48#define IEMSK 0x0204
49#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090050
51/* DO_FMT */
52/* DI_FMT */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090053#define CR_BWS_24 (0x0 << 20) /* FSI2 */
54#define CR_BWS_16 (0x1 << 20) /* FSI2 */
55#define CR_BWS_20 (0x2 << 20) /* FSI2 */
56
57#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
58#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
59#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
60
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090061#define CR_MONO (0x0 << 4)
62#define CR_MONO_D (0x1 << 4)
63#define CR_PCM (0x2 << 4)
64#define CR_I2S (0x3 << 4)
65#define CR_TDM (0x4 << 4)
66#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090067
68/* DOFF_CTL */
69/* DIFF_CTL */
70#define IRQ_HALF 0x00100000
71#define FIFO_CLR 0x00000001
72
73/* DOFF_ST */
74#define ERR_OVER 0x00000010
75#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090076#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090077
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090078/* CKG1 */
79#define ACKMD_MASK 0x00007000
80#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090081#define DIMD (1 << 4)
82#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090083
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090084/* A/B MST_CTLR */
85#define BP (1 << 4) /* Fix the signal of Biphase output */
86#define SE (1 << 0) /* Fix the master clock */
87
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090088/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +090089#define CRB (1 << 4)
90#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090091
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090092/* IO SHIFT / MACRO */
93#define BI_SHIFT 12
94#define BO_SHIFT 8
95#define AI_SHIFT 4
96#define AO_SHIFT 0
97#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090098
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +090099/* SOFT_RST */
100#define PBSR (1 << 12) /* Port B Software Reset */
101#define PASR (1 << 8) /* Port A Software Reset */
102#define IR (1 << 4) /* Interrupt Reset */
103#define FSISR (1 << 0) /* Software Reset */
104
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900105/* OUT_SEL (FSI2) */
106#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
107 /* 1: Biphase and serial */
108
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900109/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900110#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900111
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900112#define FSI_RATES SNDRV_PCM_RATE_8000_96000
113
114#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
115
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900116typedef int (*set_rate_func)(struct device *dev, int is_porta, int rate, int enable);
117
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900118/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900119 * FSI driver use below type name for variable
120 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900121 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900122 * xxx_pos : position of data
123 * xxx_capa : capacity of data
124 */
125
126/*
127 * period/frame/sample image
128 *
129 * ex) PCM (2ch)
130 *
131 * period pos period pos
132 * [n] [n + 1]
133 * |<-------------------- period--------------------->|
134 * ==|============================================ ... =|==
135 * | |
136 * ||<----- frame ----->|<------ frame ----->| ... |
137 * |+--------------------+--------------------+- ... |
138 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
139 * |+--------------------+--------------------+- ... |
140 * ==|============================================ ... =|==
141 */
142
143/*
144 * FSI FIFO image
145 *
146 * | |
147 * | |
148 * | [ sample ] |
149 * | [ sample ] |
150 * | [ sample ] |
151 * | [ sample ] |
152 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900153 */
154
155/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900156 * struct
157 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900158
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900159struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900160 struct snd_pcm_substream *substream;
161
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900162 int fifo_sample_capa; /* sample capacity of FSI FIFO */
163 int buff_sample_capa; /* sample capacity of ALSA buffer */
164 int buff_sample_pos; /* sample position of ALSA buffer */
165 int period_samples; /* sample number / 1 period */
166 int period_pos; /* current period position */
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900167
168 int uerr_num;
169 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900170};
171
172struct fsi_priv {
173 void __iomem *base;
174 struct fsi_master *master;
175
176 struct fsi_stream playback;
177 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900178
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900179 u32 do_fmt;
180 u32 di_fmt;
181
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900182 int chan_num:16;
183 int clk_master:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900184 int spdif:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900185
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000186 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900187};
188
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900189struct fsi_core {
190 int ver;
191
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900192 u32 int_st;
193 u32 iemsk;
194 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900195 u32 a_mclk;
196 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900197};
198
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900199struct fsi_master {
200 void __iomem *base;
201 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900202 struct fsi_priv fsia;
203 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900204 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900205 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900206 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900207};
208
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900209/*
210 * basic read write function
211 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900212
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100213static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900214{
215 /* valid data area is 24bit */
216 data &= 0x00ffffff;
217
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100218 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900219}
220
221static u32 __fsi_reg_read(u32 reg)
222{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100223 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900224}
225
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100226static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900227{
228 u32 val = __fsi_reg_read(reg);
229
230 val &= ~mask;
231 val |= data & mask;
232
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100233 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900234}
235
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900236#define fsi_reg_write(p, r, d)\
237 __fsi_reg_write((u32)(p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900238
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900239#define fsi_reg_read(p, r)\
240 __fsi_reg_read((u32)(p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900241
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900242#define fsi_reg_mask_set(p, r, m, d)\
243 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900244
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900245#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
246#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
247static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900248{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900249 u32 ret;
250 unsigned long flags;
251
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900252 spin_lock_irqsave(&master->lock, flags);
253 ret = __fsi_reg_read((u32)(master->base + reg));
254 spin_unlock_irqrestore(&master->lock, flags);
255
256 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900257}
258
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900259#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
260#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
261static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900262 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900263{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900264 unsigned long flags;
265
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900266 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100267 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900268 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900269}
270
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900271/*
272 * basic function
273 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900274
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900275static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900276{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900277 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900278}
279
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900280static int fsi_is_clk_master(struct fsi_priv *fsi)
281{
282 return fsi->clk_master;
283}
284
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900285static int fsi_is_port_a(struct fsi_priv *fsi)
286{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900287 return fsi->master->base == fsi->base;
288}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900289
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900290static int fsi_is_spdif(struct fsi_priv *fsi)
291{
292 return fsi->spdif;
293}
294
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900295static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900296{
297 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900298
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900300}
301
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900302static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900303{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000304 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900305
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000306 if (dai->id == 0)
307 return &master->fsia;
308 else
309 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900310}
311
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900312static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
313{
314 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
315}
316
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900317static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
318{
319 if (!master->info)
320 return NULL;
321
322 return master->info->set_rate;
323}
324
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900325static u32 fsi_get_info_flags(struct fsi_priv *fsi)
326{
327 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900328 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900329
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900330 if (!master->info)
331 return 0;
332
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900333 return is_porta ? master->info->porta_flags :
334 master->info->portb_flags;
335}
336
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900337static inline int fsi_stream_is_play(int stream)
338{
339 return stream == SNDRV_PCM_STREAM_PLAYBACK;
340}
341
Kuninori Morimoto00545782010-10-12 18:30:14 +0900342static inline int fsi_is_play(struct snd_pcm_substream *substream)
343{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900344 return fsi_stream_is_play(substream->stream);
345}
346
347static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
348 int is_play)
349{
350 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900351}
352
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900353static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900354{
355 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900356 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900357
358 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900359 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900360 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900361 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900362
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900363 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900364}
365
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900366static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
367{
368 return frames * fsi->chan_num;
369}
370
371static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
372{
373 return samples / fsi->chan_num;
374}
375
Kuninori Morimotocda828c2011-05-23 20:46:35 +0900376static int fsi_stream_is_working(struct fsi_priv *fsi,
377 int is_play)
378{
379 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
380 struct fsi_master *master = fsi_get_master(fsi);
381 unsigned long flags;
382 int ret;
383
384 spin_lock_irqsave(&master->lock, flags);
385 ret = !!io->substream;
386 spin_unlock_irqrestore(&master->lock, flags);
387
388 return ret;
389}
390
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900391static void fsi_stream_push(struct fsi_priv *fsi,
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900392 int is_play,
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900393 struct snd_pcm_substream *substream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900394{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900395 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900396 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900397 struct fsi_master *master = fsi_get_master(fsi);
398 unsigned long flags;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900399
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900400 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900401 io->substream = substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900402 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
403 io->buff_sample_pos = 0;
404 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
405 io->period_pos = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900406 io->oerr_num = -1; /* ignore 1st err */
407 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900408 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900409}
410
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900411static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900412{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900413 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900414 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900415 struct fsi_master *master = fsi_get_master(fsi);
416 unsigned long flags;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900417
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900418 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900419
420 if (io->oerr_num > 0)
421 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
422
423 if (io->uerr_num > 0)
424 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900425
426 io->substream = NULL;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900427 io->buff_sample_capa = 0;
428 io->buff_sample_pos = 0;
429 io->period_samples = 0;
430 io->period_pos = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900431 io->oerr_num = 0;
432 io->uerr_num = 0;
Kuninori Morimoto2da65892011-05-23 20:46:13 +0900433 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900434}
435
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900436static int fsi_get_current_fifo_samples(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900437{
438 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900439 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900440
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900441 status = is_play ?
442 fsi_reg_read(fsi, DOFF_ST) :
443 fsi_reg_read(fsi, DIFF_ST);
444
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900445 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900446
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900447 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900448}
449
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900450static void fsi_count_fifo_err(struct fsi_priv *fsi)
451{
452 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
453 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
454
455 if (ostatus & ERR_OVER)
456 fsi->playback.oerr_num++;
457
458 if (ostatus & ERR_UNDER)
459 fsi->playback.uerr_num++;
460
461 if (istatus & ERR_OVER)
462 fsi->capture.oerr_num++;
463
464 if (istatus & ERR_UNDER)
465 fsi->capture.uerr_num++;
466
467 fsi_reg_write(fsi, DOFF_ST, 0);
468 fsi_reg_write(fsi, DIFF_ST, 0);
469}
470
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900471/*
472 * dma function
473 */
474
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900475static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900476{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900477 int is_play = fsi_stream_is_play(stream);
478 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900479 struct snd_pcm_runtime *runtime = io->substream->runtime;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900480
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900481 return runtime->dma_area +
482 samples_to_bytes(runtime, io->buff_sample_pos);
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900483}
484
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900485static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900486{
487 u16 *start;
488 int i;
489
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900490 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900491
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900492 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900493 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
494}
495
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900496static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900497{
498 u16 *start;
499 int i;
500
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900501 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
502
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900503
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900504 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900505 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
506}
507
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900508static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900509{
510 u32 *start;
511 int i;
512
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900513 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
514
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900515
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900516 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900517 fsi_reg_write(fsi, DODT, *(start + i));
518}
519
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900520static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900521{
522 u32 *start;
523 int i;
524
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900525 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900526
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900527 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900528 *(start + i) = fsi_reg_read(fsi, DIDT);
529}
530
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900531/*
532 * irq function
533 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900534
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900535static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
536{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900537 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900538 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900539
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900540 fsi_core_mask_set(master, imsk, data, data);
541 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900542}
543
544static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
545{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900546 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900547 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900548
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900549 fsi_core_mask_set(master, imsk, data, 0);
550 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900551}
552
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900553static u32 fsi_irq_get_status(struct fsi_master *master)
554{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900555 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900556}
557
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900558static void fsi_irq_clear_status(struct fsi_priv *fsi)
559{
560 u32 data = 0;
561 struct fsi_master *master = fsi_get_master(fsi);
562
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900563 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
564 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900565
566 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900567 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900568}
569
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900570/*
571 * SPDIF master clock function
572 *
573 * These functions are used later FSI2
574 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900575static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
576{
577 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900578 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900579
580 if (master->core->ver < 2) {
581 pr_err("fsi: register access err (%s)\n", __func__);
582 return;
583 }
584
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900585 mask = BP | SE;
586 val = enable ? mask : 0;
587
588 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900589 fsi_core_mask_set(master, a_mclk, mask, val) :
590 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900591}
592
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900593/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900594 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900595 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900596static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
597 long rate, int enable)
598{
599 struct fsi_master *master = fsi_get_master(fsi);
600 set_rate_func set_rate = fsi_get_info_set_rate(master);
601 int fsi_ver = master->core->ver;
602 int ret;
603
604 ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
605 if (ret < 0) /* error */
606 return ret;
607
608 if (!enable)
609 return 0;
610
611 if (ret > 0) {
612 u32 data = 0;
613
614 switch (ret & SH_FSI_ACKMD_MASK) {
615 default:
616 /* FALL THROUGH */
617 case SH_FSI_ACKMD_512:
618 data |= (0x0 << 12);
619 break;
620 case SH_FSI_ACKMD_256:
621 data |= (0x1 << 12);
622 break;
623 case SH_FSI_ACKMD_128:
624 data |= (0x2 << 12);
625 break;
626 case SH_FSI_ACKMD_64:
627 data |= (0x3 << 12);
628 break;
629 case SH_FSI_ACKMD_32:
630 if (fsi_ver < 2)
631 dev_err(dev, "unsupported ACKMD\n");
632 else
633 data |= (0x4 << 12);
634 break;
635 }
636
637 switch (ret & SH_FSI_BPFMD_MASK) {
638 default:
639 /* FALL THROUGH */
640 case SH_FSI_BPFMD_32:
641 data |= (0x0 << 8);
642 break;
643 case SH_FSI_BPFMD_64:
644 data |= (0x1 << 8);
645 break;
646 case SH_FSI_BPFMD_128:
647 data |= (0x2 << 8);
648 break;
649 case SH_FSI_BPFMD_256:
650 data |= (0x3 << 8);
651 break;
652 case SH_FSI_BPFMD_512:
653 data |= (0x4 << 8);
654 break;
655 case SH_FSI_BPFMD_16:
656 if (fsi_ver < 2)
657 dev_err(dev, "unsupported ACKMD\n");
658 else
659 data |= (0x7 << 8);
660 break;
661 }
662
663 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
664 udelay(10);
665 ret = 0;
666 }
667
668 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900669}
670
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900671#define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
672#define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
673static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900674{
675 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900676 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900677
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900678 if (enable)
679 fsi_irq_enable(fsi, is_play);
680 else
681 fsi_irq_disable(fsi, is_play);
682
Kuninori Morimotocda828c2011-05-23 20:46:35 +0900683 if (fsi_is_clk_master(fsi))
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900684 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
685}
686
687/*
688 * ctrl function
689 */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900690static void fsi_fifo_init(struct fsi_priv *fsi,
691 int is_play,
692 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900693{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900694 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900695 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900696 u32 shift, i;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900697 int frame_capa;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900698
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900699 /* get on-chip RAM capacity */
700 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900701 shift >>= fsi_get_port_shift(fsi, is_play);
702 shift &= FIFO_SZ_MASK;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900703 frame_capa = 256 << shift;
704 dev_dbg(dai->dev, "fifo = %d words\n", frame_capa);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900705
706 /*
707 * The maximum number of sample data varies depending
708 * on the number of channels selected for the format.
709 *
710 * FIFOs are used in 4-channel units in 3-channel mode
711 * and in 8-channel units in 5- to 7-channel mode
712 * meaning that more FIFOs than the required size of DPRAM
713 * are used.
714 *
715 * ex) if 256 words of DP-RAM is connected
716 * 1 channel: 256 (256 x 1 = 256)
717 * 2 channels: 128 (128 x 2 = 256)
718 * 3 channels: 64 ( 64 x 3 = 192)
719 * 4 channels: 64 ( 64 x 4 = 256)
720 * 5 channels: 32 ( 32 x 5 = 160)
721 * 6 channels: 32 ( 32 x 6 = 192)
722 * 7 channels: 32 ( 32 x 7 = 224)
723 * 8 channels: 32 ( 32 x 8 = 256)
724 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900725 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900726 frame_capa >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900727 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900728 fsi->chan_num, frame_capa);
729
730 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900731
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900732 /*
733 * set interrupt generation factor
734 * clear FIFO
735 */
736 if (is_play) {
737 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
738 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
739 } else {
740 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
741 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
742 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900743}
744
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900745static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900746{
747 struct snd_pcm_runtime *runtime;
748 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900749 int is_play = fsi_stream_is_play(stream);
750 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900751 int sample_residues;
752 int sample_width;
753 int samples;
754 int samples_max;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900755 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900756 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900757
758 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900759 !io->substream ||
760 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900761 return -EINVAL;
762
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900763 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900764 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900765 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900766
767 /* FSI FIFO has limit.
768 * So, this driver can not send periods data at a time
769 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900770 if (io->buff_sample_pos >=
771 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900772
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900773 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900774 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900775
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900776 if (0 == io->period_pos)
777 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900778 }
779
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900780 /* get 1 sample data width */
781 sample_width = samples_to_bytes(runtime, 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900782
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900783 /* get number of residue samples */
784 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900785
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900786 if (is_play) {
787 /*
788 * for play-back
789 *
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900790 * samples_max : number of FSI fifo free samples space
791 * samples : number of ALSA residue samples
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900792 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900793 samples_max = io->fifo_sample_capa;
794 samples_max -= fsi_get_current_fifo_samples(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900795
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900796 samples = sample_residues;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900797
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900798 switch (sample_width) {
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900799 case 2:
800 fn = fsi_dma_soft_push16;
801 break;
802 case 4:
803 fn = fsi_dma_soft_push32;
804 break;
805 default:
806 return -EINVAL;
807 }
808 } else {
809 /*
810 * for capture
811 *
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900812 * samples_max : number of ALSA free samples space
813 * samples : number of samples in FSI fifo
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900814 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900815 samples_max = sample_residues;
816 samples = fsi_get_current_fifo_samples(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900817
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900818 switch (sample_width) {
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900819 case 2:
820 fn = fsi_dma_soft_pop16;
821 break;
822 case 4:
823 fn = fsi_dma_soft_pop32;
824 break;
825 default:
826 return -EINVAL;
827 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900828 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900829
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900830 samples = min(samples, samples_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900831
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900832 fn(fsi, samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900833
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900834 /* update buff_sample_pos */
835 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900836
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900837 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900838 snd_pcm_period_elapsed(substream);
839
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900840 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900841}
842
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900843static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900844{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900845 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900846}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900847
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900848static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900849{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900850 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900851}
852
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900853static irqreturn_t fsi_interrupt(int irq, void *data)
854{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900855 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900856 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900857
858 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900859 fsi_master_mask_set(master, SOFT_RST, IR, 0);
860 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900862 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900863 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900864 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900865 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900866 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900867 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900868 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900869 fsi_data_pop(&master->fsib);
870
871 fsi_count_fifo_err(&master->fsia);
872 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900873
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900874 fsi_irq_clear_status(&master->fsia);
875 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900876
877 return IRQ_HANDLED;
878}
879
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900880/*
881 * dai ops
882 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900883
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900884static int fsi_dai_startup(struct snd_pcm_substream *substream,
885 struct snd_soc_dai *dai)
886{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900887 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900888 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900889 u32 data = 0;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900890 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900891
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900892 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900893
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900894 /* clock setting */
895 if (fsi_is_clk_master(fsi))
896 data = DIMD | DOMD;
897
898 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900899
900 /* clock inversion (CKG2) */
901 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900902 if (SH_FSI_LRM_INV & flags)
903 data |= 1 << 12;
904 if (SH_FSI_BRM_INV & flags)
905 data |= 1 << 8;
906 if (SH_FSI_LRS_INV & flags)
907 data |= 1 << 4;
908 if (SH_FSI_BRS_INV & flags)
909 data |= 1 << 0;
910
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900911 fsi_reg_write(fsi, CKG2, data);
912
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900913 /* set format */
914 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
915 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
916
917 /* spdif ? */
918 if (fsi_is_spdif(fsi)) {
919 fsi_spdif_clk_ctrl(fsi, 1);
920 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
921 }
922
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900923 /* irq clear */
924 fsi_irq_disable(fsi, is_play);
925 fsi_irq_clear_status(fsi);
926
927 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900928 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900929
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900930 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900931}
932
933static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
934 struct snd_soc_dai *dai)
935{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900936 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900937
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900938 if (fsi_is_clk_master(fsi))
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900939 fsi_set_master_clk(dai->dev, fsi, fsi->rate, 0);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900940
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000941 fsi->rate = 0;
942
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900943 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900944}
945
946static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
947 struct snd_soc_dai *dai)
948{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900949 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900950 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900951 int ret = 0;
952
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900953 switch (cmd) {
954 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900955 fsi_stream_push(fsi, is_play, substream);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900956 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900957 fsi_port_start(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900958 break;
959 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900960 fsi_port_stop(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900961 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900962 break;
963 }
964
965 return ret;
966}
967
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900968static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
969{
970 u32 data = 0;
971
972 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
973 case SND_SOC_DAIFMT_I2S:
974 data = CR_I2S;
975 fsi->chan_num = 2;
976 break;
977 case SND_SOC_DAIFMT_LEFT_J:
978 data = CR_PCM;
979 fsi->chan_num = 2;
980 break;
981 default:
982 return -EINVAL;
983 }
984
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900985 fsi->do_fmt = data;
986 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900987
988 return 0;
989}
990
991static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
992{
993 struct fsi_master *master = fsi_get_master(fsi);
994 u32 data = 0;
995
996 if (master->core->ver < 2)
997 return -EINVAL;
998
999 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1000 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001001 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001002
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001003 fsi->do_fmt = data;
1004 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001005
1006 return 0;
1007}
1008
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001009static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1010{
1011 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001012 struct fsi_master *master = fsi_get_master(fsi);
1013 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001014 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001015 int ret;
1016
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001017 /* set master/slave audio interface */
1018 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1019 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001020 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001021 break;
1022 case SND_SOC_DAIFMT_CBS_CFS:
1023 break;
1024 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001025 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001026 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001027
1028 if (fsi_is_clk_master(fsi) && !set_rate) {
1029 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001030 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001031 }
1032
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001033 /* set format */
1034 switch (flags & SH_FSI_FMT_MASK) {
1035 case SH_FSI_FMT_DAI:
1036 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1037 break;
1038 case SH_FSI_FMT_SPDIF:
1039 ret = fsi_set_fmt_spdif(fsi);
1040 break;
1041 default:
1042 ret = -EINVAL;
1043 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001044
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001045 return ret;
1046}
1047
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001048static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1049 struct snd_pcm_hw_params *params,
1050 struct snd_soc_dai *dai)
1051{
1052 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001053 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001054 int ret;
1055
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001056 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001057 return 0;
1058
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001059 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1060 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001061 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001062
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001063 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001064
1065 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001066}
1067
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001068static struct snd_soc_dai_ops fsi_dai_ops = {
1069 .startup = fsi_dai_startup,
1070 .shutdown = fsi_dai_shutdown,
1071 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001072 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001073 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001074};
1075
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001076/*
1077 * pcm ops
1078 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001079
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001080static struct snd_pcm_hardware fsi_pcm_hardware = {
1081 .info = SNDRV_PCM_INFO_INTERLEAVED |
1082 SNDRV_PCM_INFO_MMAP |
1083 SNDRV_PCM_INFO_MMAP_VALID |
1084 SNDRV_PCM_INFO_PAUSE,
1085 .formats = FSI_FMTS,
1086 .rates = FSI_RATES,
1087 .rate_min = 8000,
1088 .rate_max = 192000,
1089 .channels_min = 1,
1090 .channels_max = 2,
1091 .buffer_bytes_max = 64 * 1024,
1092 .period_bytes_min = 32,
1093 .period_bytes_max = 8192,
1094 .periods_min = 1,
1095 .periods_max = 32,
1096 .fifo_size = 256,
1097};
1098
1099static int fsi_pcm_open(struct snd_pcm_substream *substream)
1100{
1101 struct snd_pcm_runtime *runtime = substream->runtime;
1102 int ret = 0;
1103
1104 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1105
1106 ret = snd_pcm_hw_constraint_integer(runtime,
1107 SNDRV_PCM_HW_PARAM_PERIODS);
1108
1109 return ret;
1110}
1111
1112static int fsi_hw_params(struct snd_pcm_substream *substream,
1113 struct snd_pcm_hw_params *hw_params)
1114{
1115 return snd_pcm_lib_malloc_pages(substream,
1116 params_buffer_bytes(hw_params));
1117}
1118
1119static int fsi_hw_free(struct snd_pcm_substream *substream)
1120{
1121 return snd_pcm_lib_free_pages(substream);
1122}
1123
1124static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1125{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001126 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001127 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001128 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001129
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001130 if (samples_pos < 0)
1131 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001132
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001133 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001134}
1135
1136static struct snd_pcm_ops fsi_pcm_ops = {
1137 .open = fsi_pcm_open,
1138 .ioctl = snd_pcm_lib_ioctl,
1139 .hw_params = fsi_hw_params,
1140 .hw_free = fsi_hw_free,
1141 .pointer = fsi_pointer,
1142};
1143
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001144/*
1145 * snd_soc_platform
1146 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001147
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001148#define PREALLOC_BUFFER (32 * 1024)
1149#define PREALLOC_BUFFER_MAX (32 * 1024)
1150
1151static void fsi_pcm_free(struct snd_pcm *pcm)
1152{
1153 snd_pcm_lib_preallocate_free_for_all(pcm);
1154}
1155
1156static int fsi_pcm_new(struct snd_card *card,
1157 struct snd_soc_dai *dai,
1158 struct snd_pcm *pcm)
1159{
1160 /*
1161 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1162 * in MMAP mode (i.e. aplay -M)
1163 */
1164 return snd_pcm_lib_preallocate_pages_for_all(
1165 pcm,
1166 SNDRV_DMA_TYPE_CONTINUOUS,
1167 snd_dma_continuous_data(GFP_KERNEL),
1168 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1169}
1170
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001171/*
1172 * alsa struct
1173 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001176 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001177 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001178 .playback = {
1179 .rates = FSI_RATES,
1180 .formats = FSI_FMTS,
1181 .channels_min = 1,
1182 .channels_max = 8,
1183 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001184 .capture = {
1185 .rates = FSI_RATES,
1186 .formats = FSI_FMTS,
1187 .channels_min = 1,
1188 .channels_max = 8,
1189 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001190 .ops = &fsi_dai_ops,
1191 },
1192 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001193 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001194 .playback = {
1195 .rates = FSI_RATES,
1196 .formats = FSI_FMTS,
1197 .channels_min = 1,
1198 .channels_max = 8,
1199 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001200 .capture = {
1201 .rates = FSI_RATES,
1202 .formats = FSI_FMTS,
1203 .channels_min = 1,
1204 .channels_max = 8,
1205 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001206 .ops = &fsi_dai_ops,
1207 },
1208};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001209
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001210static struct snd_soc_platform_driver fsi_soc_platform = {
1211 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001212 .pcm_new = fsi_pcm_new,
1213 .pcm_free = fsi_pcm_free,
1214};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001215
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001216/*
1217 * platform function
1218 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001219
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001220static int fsi_probe(struct platform_device *pdev)
1221{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001222 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001223 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001224 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001225 unsigned int irq;
1226 int ret;
1227
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001228 id_entry = pdev->id_entry;
1229 if (!id_entry) {
1230 dev_err(&pdev->dev, "unknown fsi device\n");
1231 return -ENODEV;
1232 }
1233
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001234 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1235 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001236 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001237 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1238 ret = -ENODEV;
1239 goto exit;
1240 }
1241
1242 master = kzalloc(sizeof(*master), GFP_KERNEL);
1243 if (!master) {
1244 dev_err(&pdev->dev, "Could not allocate master\n");
1245 ret = -ENOMEM;
1246 goto exit;
1247 }
1248
1249 master->base = ioremap_nocache(res->start, resource_size(res));
1250 if (!master->base) {
1251 ret = -ENXIO;
1252 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1253 goto exit_kfree;
1254 }
1255
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001256 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001257 master->irq = irq;
1258 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001259 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001260 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001261
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001262 /* FSI A setting */
1263 master->fsia.base = master->base;
1264 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001265
1266 /* FSI B setting */
1267 master->fsib.base = master->base + 0x40;
1268 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001269
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001270 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001271 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001272
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001273 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1274 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001275 if (ret) {
1276 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001277 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001278 }
1279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001281 if (ret < 0) {
1282 dev_err(&pdev->dev, "cannot snd soc register\n");
1283 goto exit_free_irq;
1284 }
1285
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001286 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1287 ARRAY_SIZE(fsi_soc_dai));
1288 if (ret < 0) {
1289 dev_err(&pdev->dev, "cannot snd dai register\n");
1290 goto exit_snd_soc;
1291 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001292
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001293 return ret;
1294
1295exit_snd_soc:
1296 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001297exit_free_irq:
1298 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001299exit_iounmap:
1300 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001301 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001302exit_kfree:
1303 kfree(master);
1304 master = NULL;
1305exit:
1306 return ret;
1307}
1308
1309static int fsi_remove(struct platform_device *pdev)
1310{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001311 struct fsi_master *master;
1312
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001313 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001314
Kuninori Morimotod985f272011-04-08 15:09:25 +09001315 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001316 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001317
Kuninori Morimotod985f272011-04-08 15:09:25 +09001318 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1319 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001320
1321 iounmap(master->base);
1322 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001323
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001324 return 0;
1325}
1326
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001327static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001328 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001329 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001330{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001331 if (!fsi_stream_is_working(fsi, is_play))
1332 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001333
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001334 fsi_port_stop(fsi, is_play);
1335 fsi_hw_shutdown(fsi, is_play, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001336}
1337
1338static void __fsi_resume(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_hw_startup(fsi, is_play, dev);
1346
1347 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001348 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001349
1350 fsi_port_start(fsi, is_play);
1351
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001352}
1353
1354static int fsi_suspend(struct device *dev)
1355{
1356 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001357 struct fsi_priv *fsia = &master->fsia;
1358 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001359
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001360 __fsi_suspend(fsia, 1, dev);
1361 __fsi_suspend(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001362
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001363 __fsi_suspend(fsib, 1, dev);
1364 __fsi_suspend(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001365
1366 return 0;
1367}
1368
1369static int fsi_resume(struct device *dev)
1370{
1371 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001372 struct fsi_priv *fsia = &master->fsia;
1373 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001374
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001375 __fsi_resume(fsia, 1, dev);
1376 __fsi_resume(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001377
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001378 __fsi_resume(fsib, 1, dev);
1379 __fsi_resume(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001380
1381 return 0;
1382}
1383
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001384static int fsi_runtime_nop(struct device *dev)
1385{
1386 /* Runtime PM callback shared between ->runtime_suspend()
1387 * and ->runtime_resume(). Simply returns success.
1388 *
1389 * This driver re-initializes all registers after
1390 * pm_runtime_get_sync() anyway so there is no need
1391 * to save and restore registers here.
1392 */
1393 return 0;
1394}
1395
1396static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001397 .suspend = fsi_suspend,
1398 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001399 .runtime_suspend = fsi_runtime_nop,
1400 .runtime_resume = fsi_runtime_nop,
1401};
1402
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001403static struct fsi_core fsi1_core = {
1404 .ver = 1,
1405
1406 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001407 .int_st = INT_ST,
1408 .iemsk = IEMSK,
1409 .imsk = IMSK,
1410};
1411
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001412static struct fsi_core fsi2_core = {
1413 .ver = 2,
1414
1415 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001416 .int_st = CPU_INT_ST,
1417 .iemsk = CPU_IEMSK,
1418 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001419 .a_mclk = A_MST_CTLR,
1420 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001421};
1422
1423static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001424 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1425 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001426 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001427};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001428MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001429
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001430static struct platform_driver fsi_driver = {
1431 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001432 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001433 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001434 },
1435 .probe = fsi_probe,
1436 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001437 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001438};
1439
1440static int __init fsi_mobile_init(void)
1441{
1442 return platform_driver_register(&fsi_driver);
1443}
1444
1445static void __exit fsi_mobile_exit(void)
1446{
1447 platform_driver_unregister(&fsi_driver);
1448}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001449
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001450module_init(fsi_mobile_init);
1451module_exit(fsi_mobile_exit);
1452
1453MODULE_LICENSE("GPL");
1454MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1455MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001456MODULE_ALIAS("platform:fsi-pcm-audio");