blob: 5a2fdf3b84b92ce670b177eab6bfb10d29b0bbab [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 */
89#define B_CLK 0x00000010
90#define A_CLK 0x00000001
91
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 *
121 * xxx_len : data length
122 * xxx_width : data width
123 * xxx_offset : data offset
124 * xxx_num : number of data
125 */
126
127/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900128 * struct
129 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900130
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900131struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900132 struct snd_pcm_substream *substream;
133
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900134 int fifo_max_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900135
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900136 int buff_offset;
137 int buff_len;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900138 int period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900139 int period_num;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900140
141 int uerr_num;
142 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900143};
144
145struct fsi_priv {
146 void __iomem *base;
147 struct fsi_master *master;
148
149 struct fsi_stream playback;
150 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900151
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900152 int chan_num:16;
153 int clk_master:1;
154
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000155 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900156};
157
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900158struct fsi_core {
159 int ver;
160
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900161 u32 int_st;
162 u32 iemsk;
163 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900164 u32 a_mclk;
165 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900166};
167
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900168struct fsi_master {
169 void __iomem *base;
170 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900171 struct fsi_priv fsia;
172 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900173 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900174 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900175 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900176};
177
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900178/*
179 * basic read write function
180 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900181
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100182static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900183{
184 /* valid data area is 24bit */
185 data &= 0x00ffffff;
186
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100187 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900188}
189
190static u32 __fsi_reg_read(u32 reg)
191{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100192 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900193}
194
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100195static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900196{
197 u32 val = __fsi_reg_read(reg);
198
199 val &= ~mask;
200 val |= data & mask;
201
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100202 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900203}
204
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900205#define fsi_reg_write(p, r, d)\
206 __fsi_reg_write((u32)(p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900207
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900208#define fsi_reg_read(p, r)\
209 __fsi_reg_read((u32)(p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900210
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900211#define fsi_reg_mask_set(p, r, m, d)\
212 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900213
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900214#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
215#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
216static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900217{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900218 u32 ret;
219 unsigned long flags;
220
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900221 spin_lock_irqsave(&master->lock, flags);
222 ret = __fsi_reg_read((u32)(master->base + reg));
223 spin_unlock_irqrestore(&master->lock, flags);
224
225 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900226}
227
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900228#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
229#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
230static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900231 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900232{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900233 unsigned long flags;
234
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900235 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100236 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900237 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900238}
239
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900240/*
241 * basic function
242 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900243
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900244static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900245{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900246 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247}
248
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900249static int fsi_is_clk_master(struct fsi_priv *fsi)
250{
251 return fsi->clk_master;
252}
253
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900254static int fsi_is_port_a(struct fsi_priv *fsi)
255{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900256 return fsi->master->base == fsi->base;
257}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900258
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900259static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900260{
261 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900262
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000263 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900264}
265
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900266static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900267{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000268 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900269
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000270 if (dai->id == 0)
271 return &master->fsia;
272 else
273 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900274}
275
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900276static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
277{
278 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
279}
280
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900281static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
282{
283 if (!master->info)
284 return NULL;
285
286 return master->info->set_rate;
287}
288
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900289static u32 fsi_get_info_flags(struct fsi_priv *fsi)
290{
291 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900292 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900293
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900294 if (!master->info)
295 return 0;
296
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900297 return is_porta ? master->info->porta_flags :
298 master->info->portb_flags;
299}
300
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900301static inline int fsi_stream_is_play(int stream)
302{
303 return stream == SNDRV_PCM_STREAM_PLAYBACK;
304}
305
Kuninori Morimoto00545782010-10-12 18:30:14 +0900306static inline int fsi_is_play(struct snd_pcm_substream *substream)
307{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900308 return fsi_stream_is_play(substream->stream);
309}
310
311static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
312 int is_play)
313{
314 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900315}
316
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900317static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900318{
319 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900320 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900321
322 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900323 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900324 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900325 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900326
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900327 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900328}
329
330static void fsi_stream_push(struct fsi_priv *fsi,
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900331 int is_play,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900332 struct snd_pcm_substream *substream,
333 u32 buffer_len,
334 u32 period_len)
335{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900336 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
337
338 io->substream = substream;
339 io->buff_len = buffer_len;
340 io->buff_offset = 0;
341 io->period_len = period_len;
342 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900343 io->oerr_num = -1; /* ignore 1st err */
344 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900345}
346
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900347static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900348{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900349 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900350 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
351
352
353 if (io->oerr_num > 0)
354 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
355
356 if (io->uerr_num > 0)
357 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900358
359 io->substream = NULL;
360 io->buff_len = 0;
361 io->buff_offset = 0;
362 io->period_len = 0;
363 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900364 io->oerr_num = 0;
365 io->uerr_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900366}
367
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900368static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900369{
370 u32 status;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900371 int data_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900372
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900373 status = is_play ?
374 fsi_reg_read(fsi, DOFF_ST) :
375 fsi_reg_read(fsi, DIFF_ST);
376
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900377 data_num = 0x1ff & (status >> 8);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900378 data_num *= fsi->chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900379
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900380 return data_num;
381}
382
383static int fsi_len2num(int len, int width)
384{
385 return len / width;
386}
387
388#define fsi_num2offset(a, b) fsi_num2len(a, b)
389static int fsi_num2len(int num, int width)
390{
391 return num * width;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900392}
393
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900394static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play)
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900395{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900396 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
397 struct snd_pcm_substream *substream = io->substream;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900398 struct snd_pcm_runtime *runtime = substream->runtime;
399
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900400 return frames_to_bytes(runtime, 1) / fsi->chan_num;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900401}
402
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900403static void fsi_count_fifo_err(struct fsi_priv *fsi)
404{
405 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
406 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
407
408 if (ostatus & ERR_OVER)
409 fsi->playback.oerr_num++;
410
411 if (ostatus & ERR_UNDER)
412 fsi->playback.uerr_num++;
413
414 if (istatus & ERR_OVER)
415 fsi->capture.oerr_num++;
416
417 if (istatus & ERR_UNDER)
418 fsi->capture.uerr_num++;
419
420 fsi_reg_write(fsi, DOFF_ST, 0);
421 fsi_reg_write(fsi, DIFF_ST, 0);
422}
423
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900424/*
425 * dma function
426 */
427
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900428static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900429{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900430 int is_play = fsi_stream_is_play(stream);
431 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
432
433 return io->substream->runtime->dma_area + io->buff_offset;
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900434}
435
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900436static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900437{
438 u16 *start;
439 int i;
440
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900441 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900442
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900443 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900444 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
445}
446
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900447static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900448{
449 u16 *start;
450 int i;
451
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900452 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
453
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900454
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900455 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900456 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
457}
458
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900459static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900460{
461 u32 *start;
462 int i;
463
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900464 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
465
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900466
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900467 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900468 fsi_reg_write(fsi, DODT, *(start + i));
469}
470
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900471static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900472{
473 u32 *start;
474 int i;
475
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900476 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900477
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900478 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900479 *(start + i) = fsi_reg_read(fsi, DIDT);
480}
481
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900482/*
483 * irq function
484 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900485
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900486static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
487{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900488 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900489 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900490
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900491 fsi_core_mask_set(master, imsk, data, data);
492 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900493}
494
495static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
496{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900497 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900498 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900499
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900500 fsi_core_mask_set(master, imsk, data, 0);
501 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900502}
503
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900504static u32 fsi_irq_get_status(struct fsi_master *master)
505{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900506 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900507}
508
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900509static void fsi_irq_clear_status(struct fsi_priv *fsi)
510{
511 u32 data = 0;
512 struct fsi_master *master = fsi_get_master(fsi);
513
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900514 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
515 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900516
517 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900518 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900519}
520
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900521/*
522 * SPDIF master clock function
523 *
524 * These functions are used later FSI2
525 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900526static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
527{
528 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900529 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900530
531 if (master->core->ver < 2) {
532 pr_err("fsi: register access err (%s)\n", __func__);
533 return;
534 }
535
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900536 mask = BP | SE;
537 val = enable ? mask : 0;
538
539 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900540 fsi_core_mask_set(master, a_mclk, mask, val) :
541 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900542}
543
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900544/*
545 * ctrl function
546 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900547
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900548static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
549{
550 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900551 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900552
553 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900554 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900555 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900556 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900557}
558
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900559static void fsi_fifo_init(struct fsi_priv *fsi,
560 int is_play,
561 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900562{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900563 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900564 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900565 u32 shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900566
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900567 /* get on-chip RAM capacity */
568 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900569 shift >>= fsi_get_port_shift(fsi, is_play);
570 shift &= FIFO_SZ_MASK;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900571 io->fifo_max_num = 256 << shift;
572 dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900573
574 /*
575 * The maximum number of sample data varies depending
576 * on the number of channels selected for the format.
577 *
578 * FIFOs are used in 4-channel units in 3-channel mode
579 * and in 8-channel units in 5- to 7-channel mode
580 * meaning that more FIFOs than the required size of DPRAM
581 * are used.
582 *
583 * ex) if 256 words of DP-RAM is connected
584 * 1 channel: 256 (256 x 1 = 256)
585 * 2 channels: 128 (128 x 2 = 256)
586 * 3 channels: 64 ( 64 x 3 = 192)
587 * 4 channels: 64 ( 64 x 4 = 256)
588 * 5 channels: 32 ( 32 x 5 = 160)
589 * 6 channels: 32 ( 32 x 6 = 192)
590 * 7 channels: 32 ( 32 x 7 = 224)
591 * 8 channels: 32 ( 32 x 8 = 256)
592 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900593 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900594 io->fifo_max_num >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900595 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900596 fsi->chan_num, io->fifo_max_num);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900597
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900598 /*
599 * set interrupt generation factor
600 * clear FIFO
601 */
602 if (is_play) {
603 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
604 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
605 } else {
606 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
607 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
608 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900609}
610
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900611static void fsi_soft_all_reset(struct fsi_master *master)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900612{
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900613 /* port AB reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900614 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900615 mdelay(10);
616
617 /* soft reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900618 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
619 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900620 mdelay(10);
621}
622
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900623static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900624{
625 struct snd_pcm_runtime *runtime;
626 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900627 int is_play = fsi_stream_is_play(stream);
628 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900629 int data_residue_num;
630 int data_num;
631 int data_num_max;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900632 int ch_width;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900633 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900634 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900635
636 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900637 !io->substream ||
638 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900639 return -EINVAL;
640
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900641 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900642 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900643 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900644
645 /* FSI FIFO has limit.
646 * So, this driver can not send periods data at a time
647 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900648 if (io->buff_offset >=
649 fsi_num2offset(io->period_num + 1, io->period_len)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900650
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900651 over_period = 1;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900652 io->period_num = (io->period_num + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900653
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900654 if (0 == io->period_num)
655 io->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900656 }
657
658 /* get 1 channel data width */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900659 ch_width = fsi_get_frame_width(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900660
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900661 /* get residue data number of alsa */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900662 data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900663 ch_width);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900664
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900665 if (is_play) {
666 /*
667 * for play-back
668 *
669 * data_num_max : number of FSI fifo free space
670 * data_num : number of ALSA residue data
671 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900672 data_num_max = io->fifo_max_num * fsi->chan_num;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900673 data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900674
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900675 data_num = data_residue_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900676
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900677 switch (ch_width) {
678 case 2:
679 fn = fsi_dma_soft_push16;
680 break;
681 case 4:
682 fn = fsi_dma_soft_push32;
683 break;
684 default:
685 return -EINVAL;
686 }
687 } else {
688 /*
689 * for capture
690 *
691 * data_num_max : number of ALSA free space
692 * data_num : number of data in FSI fifo
693 */
694 data_num_max = data_residue_num;
695 data_num = fsi_get_fifo_data_num(fsi, is_play);
696
697 switch (ch_width) {
698 case 2:
699 fn = fsi_dma_soft_pop16;
700 break;
701 case 4:
702 fn = fsi_dma_soft_pop32;
703 break;
704 default:
705 return -EINVAL;
706 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900707 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900708
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900709 data_num = min(data_num, data_num_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900710
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900711 fn(fsi, data_num);
712
713 /* update buff_offset */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900714 io->buff_offset += fsi_num2offset(data_num, ch_width);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900715
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900716 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900717 snd_pcm_period_elapsed(substream);
718
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900719 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900720}
721
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900722static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900723{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900724 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900725}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900726
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900727static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900728{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900729 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900730}
731
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900732static irqreturn_t fsi_interrupt(int irq, void *data)
733{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900734 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900735 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900736
737 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900738 fsi_master_mask_set(master, SOFT_RST, IR, 0);
739 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900740
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900741 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900742 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900743 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900744 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900745 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900746 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900747 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900748 fsi_data_pop(&master->fsib);
749
750 fsi_count_fifo_err(&master->fsia);
751 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900752
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900753 fsi_irq_clear_status(&master->fsia);
754 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900755
756 return IRQ_HANDLED;
757}
758
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900759/*
760 * dai ops
761 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900762
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900763static int fsi_dai_startup(struct snd_pcm_substream *substream,
764 struct snd_soc_dai *dai)
765{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900766 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900767 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900768 u32 data;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900769 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900770
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900771 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900772
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900773
774 /* clock inversion (CKG2) */
775 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900776 if (SH_FSI_LRM_INV & flags)
777 data |= 1 << 12;
778 if (SH_FSI_BRM_INV & flags)
779 data |= 1 << 8;
780 if (SH_FSI_LRS_INV & flags)
781 data |= 1 << 4;
782 if (SH_FSI_BRS_INV & flags)
783 data |= 1 << 0;
784
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900785 fsi_reg_write(fsi, CKG2, data);
786
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900787 /* irq clear */
788 fsi_irq_disable(fsi, is_play);
789 fsi_irq_clear_status(fsi);
790
791 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900792 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900793
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900794 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900795}
796
797static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
798 struct snd_soc_dai *dai)
799{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900800 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900801 int is_play = fsi_is_play(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000802 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900803 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900804
805 fsi_irq_disable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900806
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900807 if (fsi_is_clk_master(fsi)) {
808 fsi_clk_ctrl(fsi, 0);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000809 set_rate(dai->dev, fsi_is_port_a(fsi), fsi->rate, 0);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900810 }
811
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000812 fsi->rate = 0;
813
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900814 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900815}
816
817static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
818 struct snd_soc_dai *dai)
819{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900820 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900821 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900822 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900823 int ret = 0;
824
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900825 switch (cmd) {
826 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900827 fsi_stream_push(fsi, is_play, substream,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900828 frames_to_bytes(runtime, runtime->buffer_size),
829 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900830 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto9e261bb2010-12-17 12:52:56 +0900831 fsi_irq_enable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900832 break;
833 case SNDRV_PCM_TRIGGER_STOP:
834 fsi_irq_disable(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900835 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900836 break;
837 }
838
839 return ret;
840}
841
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900842static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
843{
844 u32 data = 0;
845
846 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
847 case SND_SOC_DAIFMT_I2S:
848 data = CR_I2S;
849 fsi->chan_num = 2;
850 break;
851 case SND_SOC_DAIFMT_LEFT_J:
852 data = CR_PCM;
853 fsi->chan_num = 2;
854 break;
855 default:
856 return -EINVAL;
857 }
858
859 fsi_reg_write(fsi, DO_FMT, data);
860 fsi_reg_write(fsi, DI_FMT, data);
861
862 return 0;
863}
864
865static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
866{
867 struct fsi_master *master = fsi_get_master(fsi);
868 u32 data = 0;
869
870 if (master->core->ver < 2)
871 return -EINVAL;
872
873 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
874 fsi->chan_num = 2;
875 fsi_spdif_clk_ctrl(fsi, 1);
876 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
877
878 fsi_reg_write(fsi, DO_FMT, data);
879 fsi_reg_write(fsi, DI_FMT, data);
880
881 return 0;
882}
883
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900884static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
885{
886 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900887 struct fsi_master *master = fsi_get_master(fsi);
888 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900889 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900890 u32 data = 0;
891 int ret;
892
893 pm_runtime_get_sync(dai->dev);
894
895 /* set master/slave audio interface */
896 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
897 case SND_SOC_DAIFMT_CBM_CFM:
898 data = DIMD | DOMD;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900899 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900900 break;
901 case SND_SOC_DAIFMT_CBS_CFS:
902 break;
903 default:
904 ret = -EINVAL;
905 goto set_fmt_exit;
906 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900907
908 if (fsi_is_clk_master(fsi) && !set_rate) {
909 dev_err(dai->dev, "platform doesn't have set_rate\n");
910 ret = -EINVAL;
911 goto set_fmt_exit;
912 }
913
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900914 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900915
916 /* set format */
917 switch (flags & SH_FSI_FMT_MASK) {
918 case SH_FSI_FMT_DAI:
919 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
920 break;
921 case SH_FSI_FMT_SPDIF:
922 ret = fsi_set_fmt_spdif(fsi);
923 break;
924 default:
925 ret = -EINVAL;
926 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900927
928set_fmt_exit:
929 pm_runtime_put_sync(dai->dev);
930
931 return ret;
932}
933
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900934static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
935 struct snd_pcm_hw_params *params,
936 struct snd_soc_dai *dai)
937{
938 struct fsi_priv *fsi = fsi_get_priv(substream);
939 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900940 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900941 int fsi_ver = master->core->ver;
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000942 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900943 int ret;
944
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900945 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900946 return 0;
947
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000948 ret = set_rate(dai->dev, fsi_is_port_a(fsi), rate, 1);
949 if (ret < 0) /* error */
950 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900951
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000952 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900953 if (ret > 0) {
954 u32 data = 0;
955
956 switch (ret & SH_FSI_ACKMD_MASK) {
957 default:
958 /* FALL THROUGH */
959 case SH_FSI_ACKMD_512:
960 data |= (0x0 << 12);
961 break;
962 case SH_FSI_ACKMD_256:
963 data |= (0x1 << 12);
964 break;
965 case SH_FSI_ACKMD_128:
966 data |= (0x2 << 12);
967 break;
968 case SH_FSI_ACKMD_64:
969 data |= (0x3 << 12);
970 break;
971 case SH_FSI_ACKMD_32:
972 if (fsi_ver < 2)
973 dev_err(dai->dev, "unsupported ACKMD\n");
974 else
975 data |= (0x4 << 12);
976 break;
977 }
978
979 switch (ret & SH_FSI_BPFMD_MASK) {
980 default:
981 /* FALL THROUGH */
982 case SH_FSI_BPFMD_32:
983 data |= (0x0 << 8);
984 break;
985 case SH_FSI_BPFMD_64:
986 data |= (0x1 << 8);
987 break;
988 case SH_FSI_BPFMD_128:
989 data |= (0x2 << 8);
990 break;
991 case SH_FSI_BPFMD_256:
992 data |= (0x3 << 8);
993 break;
994 case SH_FSI_BPFMD_512:
995 data |= (0x4 << 8);
996 break;
997 case SH_FSI_BPFMD_16:
998 if (fsi_ver < 2)
999 dev_err(dai->dev, "unsupported ACKMD\n");
1000 else
1001 data |= (0x7 << 8);
1002 break;
1003 }
1004
1005 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
1006 udelay(10);
1007 fsi_clk_ctrl(fsi, 1);
1008 ret = 0;
1009 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001010
1011 return ret;
1012
1013}
1014
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001015static struct snd_soc_dai_ops fsi_dai_ops = {
1016 .startup = fsi_dai_startup,
1017 .shutdown = fsi_dai_shutdown,
1018 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001019 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001020 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001021};
1022
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001023/*
1024 * pcm ops
1025 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001026
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001027static struct snd_pcm_hardware fsi_pcm_hardware = {
1028 .info = SNDRV_PCM_INFO_INTERLEAVED |
1029 SNDRV_PCM_INFO_MMAP |
1030 SNDRV_PCM_INFO_MMAP_VALID |
1031 SNDRV_PCM_INFO_PAUSE,
1032 .formats = FSI_FMTS,
1033 .rates = FSI_RATES,
1034 .rate_min = 8000,
1035 .rate_max = 192000,
1036 .channels_min = 1,
1037 .channels_max = 2,
1038 .buffer_bytes_max = 64 * 1024,
1039 .period_bytes_min = 32,
1040 .period_bytes_max = 8192,
1041 .periods_min = 1,
1042 .periods_max = 32,
1043 .fifo_size = 256,
1044};
1045
1046static int fsi_pcm_open(struct snd_pcm_substream *substream)
1047{
1048 struct snd_pcm_runtime *runtime = substream->runtime;
1049 int ret = 0;
1050
1051 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1052
1053 ret = snd_pcm_hw_constraint_integer(runtime,
1054 SNDRV_PCM_HW_PARAM_PERIODS);
1055
1056 return ret;
1057}
1058
1059static int fsi_hw_params(struct snd_pcm_substream *substream,
1060 struct snd_pcm_hw_params *hw_params)
1061{
1062 return snd_pcm_lib_malloc_pages(substream,
1063 params_buffer_bytes(hw_params));
1064}
1065
1066static int fsi_hw_free(struct snd_pcm_substream *substream)
1067{
1068 return snd_pcm_lib_free_pages(substream);
1069}
1070
1071static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1072{
1073 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001074 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001075 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001076 long location;
1077
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001078 location = (io->buff_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001079 if (location < 0)
1080 location = 0;
1081
1082 return bytes_to_frames(runtime, location);
1083}
1084
1085static struct snd_pcm_ops fsi_pcm_ops = {
1086 .open = fsi_pcm_open,
1087 .ioctl = snd_pcm_lib_ioctl,
1088 .hw_params = fsi_hw_params,
1089 .hw_free = fsi_hw_free,
1090 .pointer = fsi_pointer,
1091};
1092
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001093/*
1094 * snd_soc_platform
1095 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001096
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001097#define PREALLOC_BUFFER (32 * 1024)
1098#define PREALLOC_BUFFER_MAX (32 * 1024)
1099
1100static void fsi_pcm_free(struct snd_pcm *pcm)
1101{
1102 snd_pcm_lib_preallocate_free_for_all(pcm);
1103}
1104
1105static int fsi_pcm_new(struct snd_card *card,
1106 struct snd_soc_dai *dai,
1107 struct snd_pcm *pcm)
1108{
1109 /*
1110 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1111 * in MMAP mode (i.e. aplay -M)
1112 */
1113 return snd_pcm_lib_preallocate_pages_for_all(
1114 pcm,
1115 SNDRV_DMA_TYPE_CONTINUOUS,
1116 snd_dma_continuous_data(GFP_KERNEL),
1117 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1118}
1119
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001120/*
1121 * alsa struct
1122 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001123
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001124static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001125 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001126 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001127 .playback = {
1128 .rates = FSI_RATES,
1129 .formats = FSI_FMTS,
1130 .channels_min = 1,
1131 .channels_max = 8,
1132 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001133 .capture = {
1134 .rates = FSI_RATES,
1135 .formats = FSI_FMTS,
1136 .channels_min = 1,
1137 .channels_max = 8,
1138 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001139 .ops = &fsi_dai_ops,
1140 },
1141 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001142 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001143 .playback = {
1144 .rates = FSI_RATES,
1145 .formats = FSI_FMTS,
1146 .channels_min = 1,
1147 .channels_max = 8,
1148 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001149 .capture = {
1150 .rates = FSI_RATES,
1151 .formats = FSI_FMTS,
1152 .channels_min = 1,
1153 .channels_max = 8,
1154 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001155 .ops = &fsi_dai_ops,
1156 },
1157};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001159static struct snd_soc_platform_driver fsi_soc_platform = {
1160 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001161 .pcm_new = fsi_pcm_new,
1162 .pcm_free = fsi_pcm_free,
1163};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001164
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001165/*
1166 * platform function
1167 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001168
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001169static int fsi_probe(struct platform_device *pdev)
1170{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001171 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001172 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001173 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001174 unsigned int irq;
1175 int ret;
1176
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001177 id_entry = pdev->id_entry;
1178 if (!id_entry) {
1179 dev_err(&pdev->dev, "unknown fsi device\n");
1180 return -ENODEV;
1181 }
1182
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001183 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1184 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001185 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001186 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1187 ret = -ENODEV;
1188 goto exit;
1189 }
1190
1191 master = kzalloc(sizeof(*master), GFP_KERNEL);
1192 if (!master) {
1193 dev_err(&pdev->dev, "Could not allocate master\n");
1194 ret = -ENOMEM;
1195 goto exit;
1196 }
1197
1198 master->base = ioremap_nocache(res->start, resource_size(res));
1199 if (!master->base) {
1200 ret = -ENXIO;
1201 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1202 goto exit_kfree;
1203 }
1204
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001205 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001206 master->irq = irq;
1207 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001208 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001209 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001210
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001211 /* FSI A setting */
1212 master->fsia.base = master->base;
1213 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001214
1215 /* FSI B setting */
1216 master->fsib.base = master->base + 0x40;
1217 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001218
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001219 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001220 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001221
Kuninori Morimotob9c9f962011-04-08 15:09:17 +09001222 pm_runtime_get_sync(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001223 fsi_soft_all_reset(master);
Kuninori Morimotob9c9f962011-04-08 15:09:17 +09001224 pm_runtime_put_sync(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001225
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001226 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1227 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001228 if (ret) {
1229 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001230 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001231 }
1232
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001234 if (ret < 0) {
1235 dev_err(&pdev->dev, "cannot snd soc register\n");
1236 goto exit_free_irq;
1237 }
1238
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001239 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1240 ARRAY_SIZE(fsi_soc_dai));
1241 if (ret < 0) {
1242 dev_err(&pdev->dev, "cannot snd dai register\n");
1243 goto exit_snd_soc;
1244 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001245
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001246 return ret;
1247
1248exit_snd_soc:
1249 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001250exit_free_irq:
1251 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001252exit_iounmap:
1253 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001254 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001255exit_kfree:
1256 kfree(master);
1257 master = NULL;
1258exit:
1259 return ret;
1260}
1261
1262static int fsi_remove(struct platform_device *pdev)
1263{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001264 struct fsi_master *master;
1265
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001266 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001267
Kuninori Morimotod985f272011-04-08 15:09:25 +09001268 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001269 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001270
Kuninori Morimotod985f272011-04-08 15:09:25 +09001271 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1272 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001273
1274 iounmap(master->base);
1275 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001276
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001277 return 0;
1278}
1279
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001280static int fsi_runtime_nop(struct device *dev)
1281{
1282 /* Runtime PM callback shared between ->runtime_suspend()
1283 * and ->runtime_resume(). Simply returns success.
1284 *
1285 * This driver re-initializes all registers after
1286 * pm_runtime_get_sync() anyway so there is no need
1287 * to save and restore registers here.
1288 */
1289 return 0;
1290}
1291
1292static struct dev_pm_ops fsi_pm_ops = {
1293 .runtime_suspend = fsi_runtime_nop,
1294 .runtime_resume = fsi_runtime_nop,
1295};
1296
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001297static struct fsi_core fsi1_core = {
1298 .ver = 1,
1299
1300 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001301 .int_st = INT_ST,
1302 .iemsk = IEMSK,
1303 .imsk = IMSK,
1304};
1305
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001306static struct fsi_core fsi2_core = {
1307 .ver = 2,
1308
1309 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001310 .int_st = CPU_INT_ST,
1311 .iemsk = CPU_IEMSK,
1312 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001313 .a_mclk = A_MST_CTLR,
1314 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001315};
1316
1317static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001318 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1319 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001320 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001321};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001322MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001323
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001324static struct platform_driver fsi_driver = {
1325 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001326 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001327 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001328 },
1329 .probe = fsi_probe,
1330 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001331 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001332};
1333
1334static int __init fsi_mobile_init(void)
1335{
1336 return platform_driver_register(&fsi_driver);
1337}
1338
1339static void __exit fsi_mobile_exit(void)
1340{
1341 platform_driver_unregister(&fsi_driver);
1342}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001343
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001344module_init(fsi_mobile_init);
1345module_exit(fsi_mobile_exit);
1346
1347MODULE_LICENSE("GPL");
1348MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1349MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001350MODULE_ALIAS("platform:fsi-pcm-audio");