blob: 5f39f364effdfa85d263991010cb796c4ff4a224 [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 Morimotoc8fe2572010-09-17 13:48:17 +0900116/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900117 * FSI driver use below type name for variable
118 *
119 * xxx_len : data length
120 * xxx_width : data width
121 * xxx_offset : data offset
122 * xxx_num : number of data
123 */
124
125/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900126 * struct
127 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900128
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900129struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900130 struct snd_pcm_substream *substream;
131
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900132 int fifo_max_num;
133 int chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900134
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900135 int buff_offset;
136 int buff_len;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900137 int period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900138 int period_num;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900139
140 int uerr_num;
141 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900142};
143
144struct fsi_priv {
145 void __iomem *base;
146 struct fsi_master *master;
147
148 struct fsi_stream playback;
149 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900150
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000151 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900152};
153
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900154struct fsi_core {
155 int ver;
156
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900157 u32 int_st;
158 u32 iemsk;
159 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900160 u32 a_mclk;
161 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900162};
163
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900164struct fsi_master {
165 void __iomem *base;
166 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900167 struct fsi_priv fsia;
168 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900169 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900170 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900171 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900172};
173
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900174/*
175 * basic read write function
176 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900177
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100178static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900179{
180 /* valid data area is 24bit */
181 data &= 0x00ffffff;
182
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100183 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900184}
185
186static u32 __fsi_reg_read(u32 reg)
187{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100188 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900189}
190
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100191static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900192{
193 u32 val = __fsi_reg_read(reg);
194
195 val &= ~mask;
196 val |= data & mask;
197
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100198 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900199}
200
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900201#define fsi_reg_write(p, r, d)\
202 __fsi_reg_write((u32)(p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900203
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900204#define fsi_reg_read(p, r)\
205 __fsi_reg_read((u32)(p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900206
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900207#define fsi_reg_mask_set(p, r, m, d)\
208 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900209
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900210#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
211#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
212static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900213{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900214 u32 ret;
215 unsigned long flags;
216
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900217 spin_lock_irqsave(&master->lock, flags);
218 ret = __fsi_reg_read((u32)(master->base + reg));
219 spin_unlock_irqrestore(&master->lock, flags);
220
221 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900222}
223
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900224#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
225#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
226static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900227 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900228{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900229 unsigned long flags;
230
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900231 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100232 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900233 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900234}
235
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900236/*
237 * basic function
238 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900239
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900240static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900241{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900242 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900243}
244
245static int fsi_is_port_a(struct fsi_priv *fsi)
246{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900247 return fsi->master->base == fsi->base;
248}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900249
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900250static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900251{
252 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900253
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000254 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900255}
256
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900257static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900258{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000259 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000261 if (dai->id == 0)
262 return &master->fsia;
263 else
264 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900265}
266
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900267static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
268{
269 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
270}
271
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900272static u32 fsi_get_info_flags(struct fsi_priv *fsi)
273{
274 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900275 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900276
277 return is_porta ? master->info->porta_flags :
278 master->info->portb_flags;
279}
280
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900281static inline int fsi_stream_is_play(int stream)
282{
283 return stream == SNDRV_PCM_STREAM_PLAYBACK;
284}
285
Kuninori Morimoto00545782010-10-12 18:30:14 +0900286static inline int fsi_is_play(struct snd_pcm_substream *substream)
287{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900288 return fsi_stream_is_play(substream->stream);
289}
290
291static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
292 int is_play)
293{
294 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900295}
296
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900297static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900298{
299 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900300 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900301
302 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900303 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900304 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900305 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900306
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900307 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900308}
309
310static void fsi_stream_push(struct fsi_priv *fsi,
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900311 int is_play,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900312 struct snd_pcm_substream *substream,
313 u32 buffer_len,
314 u32 period_len)
315{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900316 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
317
318 io->substream = substream;
319 io->buff_len = buffer_len;
320 io->buff_offset = 0;
321 io->period_len = period_len;
322 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900323 io->oerr_num = -1; /* ignore 1st err */
324 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900325}
326
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900327static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900328{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900329 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900330 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
331
332
333 if (io->oerr_num > 0)
334 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
335
336 if (io->uerr_num > 0)
337 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900338
339 io->substream = NULL;
340 io->buff_len = 0;
341 io->buff_offset = 0;
342 io->period_len = 0;
343 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900344 io->oerr_num = 0;
345 io->uerr_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900346}
347
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900348static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900349{
350 u32 status;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900351 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900352 int data_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900353
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900354 status = is_play ?
355 fsi_reg_read(fsi, DOFF_ST) :
356 fsi_reg_read(fsi, DIFF_ST);
357
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900358 data_num = 0x1ff & (status >> 8);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900359 data_num *= io->chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900360
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900361 return data_num;
362}
363
364static int fsi_len2num(int len, int width)
365{
366 return len / width;
367}
368
369#define fsi_num2offset(a, b) fsi_num2len(a, b)
370static int fsi_num2len(int num, int width)
371{
372 return num * width;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900373}
374
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900375static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play)
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900376{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900377 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
378 struct snd_pcm_substream *substream = io->substream;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900379 struct snd_pcm_runtime *runtime = substream->runtime;
380
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900381 return frames_to_bytes(runtime, 1) / io->chan_num;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900382}
383
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900384static void fsi_count_fifo_err(struct fsi_priv *fsi)
385{
386 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
387 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
388
389 if (ostatus & ERR_OVER)
390 fsi->playback.oerr_num++;
391
392 if (ostatus & ERR_UNDER)
393 fsi->playback.uerr_num++;
394
395 if (istatus & ERR_OVER)
396 fsi->capture.oerr_num++;
397
398 if (istatus & ERR_UNDER)
399 fsi->capture.uerr_num++;
400
401 fsi_reg_write(fsi, DOFF_ST, 0);
402 fsi_reg_write(fsi, DIFF_ST, 0);
403}
404
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900405/*
406 * dma function
407 */
408
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900409static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900410{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900411 int is_play = fsi_stream_is_play(stream);
412 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
413
414 return io->substream->runtime->dma_area + io->buff_offset;
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900415}
416
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900417static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900418{
419 u16 *start;
420 int i;
421
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900422 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900423
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900424 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900425 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
426}
427
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900428static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900429{
430 u16 *start;
431 int i;
432
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900433 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
434
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900435
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900436 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900437 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
438}
439
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900440static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900441{
442 u32 *start;
443 int i;
444
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900445 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
446
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900447
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900448 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900449 fsi_reg_write(fsi, DODT, *(start + i));
450}
451
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900452static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900453{
454 u32 *start;
455 int i;
456
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900457 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900458
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900459 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900460 *(start + i) = fsi_reg_read(fsi, DIDT);
461}
462
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900463/*
464 * irq function
465 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900466
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900467static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
468{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900469 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900470 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900471
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900472 fsi_core_mask_set(master, imsk, data, data);
473 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900474}
475
476static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
477{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900478 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900479 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900480
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900481 fsi_core_mask_set(master, imsk, data, 0);
482 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900483}
484
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900485static u32 fsi_irq_get_status(struct fsi_master *master)
486{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900487 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900488}
489
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900490static void fsi_irq_clear_status(struct fsi_priv *fsi)
491{
492 u32 data = 0;
493 struct fsi_master *master = fsi_get_master(fsi);
494
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900495 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
496 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900497
498 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900499 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900500}
501
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900502/*
503 * SPDIF master clock function
504 *
505 * These functions are used later FSI2
506 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900507static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
508{
509 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900510 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900511
512 if (master->core->ver < 2) {
513 pr_err("fsi: register access err (%s)\n", __func__);
514 return;
515 }
516
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900517 mask = BP | SE;
518 val = enable ? mask : 0;
519
520 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900521 fsi_core_mask_set(master, a_mclk, mask, val) :
522 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900523}
524
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900525/*
526 * ctrl function
527 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900528
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900529static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
530{
531 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900532 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900533
534 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900535 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900536 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900537 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900538}
539
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900540static void fsi_fifo_init(struct fsi_priv *fsi,
541 int is_play,
542 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900543{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900544 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900545 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900546 u32 shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900547
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900548 /* get on-chip RAM capacity */
549 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900550 shift >>= fsi_get_port_shift(fsi, is_play);
551 shift &= FIFO_SZ_MASK;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900552 io->fifo_max_num = 256 << shift;
553 dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900554
555 /*
556 * The maximum number of sample data varies depending
557 * on the number of channels selected for the format.
558 *
559 * FIFOs are used in 4-channel units in 3-channel mode
560 * and in 8-channel units in 5- to 7-channel mode
561 * meaning that more FIFOs than the required size of DPRAM
562 * are used.
563 *
564 * ex) if 256 words of DP-RAM is connected
565 * 1 channel: 256 (256 x 1 = 256)
566 * 2 channels: 128 (128 x 2 = 256)
567 * 3 channels: 64 ( 64 x 3 = 192)
568 * 4 channels: 64 ( 64 x 4 = 256)
569 * 5 channels: 32 ( 32 x 5 = 160)
570 * 6 channels: 32 ( 32 x 6 = 192)
571 * 7 channels: 32 ( 32 x 7 = 224)
572 * 8 channels: 32 ( 32 x 8 = 256)
573 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900574 for (i = 1; i < io->chan_num; i <<= 1)
575 io->fifo_max_num >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900576 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900577 io->chan_num, io->fifo_max_num);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900578
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900579 /*
580 * set interrupt generation factor
581 * clear FIFO
582 */
583 if (is_play) {
584 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
585 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
586 } else {
587 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
588 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
589 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900590}
591
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900592static void fsi_soft_all_reset(struct fsi_master *master)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900593{
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900594 /* port AB reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900595 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900596 mdelay(10);
597
598 /* soft reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900599 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
600 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900601 mdelay(10);
602}
603
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900604static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900605{
606 struct snd_pcm_runtime *runtime;
607 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900608 int is_play = fsi_stream_is_play(stream);
609 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900610 int data_residue_num;
611 int data_num;
612 int data_num_max;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900613 int ch_width;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900614 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900615 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900616
617 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900618 !io->substream ||
619 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900620 return -EINVAL;
621
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900622 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900623 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900624 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900625
626 /* FSI FIFO has limit.
627 * So, this driver can not send periods data at a time
628 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900629 if (io->buff_offset >=
630 fsi_num2offset(io->period_num + 1, io->period_len)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900631
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900632 over_period = 1;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900633 io->period_num = (io->period_num + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900634
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900635 if (0 == io->period_num)
636 io->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900637 }
638
639 /* get 1 channel data width */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900640 ch_width = fsi_get_frame_width(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900641
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900642 /* get residue data number of alsa */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900643 data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900644 ch_width);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900645
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900646 if (is_play) {
647 /*
648 * for play-back
649 *
650 * data_num_max : number of FSI fifo free space
651 * data_num : number of ALSA residue data
652 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900653 data_num_max = io->fifo_max_num * io->chan_num;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900654 data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900655
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900656 data_num = data_residue_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900657
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900658 switch (ch_width) {
659 case 2:
660 fn = fsi_dma_soft_push16;
661 break;
662 case 4:
663 fn = fsi_dma_soft_push32;
664 break;
665 default:
666 return -EINVAL;
667 }
668 } else {
669 /*
670 * for capture
671 *
672 * data_num_max : number of ALSA free space
673 * data_num : number of data in FSI fifo
674 */
675 data_num_max = data_residue_num;
676 data_num = fsi_get_fifo_data_num(fsi, is_play);
677
678 switch (ch_width) {
679 case 2:
680 fn = fsi_dma_soft_pop16;
681 break;
682 case 4:
683 fn = fsi_dma_soft_pop32;
684 break;
685 default:
686 return -EINVAL;
687 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900688 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900689
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900690 data_num = min(data_num, data_num_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900691
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900692 fn(fsi, data_num);
693
694 /* update buff_offset */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900695 io->buff_offset += fsi_num2offset(data_num, ch_width);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900696
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900697 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900698 snd_pcm_period_elapsed(substream);
699
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900700 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900701}
702
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900703static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900704{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900705 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900706}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900707
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900708static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900709{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900710 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900711}
712
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900713static irqreturn_t fsi_interrupt(int irq, void *data)
714{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900715 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900716 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900717
718 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900719 fsi_master_mask_set(master, SOFT_RST, IR, 0);
720 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900721
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900722 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900723 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900724 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900725 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900726 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900727 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900728 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900729 fsi_data_pop(&master->fsib);
730
731 fsi_count_fifo_err(&master->fsia);
732 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900733
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900734 fsi_irq_clear_status(&master->fsia);
735 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900736
737 return IRQ_HANDLED;
738}
739
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900740/*
741 * dai ops
742 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900743
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900744static int fsi_dai_startup(struct snd_pcm_substream *substream,
745 struct snd_soc_dai *dai)
746{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900747 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900748 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900749 struct fsi_stream *io;
750 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900751 u32 fmt;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900752 u32 data;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900753 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900754
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900755 io = fsi_get_stream(fsi, is_play);
756
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900757 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900758
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900759
760 /* clock inversion (CKG2) */
761 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900762 if (SH_FSI_LRM_INV & flags)
763 data |= 1 << 12;
764 if (SH_FSI_BRM_INV & flags)
765 data |= 1 << 8;
766 if (SH_FSI_LRS_INV & flags)
767 data |= 1 << 4;
768 if (SH_FSI_BRS_INV & flags)
769 data |= 1 << 0;
770
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900771 fsi_reg_write(fsi, CKG2, data);
772
773 /* do fmt, di fmt */
774 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900775 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
776 switch (fmt) {
777 case SH_FSI_FMT_MONO:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900778 data = CR_MONO;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900779 io->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900780 break;
781 case SH_FSI_FMT_MONO_DELAY:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900782 data = CR_MONO_D;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900783 io->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900784 break;
785 case SH_FSI_FMT_PCM:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900786 data = CR_PCM;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900787 io->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900788 break;
789 case SH_FSI_FMT_I2S:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900790 data = CR_I2S;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900791 io->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900792 break;
793 case SH_FSI_FMT_TDM:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900794 io->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900795 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900796 data = CR_TDM | (io->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900797 break;
798 case SH_FSI_FMT_TDM_DELAY:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900799 io->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900800 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900801 data = CR_TDM_D | (io->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900802 break;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900803 case SH_FSI_FMT_SPDIF:
804 if (master->core->ver < 2) {
805 dev_err(dai->dev, "This FSI can not use SPDIF\n");
806 return -EINVAL;
807 }
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900808 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900809 io->chan_num = 2;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900810 fsi_spdif_clk_ctrl(fsi, 1);
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900811 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900812 break;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900813 default:
814 dev_err(dai->dev, "unknown format.\n");
815 return -EINVAL;
816 }
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900817 is_play ?
818 fsi_reg_write(fsi, DO_FMT, data) :
819 fsi_reg_write(fsi, DI_FMT, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900820
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900821 /* irq clear */
822 fsi_irq_disable(fsi, is_play);
823 fsi_irq_clear_status(fsi);
824
825 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900826 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900827
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900828 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900829}
830
831static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
832 struct snd_soc_dai *dai)
833{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900834 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900835 int is_play = fsi_is_play(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000836 struct fsi_master *master = fsi_get_master(fsi);
837 int (*set_rate)(struct device *dev, int is_porta, int rate, int enable);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900838
839 fsi_irq_disable(fsi, is_play);
840 fsi_clk_ctrl(fsi, 0);
841
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000842 set_rate = master->info->set_rate;
843 if (set_rate && fsi->rate)
844 set_rate(dai->dev, fsi_is_port_a(fsi), fsi->rate, 0);
845 fsi->rate = 0;
846
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900847 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900848}
849
850static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
851 struct snd_soc_dai *dai)
852{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900853 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900854 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900855 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900856 int ret = 0;
857
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900858 switch (cmd) {
859 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900860 fsi_stream_push(fsi, is_play, substream,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861 frames_to_bytes(runtime, runtime->buffer_size),
862 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900863 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto9e261bb2010-12-17 12:52:56 +0900864 fsi_irq_enable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900865 break;
866 case SNDRV_PCM_TRIGGER_STOP:
867 fsi_irq_disable(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900868 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900869 break;
870 }
871
872 return ret;
873}
874
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900875static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
876{
877 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
878 u32 data = 0;
879 int ret;
880
881 pm_runtime_get_sync(dai->dev);
882
883 /* set master/slave audio interface */
884 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
885 case SND_SOC_DAIFMT_CBM_CFM:
886 data = DIMD | DOMD;
887 break;
888 case SND_SOC_DAIFMT_CBS_CFS:
889 break;
890 default:
891 ret = -EINVAL;
892 goto set_fmt_exit;
893 }
894 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
895 ret = 0;
896
897set_fmt_exit:
898 pm_runtime_put_sync(dai->dev);
899
900 return ret;
901}
902
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900903static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
904 struct snd_pcm_hw_params *params,
905 struct snd_soc_dai *dai)
906{
907 struct fsi_priv *fsi = fsi_get_priv(substream);
908 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000909 int (*set_rate)(struct device *dev, int is_porta, int rate, int enable);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900910 int fsi_ver = master->core->ver;
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000911 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900912 int ret;
913
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000914 set_rate = master->info->set_rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900915 if (!set_rate)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900916 return 0;
917
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000918 ret = set_rate(dai->dev, fsi_is_port_a(fsi), rate, 1);
919 if (ret < 0) /* error */
920 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900921
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000922 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900923 if (ret > 0) {
924 u32 data = 0;
925
926 switch (ret & SH_FSI_ACKMD_MASK) {
927 default:
928 /* FALL THROUGH */
929 case SH_FSI_ACKMD_512:
930 data |= (0x0 << 12);
931 break;
932 case SH_FSI_ACKMD_256:
933 data |= (0x1 << 12);
934 break;
935 case SH_FSI_ACKMD_128:
936 data |= (0x2 << 12);
937 break;
938 case SH_FSI_ACKMD_64:
939 data |= (0x3 << 12);
940 break;
941 case SH_FSI_ACKMD_32:
942 if (fsi_ver < 2)
943 dev_err(dai->dev, "unsupported ACKMD\n");
944 else
945 data |= (0x4 << 12);
946 break;
947 }
948
949 switch (ret & SH_FSI_BPFMD_MASK) {
950 default:
951 /* FALL THROUGH */
952 case SH_FSI_BPFMD_32:
953 data |= (0x0 << 8);
954 break;
955 case SH_FSI_BPFMD_64:
956 data |= (0x1 << 8);
957 break;
958 case SH_FSI_BPFMD_128:
959 data |= (0x2 << 8);
960 break;
961 case SH_FSI_BPFMD_256:
962 data |= (0x3 << 8);
963 break;
964 case SH_FSI_BPFMD_512:
965 data |= (0x4 << 8);
966 break;
967 case SH_FSI_BPFMD_16:
968 if (fsi_ver < 2)
969 dev_err(dai->dev, "unsupported ACKMD\n");
970 else
971 data |= (0x7 << 8);
972 break;
973 }
974
975 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
976 udelay(10);
977 fsi_clk_ctrl(fsi, 1);
978 ret = 0;
979 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900980
981 return ret;
982
983}
984
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900985static struct snd_soc_dai_ops fsi_dai_ops = {
986 .startup = fsi_dai_startup,
987 .shutdown = fsi_dai_shutdown,
988 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900989 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900990 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900991};
992
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900993/*
994 * pcm ops
995 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900996
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900997static struct snd_pcm_hardware fsi_pcm_hardware = {
998 .info = SNDRV_PCM_INFO_INTERLEAVED |
999 SNDRV_PCM_INFO_MMAP |
1000 SNDRV_PCM_INFO_MMAP_VALID |
1001 SNDRV_PCM_INFO_PAUSE,
1002 .formats = FSI_FMTS,
1003 .rates = FSI_RATES,
1004 .rate_min = 8000,
1005 .rate_max = 192000,
1006 .channels_min = 1,
1007 .channels_max = 2,
1008 .buffer_bytes_max = 64 * 1024,
1009 .period_bytes_min = 32,
1010 .period_bytes_max = 8192,
1011 .periods_min = 1,
1012 .periods_max = 32,
1013 .fifo_size = 256,
1014};
1015
1016static int fsi_pcm_open(struct snd_pcm_substream *substream)
1017{
1018 struct snd_pcm_runtime *runtime = substream->runtime;
1019 int ret = 0;
1020
1021 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1022
1023 ret = snd_pcm_hw_constraint_integer(runtime,
1024 SNDRV_PCM_HW_PARAM_PERIODS);
1025
1026 return ret;
1027}
1028
1029static int fsi_hw_params(struct snd_pcm_substream *substream,
1030 struct snd_pcm_hw_params *hw_params)
1031{
1032 return snd_pcm_lib_malloc_pages(substream,
1033 params_buffer_bytes(hw_params));
1034}
1035
1036static int fsi_hw_free(struct snd_pcm_substream *substream)
1037{
1038 return snd_pcm_lib_free_pages(substream);
1039}
1040
1041static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1042{
1043 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001044 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001045 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001046 long location;
1047
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001048 location = (io->buff_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001049 if (location < 0)
1050 location = 0;
1051
1052 return bytes_to_frames(runtime, location);
1053}
1054
1055static struct snd_pcm_ops fsi_pcm_ops = {
1056 .open = fsi_pcm_open,
1057 .ioctl = snd_pcm_lib_ioctl,
1058 .hw_params = fsi_hw_params,
1059 .hw_free = fsi_hw_free,
1060 .pointer = fsi_pointer,
1061};
1062
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001063/*
1064 * snd_soc_platform
1065 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001066
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001067#define PREALLOC_BUFFER (32 * 1024)
1068#define PREALLOC_BUFFER_MAX (32 * 1024)
1069
1070static void fsi_pcm_free(struct snd_pcm *pcm)
1071{
1072 snd_pcm_lib_preallocate_free_for_all(pcm);
1073}
1074
1075static int fsi_pcm_new(struct snd_card *card,
1076 struct snd_soc_dai *dai,
1077 struct snd_pcm *pcm)
1078{
1079 /*
1080 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1081 * in MMAP mode (i.e. aplay -M)
1082 */
1083 return snd_pcm_lib_preallocate_pages_for_all(
1084 pcm,
1085 SNDRV_DMA_TYPE_CONTINUOUS,
1086 snd_dma_continuous_data(GFP_KERNEL),
1087 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1088}
1089
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001090/*
1091 * alsa struct
1092 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001093
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001094static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001095 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001096 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001097 .playback = {
1098 .rates = FSI_RATES,
1099 .formats = FSI_FMTS,
1100 .channels_min = 1,
1101 .channels_max = 8,
1102 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001103 .capture = {
1104 .rates = FSI_RATES,
1105 .formats = FSI_FMTS,
1106 .channels_min = 1,
1107 .channels_max = 8,
1108 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001109 .ops = &fsi_dai_ops,
1110 },
1111 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001112 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001113 .playback = {
1114 .rates = FSI_RATES,
1115 .formats = FSI_FMTS,
1116 .channels_min = 1,
1117 .channels_max = 8,
1118 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001119 .capture = {
1120 .rates = FSI_RATES,
1121 .formats = FSI_FMTS,
1122 .channels_min = 1,
1123 .channels_max = 8,
1124 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001125 .ops = &fsi_dai_ops,
1126 },
1127};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001128
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001129static struct snd_soc_platform_driver fsi_soc_platform = {
1130 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001131 .pcm_new = fsi_pcm_new,
1132 .pcm_free = fsi_pcm_free,
1133};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001134
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001135/*
1136 * platform function
1137 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001138
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001139static int fsi_probe(struct platform_device *pdev)
1140{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001141 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001142 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001143 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001144 unsigned int irq;
1145 int ret;
1146
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001147 id_entry = pdev->id_entry;
1148 if (!id_entry) {
1149 dev_err(&pdev->dev, "unknown fsi device\n");
1150 return -ENODEV;
1151 }
1152
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001153 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1154 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001155 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001156 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1157 ret = -ENODEV;
1158 goto exit;
1159 }
1160
1161 master = kzalloc(sizeof(*master), GFP_KERNEL);
1162 if (!master) {
1163 dev_err(&pdev->dev, "Could not allocate master\n");
1164 ret = -ENOMEM;
1165 goto exit;
1166 }
1167
1168 master->base = ioremap_nocache(res->start, resource_size(res));
1169 if (!master->base) {
1170 ret = -ENXIO;
1171 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1172 goto exit_kfree;
1173 }
1174
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001175 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001176 master->irq = irq;
1177 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001178 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001179 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001180
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001181 /* FSI A setting */
1182 master->fsia.base = master->base;
1183 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001184
1185 /* FSI B setting */
1186 master->fsib.base = master->base + 0x40;
1187 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001188
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001189 pm_runtime_enable(&pdev->dev);
1190 pm_runtime_resume(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001191 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001192
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001193 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001194
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001195 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1196 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001197 if (ret) {
1198 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001199 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001200 }
1201
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001202 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001203 if (ret < 0) {
1204 dev_err(&pdev->dev, "cannot snd soc register\n");
1205 goto exit_free_irq;
1206 }
1207
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001208 return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001209
1210exit_free_irq:
1211 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001212exit_iounmap:
1213 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001214 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001215exit_kfree:
1216 kfree(master);
1217 master = NULL;
1218exit:
1219 return ret;
1220}
1221
1222static int fsi_remove(struct platform_device *pdev)
1223{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001224 struct fsi_master *master;
1225
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001226 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001227
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1229 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001230
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001231 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001232
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001233 free_irq(master->irq, master);
1234
1235 iounmap(master->base);
1236 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001237
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001238 return 0;
1239}
1240
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001241static int fsi_runtime_nop(struct device *dev)
1242{
1243 /* Runtime PM callback shared between ->runtime_suspend()
1244 * and ->runtime_resume(). Simply returns success.
1245 *
1246 * This driver re-initializes all registers after
1247 * pm_runtime_get_sync() anyway so there is no need
1248 * to save and restore registers here.
1249 */
1250 return 0;
1251}
1252
1253static struct dev_pm_ops fsi_pm_ops = {
1254 .runtime_suspend = fsi_runtime_nop,
1255 .runtime_resume = fsi_runtime_nop,
1256};
1257
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001258static struct fsi_core fsi1_core = {
1259 .ver = 1,
1260
1261 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001262 .int_st = INT_ST,
1263 .iemsk = IEMSK,
1264 .imsk = IMSK,
1265};
1266
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001267static struct fsi_core fsi2_core = {
1268 .ver = 2,
1269
1270 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001271 .int_st = CPU_INT_ST,
1272 .iemsk = CPU_IEMSK,
1273 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001274 .a_mclk = A_MST_CTLR,
1275 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001276};
1277
1278static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001279 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1280 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001281 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001282};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001283MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001284
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001285static struct platform_driver fsi_driver = {
1286 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001287 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001288 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001289 },
1290 .probe = fsi_probe,
1291 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001292 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001293};
1294
1295static int __init fsi_mobile_init(void)
1296{
1297 return platform_driver_register(&fsi_driver);
1298}
1299
1300static void __exit fsi_mobile_exit(void)
1301{
1302 platform_driver_unregister(&fsi_driver);
1303}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001304
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001305module_init(fsi_mobile_init);
1306module_exit(fsi_mobile_exit);
1307
1308MODULE_LICENSE("GPL");
1309MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1310MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");