blob: a1081c755239e5670f41941a580195c9d007309f [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 Morimoto106c79e2011-04-21 10:33:47 +0900187
188 /* for suspend/resume */
189 u32 saved_do_fmt;
190 u32 saved_di_fmt;
191 u32 saved_ckg1;
192 u32 saved_ckg2;
193 u32 saved_out_sel;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900194};
195
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900196struct fsi_core {
197 int ver;
198
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900199 u32 int_st;
200 u32 iemsk;
201 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900202 u32 a_mclk;
203 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900204};
205
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900206struct fsi_master {
207 void __iomem *base;
208 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900209 struct fsi_priv fsia;
210 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900211 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900212 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900213 spinlock_t lock;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +0900214
215 /* for suspend/resume */
216 u32 saved_a_mclk;
217 u32 saved_b_mclk;
218 u32 saved_iemsk;
219 u32 saved_imsk;
220 u32 saved_clk_rst;
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900221 u32 saved_soft_rst;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900222};
223
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900224/*
225 * basic read write function
226 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900227
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100228static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900229{
230 /* valid data area is 24bit */
231 data &= 0x00ffffff;
232
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100233 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900234}
235
236static u32 __fsi_reg_read(u32 reg)
237{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100238 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900239}
240
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100241static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900242{
243 u32 val = __fsi_reg_read(reg);
244
245 val &= ~mask;
246 val |= data & mask;
247
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100248 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900249}
250
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900251#define fsi_reg_write(p, r, d)\
252 __fsi_reg_write((u32)(p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900253
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900254#define fsi_reg_read(p, r)\
255 __fsi_reg_read((u32)(p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900256
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900257#define fsi_reg_mask_set(p, r, m, d)\
258 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900259
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900260#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
261#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
262static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900263{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900264 u32 ret;
265 unsigned long flags;
266
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900267 spin_lock_irqsave(&master->lock, flags);
268 ret = __fsi_reg_read((u32)(master->base + reg));
269 spin_unlock_irqrestore(&master->lock, flags);
270
271 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900272}
273
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900274#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
275#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
276static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900277 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900278{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900279 unsigned long flags;
280
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900281 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100282 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900283 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900284}
285
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900286/*
287 * basic function
288 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900289
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900290static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900291{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900292 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900293}
294
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900295static int fsi_is_clk_master(struct fsi_priv *fsi)
296{
297 return fsi->clk_master;
298}
299
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900300static int fsi_is_port_a(struct fsi_priv *fsi)
301{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900302 return fsi->master->base == fsi->base;
303}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900304
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900305static int fsi_is_spdif(struct fsi_priv *fsi)
306{
307 return fsi->spdif;
308}
309
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900310static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900311{
312 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900313
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000314 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900315}
316
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900317static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900318{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000319 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900320
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000321 if (dai->id == 0)
322 return &master->fsia;
323 else
324 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900325}
326
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900327static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
328{
329 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
330}
331
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900332static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
333{
334 if (!master->info)
335 return NULL;
336
337 return master->info->set_rate;
338}
339
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900340static u32 fsi_get_info_flags(struct fsi_priv *fsi)
341{
342 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900343 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900344
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900345 if (!master->info)
346 return 0;
347
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900348 return is_porta ? master->info->porta_flags :
349 master->info->portb_flags;
350}
351
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900352static inline int fsi_stream_is_play(int stream)
353{
354 return stream == SNDRV_PCM_STREAM_PLAYBACK;
355}
356
Kuninori Morimoto00545782010-10-12 18:30:14 +0900357static inline int fsi_is_play(struct snd_pcm_substream *substream)
358{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900359 return fsi_stream_is_play(substream->stream);
360}
361
362static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
363 int is_play)
364{
365 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900366}
367
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900368static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900369{
370 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900371 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900372
373 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900374 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900375 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900376 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900377
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900378 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900379}
380
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900381static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
382{
383 return frames * fsi->chan_num;
384}
385
386static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
387{
388 return samples / fsi->chan_num;
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 Morimoto93193c22010-10-12 19:19:28 +0900397
398 io->substream = substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900399 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
400 io->buff_sample_pos = 0;
401 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
402 io->period_pos = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900403 io->oerr_num = -1; /* ignore 1st err */
404 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900405}
406
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900407static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900408{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900409 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900410 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
411
412
413 if (io->oerr_num > 0)
414 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
415
416 if (io->uerr_num > 0)
417 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900418
419 io->substream = NULL;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900420 io->buff_sample_capa = 0;
421 io->buff_sample_pos = 0;
422 io->period_samples = 0;
423 io->period_pos = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900424 io->oerr_num = 0;
425 io->uerr_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900426}
427
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900428static int fsi_get_current_fifo_samples(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900429{
430 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900431 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900432
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900433 status = is_play ?
434 fsi_reg_read(fsi, DOFF_ST) :
435 fsi_reg_read(fsi, DIFF_ST);
436
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900437 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900438
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900439 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900440}
441
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900442static void fsi_count_fifo_err(struct fsi_priv *fsi)
443{
444 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
445 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
446
447 if (ostatus & ERR_OVER)
448 fsi->playback.oerr_num++;
449
450 if (ostatus & ERR_UNDER)
451 fsi->playback.uerr_num++;
452
453 if (istatus & ERR_OVER)
454 fsi->capture.oerr_num++;
455
456 if (istatus & ERR_UNDER)
457 fsi->capture.uerr_num++;
458
459 fsi_reg_write(fsi, DOFF_ST, 0);
460 fsi_reg_write(fsi, DIFF_ST, 0);
461}
462
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900463/*
464 * dma function
465 */
466
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900467static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900468{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900469 int is_play = fsi_stream_is_play(stream);
470 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900471 struct snd_pcm_runtime *runtime = io->substream->runtime;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900472
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900473 return runtime->dma_area +
474 samples_to_bytes(runtime, io->buff_sample_pos);
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900475}
476
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900477static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900478{
479 u16 *start;
480 int i;
481
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900482 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900483
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900484 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900485 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
486}
487
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900488static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900489{
490 u16 *start;
491 int i;
492
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900493 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
494
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900495
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900496 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900497 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
498}
499
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900500static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900501{
502 u32 *start;
503 int i;
504
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900505 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
506
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900507
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900508 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900509 fsi_reg_write(fsi, DODT, *(start + i));
510}
511
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900512static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900513{
514 u32 *start;
515 int i;
516
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900517 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900518
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900519 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900520 *(start + i) = fsi_reg_read(fsi, DIDT);
521}
522
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900523/*
524 * irq function
525 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900526
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900527static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
528{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900529 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900530 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900531
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900532 fsi_core_mask_set(master, imsk, data, data);
533 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900534}
535
536static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
537{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900538 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900539 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900540
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900541 fsi_core_mask_set(master, imsk, data, 0);
542 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900543}
544
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900545static u32 fsi_irq_get_status(struct fsi_master *master)
546{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900547 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900548}
549
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900550static void fsi_irq_clear_status(struct fsi_priv *fsi)
551{
552 u32 data = 0;
553 struct fsi_master *master = fsi_get_master(fsi);
554
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900555 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
556 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900557
558 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900559 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900560}
561
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900562/*
563 * SPDIF master clock function
564 *
565 * These functions are used later FSI2
566 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900567static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
568{
569 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900570 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900571
572 if (master->core->ver < 2) {
573 pr_err("fsi: register access err (%s)\n", __func__);
574 return;
575 }
576
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900577 mask = BP | SE;
578 val = enable ? mask : 0;
579
580 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900581 fsi_core_mask_set(master, a_mclk, mask, val) :
582 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900583}
584
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900585/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900586 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900587 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900588static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
589 long rate, int enable)
590{
591 struct fsi_master *master = fsi_get_master(fsi);
592 set_rate_func set_rate = fsi_get_info_set_rate(master);
593 int fsi_ver = master->core->ver;
594 int ret;
595
596 ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
597 if (ret < 0) /* error */
598 return ret;
599
600 if (!enable)
601 return 0;
602
603 if (ret > 0) {
604 u32 data = 0;
605
606 switch (ret & SH_FSI_ACKMD_MASK) {
607 default:
608 /* FALL THROUGH */
609 case SH_FSI_ACKMD_512:
610 data |= (0x0 << 12);
611 break;
612 case SH_FSI_ACKMD_256:
613 data |= (0x1 << 12);
614 break;
615 case SH_FSI_ACKMD_128:
616 data |= (0x2 << 12);
617 break;
618 case SH_FSI_ACKMD_64:
619 data |= (0x3 << 12);
620 break;
621 case SH_FSI_ACKMD_32:
622 if (fsi_ver < 2)
623 dev_err(dev, "unsupported ACKMD\n");
624 else
625 data |= (0x4 << 12);
626 break;
627 }
628
629 switch (ret & SH_FSI_BPFMD_MASK) {
630 default:
631 /* FALL THROUGH */
632 case SH_FSI_BPFMD_32:
633 data |= (0x0 << 8);
634 break;
635 case SH_FSI_BPFMD_64:
636 data |= (0x1 << 8);
637 break;
638 case SH_FSI_BPFMD_128:
639 data |= (0x2 << 8);
640 break;
641 case SH_FSI_BPFMD_256:
642 data |= (0x3 << 8);
643 break;
644 case SH_FSI_BPFMD_512:
645 data |= (0x4 << 8);
646 break;
647 case SH_FSI_BPFMD_16:
648 if (fsi_ver < 2)
649 dev_err(dev, "unsupported ACKMD\n");
650 else
651 data |= (0x7 << 8);
652 break;
653 }
654
655 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
656 udelay(10);
657 ret = 0;
658 }
659
660 return ret;
661
662}
663
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900664#define fsi_module_init(m, d) __fsi_module_clk_ctrl(m, d, 1)
665#define fsi_module_kill(m, d) __fsi_module_clk_ctrl(m, d, 0)
666static void __fsi_module_clk_ctrl(struct fsi_master *master,
667 struct device *dev,
668 int enable)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900669{
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900670 pm_runtime_get_sync(dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900671
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900672 if (enable) {
673 /* enable only SR */
674 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
675 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
676 } else {
677 /* clear all registers */
678 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
679 }
680
681 pm_runtime_put_sync(dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900682}
683
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900684#define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
685#define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
686static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900687{
688 struct fsi_master *master = fsi_get_master(fsi);
689 u32 soft = fsi_is_port_a(fsi) ? PASR : PBSR;
690 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
691 int is_master = fsi_is_clk_master(fsi);
692
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900693 if (enable)
694 fsi_irq_enable(fsi, is_play);
695 else
696 fsi_irq_disable(fsi, is_play);
697
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900698 fsi_master_mask_set(master, SOFT_RST, soft, (enable) ? soft : 0);
699 if (is_master)
700 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
701}
702
703/*
704 * ctrl function
705 */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900706static void fsi_fifo_init(struct fsi_priv *fsi,
707 int is_play,
708 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900709{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900710 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900711 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900712 u32 shift, i;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900713 int frame_capa;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900714
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900715 /* get on-chip RAM capacity */
716 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900717 shift >>= fsi_get_port_shift(fsi, is_play);
718 shift &= FIFO_SZ_MASK;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900719 frame_capa = 256 << shift;
720 dev_dbg(dai->dev, "fifo = %d words\n", frame_capa);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900721
722 /*
723 * The maximum number of sample data varies depending
724 * on the number of channels selected for the format.
725 *
726 * FIFOs are used in 4-channel units in 3-channel mode
727 * and in 8-channel units in 5- to 7-channel mode
728 * meaning that more FIFOs than the required size of DPRAM
729 * are used.
730 *
731 * ex) if 256 words of DP-RAM is connected
732 * 1 channel: 256 (256 x 1 = 256)
733 * 2 channels: 128 (128 x 2 = 256)
734 * 3 channels: 64 ( 64 x 3 = 192)
735 * 4 channels: 64 ( 64 x 4 = 256)
736 * 5 channels: 32 ( 32 x 5 = 160)
737 * 6 channels: 32 ( 32 x 6 = 192)
738 * 7 channels: 32 ( 32 x 7 = 224)
739 * 8 channels: 32 ( 32 x 8 = 256)
740 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900741 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900742 frame_capa >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900743 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900744 fsi->chan_num, frame_capa);
745
746 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900747
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900748 /*
749 * set interrupt generation factor
750 * clear FIFO
751 */
752 if (is_play) {
753 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
754 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
755 } else {
756 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
757 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
758 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900759}
760
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900761static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900762{
763 struct snd_pcm_runtime *runtime;
764 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900765 int is_play = fsi_stream_is_play(stream);
766 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900767 int sample_residues;
768 int sample_width;
769 int samples;
770 int samples_max;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900771 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900772 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900773
774 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900775 !io->substream ||
776 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900777 return -EINVAL;
778
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900779 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900780 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900781 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900782
783 /* FSI FIFO has limit.
784 * So, this driver can not send periods data at a time
785 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900786 if (io->buff_sample_pos >=
787 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900788
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900789 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900790 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900791
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900792 if (0 == io->period_pos)
793 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900794 }
795
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900796 /* get 1 sample data width */
797 sample_width = samples_to_bytes(runtime, 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900798
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900799 /* get number of residue samples */
800 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900801
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900802 if (is_play) {
803 /*
804 * for play-back
805 *
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900806 * samples_max : number of FSI fifo free samples space
807 * samples : number of ALSA residue samples
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900808 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900809 samples_max = io->fifo_sample_capa;
810 samples_max -= fsi_get_current_fifo_samples(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900811
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900812 samples = sample_residues;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900813
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900814 switch (sample_width) {
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900815 case 2:
816 fn = fsi_dma_soft_push16;
817 break;
818 case 4:
819 fn = fsi_dma_soft_push32;
820 break;
821 default:
822 return -EINVAL;
823 }
824 } else {
825 /*
826 * for capture
827 *
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900828 * samples_max : number of ALSA free samples space
829 * samples : number of samples in FSI fifo
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900830 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900831 samples_max = sample_residues;
832 samples = fsi_get_current_fifo_samples(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900833
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900834 switch (sample_width) {
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900835 case 2:
836 fn = fsi_dma_soft_pop16;
837 break;
838 case 4:
839 fn = fsi_dma_soft_pop32;
840 break;
841 default:
842 return -EINVAL;
843 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900844 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900845
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900846 samples = min(samples, samples_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900847
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900848 fn(fsi, samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900849
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900850 /* update buff_sample_pos */
851 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900852
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900853 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900854 snd_pcm_period_elapsed(substream);
855
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900856 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900857}
858
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900859static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900860{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900861 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900862}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900863
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900864static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900865{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900866 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900867}
868
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900869static irqreturn_t fsi_interrupt(int irq, void *data)
870{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900871 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900872 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900873
874 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900875 fsi_master_mask_set(master, SOFT_RST, IR, 0);
876 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900877
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900878 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900879 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900880 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900881 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900882 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900883 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900884 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900885 fsi_data_pop(&master->fsib);
886
887 fsi_count_fifo_err(&master->fsia);
888 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900889
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900890 fsi_irq_clear_status(&master->fsia);
891 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900892
893 return IRQ_HANDLED;
894}
895
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900896/*
897 * dai ops
898 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900899
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900900static int fsi_dai_startup(struct snd_pcm_substream *substream,
901 struct snd_soc_dai *dai)
902{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900903 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900904 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900905 u32 data = 0;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900906 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900907
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900908 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900909
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900910 /* clock setting */
911 if (fsi_is_clk_master(fsi))
912 data = DIMD | DOMD;
913
914 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900915
916 /* clock inversion (CKG2) */
917 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900918 if (SH_FSI_LRM_INV & flags)
919 data |= 1 << 12;
920 if (SH_FSI_BRM_INV & flags)
921 data |= 1 << 8;
922 if (SH_FSI_LRS_INV & flags)
923 data |= 1 << 4;
924 if (SH_FSI_BRS_INV & flags)
925 data |= 1 << 0;
926
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900927 fsi_reg_write(fsi, CKG2, data);
928
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900929 /* set format */
930 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
931 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
932
933 /* spdif ? */
934 if (fsi_is_spdif(fsi)) {
935 fsi_spdif_clk_ctrl(fsi, 1);
936 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
937 }
938
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900939 /* irq clear */
940 fsi_irq_disable(fsi, is_play);
941 fsi_irq_clear_status(fsi);
942
943 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900944 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900945
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900946 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900947}
948
949static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
950 struct snd_soc_dai *dai)
951{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900952 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900953
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900954 if (fsi_is_clk_master(fsi))
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900955 fsi_set_master_clk(dai->dev, fsi, fsi->rate, 0);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900956
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000957 fsi->rate = 0;
958
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900959 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900960}
961
962static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
963 struct snd_soc_dai *dai)
964{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900965 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900966 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900967 int ret = 0;
968
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900969 switch (cmd) {
970 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto0ffe2962011-05-23 20:45:57 +0900971 fsi_stream_push(fsi, is_play, substream);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900972 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900973 fsi_port_start(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900974 break;
975 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900976 fsi_port_stop(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900977 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900978 break;
979 }
980
981 return ret;
982}
983
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900984static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
985{
986 u32 data = 0;
987
988 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
989 case SND_SOC_DAIFMT_I2S:
990 data = CR_I2S;
991 fsi->chan_num = 2;
992 break;
993 case SND_SOC_DAIFMT_LEFT_J:
994 data = CR_PCM;
995 fsi->chan_num = 2;
996 break;
997 default:
998 return -EINVAL;
999 }
1000
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001001 fsi->do_fmt = data;
1002 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001003
1004 return 0;
1005}
1006
1007static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1008{
1009 struct fsi_master *master = fsi_get_master(fsi);
1010 u32 data = 0;
1011
1012 if (master->core->ver < 2)
1013 return -EINVAL;
1014
1015 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1016 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001017 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001018
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001019 fsi->do_fmt = data;
1020 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001021
1022 return 0;
1023}
1024
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001025static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1026{
1027 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001028 struct fsi_master *master = fsi_get_master(fsi);
1029 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001030 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001031 int ret;
1032
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001033 /* set master/slave audio interface */
1034 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1035 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001036 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001037 break;
1038 case SND_SOC_DAIFMT_CBS_CFS:
1039 break;
1040 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001041 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001042 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001043
1044 if (fsi_is_clk_master(fsi) && !set_rate) {
1045 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001046 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001047 }
1048
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001049 /* set format */
1050 switch (flags & SH_FSI_FMT_MASK) {
1051 case SH_FSI_FMT_DAI:
1052 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1053 break;
1054 case SH_FSI_FMT_SPDIF:
1055 ret = fsi_set_fmt_spdif(fsi);
1056 break;
1057 default:
1058 ret = -EINVAL;
1059 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001060
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001061 return ret;
1062}
1063
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001064static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1065 struct snd_pcm_hw_params *params,
1066 struct snd_soc_dai *dai)
1067{
1068 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001069 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001070 int ret;
1071
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001072 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001073 return 0;
1074
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001075 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1076 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001077 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001078
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001079 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001080
1081 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001082}
1083
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001084static struct snd_soc_dai_ops fsi_dai_ops = {
1085 .startup = fsi_dai_startup,
1086 .shutdown = fsi_dai_shutdown,
1087 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001088 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001089 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001090};
1091
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001092/*
1093 * pcm ops
1094 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001095
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001096static struct snd_pcm_hardware fsi_pcm_hardware = {
1097 .info = SNDRV_PCM_INFO_INTERLEAVED |
1098 SNDRV_PCM_INFO_MMAP |
1099 SNDRV_PCM_INFO_MMAP_VALID |
1100 SNDRV_PCM_INFO_PAUSE,
1101 .formats = FSI_FMTS,
1102 .rates = FSI_RATES,
1103 .rate_min = 8000,
1104 .rate_max = 192000,
1105 .channels_min = 1,
1106 .channels_max = 2,
1107 .buffer_bytes_max = 64 * 1024,
1108 .period_bytes_min = 32,
1109 .period_bytes_max = 8192,
1110 .periods_min = 1,
1111 .periods_max = 32,
1112 .fifo_size = 256,
1113};
1114
1115static int fsi_pcm_open(struct snd_pcm_substream *substream)
1116{
1117 struct snd_pcm_runtime *runtime = substream->runtime;
1118 int ret = 0;
1119
1120 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1121
1122 ret = snd_pcm_hw_constraint_integer(runtime,
1123 SNDRV_PCM_HW_PARAM_PERIODS);
1124
1125 return ret;
1126}
1127
1128static int fsi_hw_params(struct snd_pcm_substream *substream,
1129 struct snd_pcm_hw_params *hw_params)
1130{
1131 return snd_pcm_lib_malloc_pages(substream,
1132 params_buffer_bytes(hw_params));
1133}
1134
1135static int fsi_hw_free(struct snd_pcm_substream *substream)
1136{
1137 return snd_pcm_lib_free_pages(substream);
1138}
1139
1140static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1141{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001142 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001143 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001144 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001145
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001146 if (samples_pos < 0)
1147 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001148
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001149 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001150}
1151
1152static struct snd_pcm_ops fsi_pcm_ops = {
1153 .open = fsi_pcm_open,
1154 .ioctl = snd_pcm_lib_ioctl,
1155 .hw_params = fsi_hw_params,
1156 .hw_free = fsi_hw_free,
1157 .pointer = fsi_pointer,
1158};
1159
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001160/*
1161 * snd_soc_platform
1162 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001163
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001164#define PREALLOC_BUFFER (32 * 1024)
1165#define PREALLOC_BUFFER_MAX (32 * 1024)
1166
1167static void fsi_pcm_free(struct snd_pcm *pcm)
1168{
1169 snd_pcm_lib_preallocate_free_for_all(pcm);
1170}
1171
1172static int fsi_pcm_new(struct snd_card *card,
1173 struct snd_soc_dai *dai,
1174 struct snd_pcm *pcm)
1175{
1176 /*
1177 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1178 * in MMAP mode (i.e. aplay -M)
1179 */
1180 return snd_pcm_lib_preallocate_pages_for_all(
1181 pcm,
1182 SNDRV_DMA_TYPE_CONTINUOUS,
1183 snd_dma_continuous_data(GFP_KERNEL),
1184 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1185}
1186
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001187/*
1188 * alsa struct
1189 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001190
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001191static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001192 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001193 .name = "fsia-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 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001209 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001210 .playback = {
1211 .rates = FSI_RATES,
1212 .formats = FSI_FMTS,
1213 .channels_min = 1,
1214 .channels_max = 8,
1215 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001216 .capture = {
1217 .rates = FSI_RATES,
1218 .formats = FSI_FMTS,
1219 .channels_min = 1,
1220 .channels_max = 8,
1221 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001222 .ops = &fsi_dai_ops,
1223 },
1224};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001225
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001226static struct snd_soc_platform_driver fsi_soc_platform = {
1227 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001228 .pcm_new = fsi_pcm_new,
1229 .pcm_free = fsi_pcm_free,
1230};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001231
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001232/*
1233 * platform function
1234 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001235
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001236static int fsi_probe(struct platform_device *pdev)
1237{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001238 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001239 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001240 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001241 unsigned int irq;
1242 int ret;
1243
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001244 id_entry = pdev->id_entry;
1245 if (!id_entry) {
1246 dev_err(&pdev->dev, "unknown fsi device\n");
1247 return -ENODEV;
1248 }
1249
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001250 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1251 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001252 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001253 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1254 ret = -ENODEV;
1255 goto exit;
1256 }
1257
1258 master = kzalloc(sizeof(*master), GFP_KERNEL);
1259 if (!master) {
1260 dev_err(&pdev->dev, "Could not allocate master\n");
1261 ret = -ENOMEM;
1262 goto exit;
1263 }
1264
1265 master->base = ioremap_nocache(res->start, resource_size(res));
1266 if (!master->base) {
1267 ret = -ENXIO;
1268 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1269 goto exit_kfree;
1270 }
1271
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001272 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001273 master->irq = irq;
1274 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001275 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001276 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001277
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001278 /* FSI A setting */
1279 master->fsia.base = master->base;
1280 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001281
1282 /* FSI B setting */
1283 master->fsib.base = master->base + 0x40;
1284 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001285
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001286 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001287 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001288
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001289 fsi_module_init(master, &pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001290
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001291 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1292 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001293 if (ret) {
1294 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001295 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001296 }
1297
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001299 if (ret < 0) {
1300 dev_err(&pdev->dev, "cannot snd soc register\n");
1301 goto exit_free_irq;
1302 }
1303
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001304 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1305 ARRAY_SIZE(fsi_soc_dai));
1306 if (ret < 0) {
1307 dev_err(&pdev->dev, "cannot snd dai register\n");
1308 goto exit_snd_soc;
1309 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001310
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001311 return ret;
1312
1313exit_snd_soc:
1314 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001315exit_free_irq:
1316 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001317exit_iounmap:
1318 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001319 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001320exit_kfree:
1321 kfree(master);
1322 master = NULL;
1323exit:
1324 return ret;
1325}
1326
1327static int fsi_remove(struct platform_device *pdev)
1328{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001329 struct fsi_master *master;
1330
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001331 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001332
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001333 fsi_module_kill(master, &pdev->dev);
1334
Kuninori Morimotod985f272011-04-08 15:09:25 +09001335 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001336 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001337
Kuninori Morimotod985f272011-04-08 15:09:25 +09001338 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1339 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001340
1341 iounmap(master->base);
1342 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001343
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001344 return 0;
1345}
1346
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001347static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001348 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001349{
1350 fsi->saved_do_fmt = fsi_reg_read(fsi, DO_FMT);
1351 fsi->saved_di_fmt = fsi_reg_read(fsi, DI_FMT);
1352 fsi->saved_ckg1 = fsi_reg_read(fsi, CKG1);
1353 fsi->saved_ckg2 = fsi_reg_read(fsi, CKG2);
1354 fsi->saved_out_sel = fsi_reg_read(fsi, OUT_SEL);
1355
1356 if (fsi_is_clk_master(fsi))
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001357 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001358}
1359
1360static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001361 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001362{
1363 fsi_reg_write(fsi, DO_FMT, fsi->saved_do_fmt);
1364 fsi_reg_write(fsi, DI_FMT, fsi->saved_di_fmt);
1365 fsi_reg_write(fsi, CKG1, fsi->saved_ckg1);
1366 fsi_reg_write(fsi, CKG2, fsi->saved_ckg2);
1367 fsi_reg_write(fsi, OUT_SEL, fsi->saved_out_sel);
1368
1369 if (fsi_is_clk_master(fsi))
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001370 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001371}
1372
1373static int fsi_suspend(struct device *dev)
1374{
1375 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001376
1377 pm_runtime_get_sync(dev);
1378
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001379 __fsi_suspend(&master->fsia, dev);
1380 __fsi_suspend(&master->fsib, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001381
1382 master->saved_a_mclk = fsi_core_read(master, a_mclk);
1383 master->saved_b_mclk = fsi_core_read(master, b_mclk);
1384 master->saved_iemsk = fsi_core_read(master, iemsk);
1385 master->saved_imsk = fsi_core_read(master, imsk);
1386 master->saved_clk_rst = fsi_master_read(master, CLK_RST);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001387 master->saved_soft_rst = fsi_master_read(master, SOFT_RST);
1388
1389 fsi_module_kill(master, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001390
1391 pm_runtime_put_sync(dev);
1392
1393 return 0;
1394}
1395
1396static int fsi_resume(struct device *dev)
1397{
1398 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001399
1400 pm_runtime_get_sync(dev);
1401
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001402 fsi_module_init(master, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001403
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001404 fsi_master_mask_set(master, SOFT_RST, 0xffff, master->saved_soft_rst);
1405 fsi_master_mask_set(master, CLK_RST, 0xffff, master->saved_clk_rst);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001406 fsi_core_mask_set(master, a_mclk, 0xffff, master->saved_a_mclk);
1407 fsi_core_mask_set(master, b_mclk, 0xffff, master->saved_b_mclk);
1408 fsi_core_mask_set(master, iemsk, 0xffff, master->saved_iemsk);
1409 fsi_core_mask_set(master, imsk, 0xffff, master->saved_imsk);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001410
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001411 __fsi_resume(&master->fsia, dev);
1412 __fsi_resume(&master->fsib, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001413
1414 pm_runtime_put_sync(dev);
1415
1416 return 0;
1417}
1418
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001419static int fsi_runtime_nop(struct device *dev)
1420{
1421 /* Runtime PM callback shared between ->runtime_suspend()
1422 * and ->runtime_resume(). Simply returns success.
1423 *
1424 * This driver re-initializes all registers after
1425 * pm_runtime_get_sync() anyway so there is no need
1426 * to save and restore registers here.
1427 */
1428 return 0;
1429}
1430
1431static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001432 .suspend = fsi_suspend,
1433 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001434 .runtime_suspend = fsi_runtime_nop,
1435 .runtime_resume = fsi_runtime_nop,
1436};
1437
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001438static struct fsi_core fsi1_core = {
1439 .ver = 1,
1440
1441 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001442 .int_st = INT_ST,
1443 .iemsk = IEMSK,
1444 .imsk = IMSK,
1445};
1446
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001447static struct fsi_core fsi2_core = {
1448 .ver = 2,
1449
1450 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001451 .int_st = CPU_INT_ST,
1452 .iemsk = CPU_IEMSK,
1453 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001454 .a_mclk = A_MST_CTLR,
1455 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001456};
1457
1458static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001459 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1460 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001461 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001462};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001463MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001464
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001465static struct platform_driver fsi_driver = {
1466 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001467 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001468 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001469 },
1470 .probe = fsi_probe,
1471 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001472 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001473};
1474
1475static int __init fsi_mobile_init(void)
1476{
1477 return platform_driver_register(&fsi_driver);
1478}
1479
1480static void __exit fsi_mobile_exit(void)
1481{
1482 platform_driver_unregister(&fsi_driver);
1483}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001484
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001485module_init(fsi_mobile_init);
1486module_exit(fsi_mobile_exit);
1487
1488MODULE_LICENSE("GPL");
1489MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1490MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001491MODULE_ALIAS("platform:fsi-pcm-audio");