blob: 605ea7f17cf3f9216fa685944cfed5200cd564d2 [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;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900133
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900134 int buff_offset;
135 int buff_len;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900136 int period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900137 int period_num;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900138
139 int uerr_num;
140 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900141};
142
143struct fsi_priv {
144 void __iomem *base;
145 struct fsi_master *master;
146
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900147 int chan_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900148 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 Morimoto5bfb9ad2010-09-17 13:48:45 +0900351 int data_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900352
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900353 status = is_play ?
354 fsi_reg_read(fsi, DOFF_ST) :
355 fsi_reg_read(fsi, DIFF_ST);
356
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900357 data_num = 0x1ff & (status >> 8);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900358 data_num *= fsi->chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900359
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900360 return data_num;
361}
362
363static int fsi_len2num(int len, int width)
364{
365 return len / width;
366}
367
368#define fsi_num2offset(a, b) fsi_num2len(a, b)
369static int fsi_num2len(int num, int width)
370{
371 return num * width;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900372}
373
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900374static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play)
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900375{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900376 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
377 struct snd_pcm_substream *substream = io->substream;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900378 struct snd_pcm_runtime *runtime = substream->runtime;
379
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900380 return frames_to_bytes(runtime, 1) / fsi->chan_num;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900381}
382
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900383static void fsi_count_fifo_err(struct fsi_priv *fsi)
384{
385 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
386 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
387
388 if (ostatus & ERR_OVER)
389 fsi->playback.oerr_num++;
390
391 if (ostatus & ERR_UNDER)
392 fsi->playback.uerr_num++;
393
394 if (istatus & ERR_OVER)
395 fsi->capture.oerr_num++;
396
397 if (istatus & ERR_UNDER)
398 fsi->capture.uerr_num++;
399
400 fsi_reg_write(fsi, DOFF_ST, 0);
401 fsi_reg_write(fsi, DIFF_ST, 0);
402}
403
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900404/*
405 * dma function
406 */
407
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900408static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900409{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900410 int is_play = fsi_stream_is_play(stream);
411 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
412
413 return io->substream->runtime->dma_area + io->buff_offset;
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900414}
415
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900416static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900417{
418 u16 *start;
419 int i;
420
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900421 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900422
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900423 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900424 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
425}
426
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900427static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900428{
429 u16 *start;
430 int i;
431
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900432 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
433
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900434
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900435 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900436 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
437}
438
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900439static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900440{
441 u32 *start;
442 int i;
443
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900444 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
445
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900446
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900447 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900448 fsi_reg_write(fsi, DODT, *(start + i));
449}
450
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900451static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900452{
453 u32 *start;
454 int i;
455
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900456 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900457
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900458 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900459 *(start + i) = fsi_reg_read(fsi, DIDT);
460}
461
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900462/*
463 * irq function
464 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900465
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900466static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
467{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900468 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900469 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900470
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900471 fsi_core_mask_set(master, imsk, data, data);
472 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900473}
474
475static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
476{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900477 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900478 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900479
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900480 fsi_core_mask_set(master, imsk, data, 0);
481 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900482}
483
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900484static u32 fsi_irq_get_status(struct fsi_master *master)
485{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900486 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900487}
488
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900489static void fsi_irq_clear_status(struct fsi_priv *fsi)
490{
491 u32 data = 0;
492 struct fsi_master *master = fsi_get_master(fsi);
493
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900494 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
495 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900496
497 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900498 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900499}
500
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900501/*
502 * SPDIF master clock function
503 *
504 * These functions are used later FSI2
505 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900506static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
507{
508 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900509 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900510
511 if (master->core->ver < 2) {
512 pr_err("fsi: register access err (%s)\n", __func__);
513 return;
514 }
515
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900516 mask = BP | SE;
517 val = enable ? mask : 0;
518
519 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900520 fsi_core_mask_set(master, a_mclk, mask, val) :
521 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900522}
523
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900524/*
525 * ctrl function
526 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900527
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900528static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
529{
530 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900531 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900532
533 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900534 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900535 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900536 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900537}
538
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900539static void fsi_fifo_init(struct fsi_priv *fsi,
540 int is_play,
541 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900542{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900543 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900544 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900545 u32 shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900546
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900547 /* get on-chip RAM capacity */
548 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900549 shift >>= fsi_get_port_shift(fsi, is_play);
550 shift &= FIFO_SZ_MASK;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900551 io->fifo_max_num = 256 << shift;
552 dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900553
554 /*
555 * The maximum number of sample data varies depending
556 * on the number of channels selected for the format.
557 *
558 * FIFOs are used in 4-channel units in 3-channel mode
559 * and in 8-channel units in 5- to 7-channel mode
560 * meaning that more FIFOs than the required size of DPRAM
561 * are used.
562 *
563 * ex) if 256 words of DP-RAM is connected
564 * 1 channel: 256 (256 x 1 = 256)
565 * 2 channels: 128 (128 x 2 = 256)
566 * 3 channels: 64 ( 64 x 3 = 192)
567 * 4 channels: 64 ( 64 x 4 = 256)
568 * 5 channels: 32 ( 32 x 5 = 160)
569 * 6 channels: 32 ( 32 x 6 = 192)
570 * 7 channels: 32 ( 32 x 7 = 224)
571 * 8 channels: 32 ( 32 x 8 = 256)
572 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900573 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900574 io->fifo_max_num >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900575 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900576 fsi->chan_num, io->fifo_max_num);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900577
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900578 /*
579 * set interrupt generation factor
580 * clear FIFO
581 */
582 if (is_play) {
583 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
584 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
585 } else {
586 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
587 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
588 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900589}
590
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900591static void fsi_soft_all_reset(struct fsi_master *master)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900592{
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900593 /* port AB reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900594 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900595 mdelay(10);
596
597 /* soft reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900598 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
599 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900600 mdelay(10);
601}
602
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900603static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900604{
605 struct snd_pcm_runtime *runtime;
606 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900607 int is_play = fsi_stream_is_play(stream);
608 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900609 int data_residue_num;
610 int data_num;
611 int data_num_max;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900612 int ch_width;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900613 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900614 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900615
616 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900617 !io->substream ||
618 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900619 return -EINVAL;
620
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900621 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900622 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900623 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900624
625 /* FSI FIFO has limit.
626 * So, this driver can not send periods data at a time
627 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900628 if (io->buff_offset >=
629 fsi_num2offset(io->period_num + 1, io->period_len)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900630
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900631 over_period = 1;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900632 io->period_num = (io->period_num + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900633
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900634 if (0 == io->period_num)
635 io->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900636 }
637
638 /* get 1 channel data width */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900639 ch_width = fsi_get_frame_width(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900640
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900641 /* get residue data number of alsa */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900642 data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900643 ch_width);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900644
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900645 if (is_play) {
646 /*
647 * for play-back
648 *
649 * data_num_max : number of FSI fifo free space
650 * data_num : number of ALSA residue data
651 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900652 data_num_max = io->fifo_max_num * fsi->chan_num;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900653 data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900654
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900655 data_num = data_residue_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900656
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900657 switch (ch_width) {
658 case 2:
659 fn = fsi_dma_soft_push16;
660 break;
661 case 4:
662 fn = fsi_dma_soft_push32;
663 break;
664 default:
665 return -EINVAL;
666 }
667 } else {
668 /*
669 * for capture
670 *
671 * data_num_max : number of ALSA free space
672 * data_num : number of data in FSI fifo
673 */
674 data_num_max = data_residue_num;
675 data_num = fsi_get_fifo_data_num(fsi, is_play);
676
677 switch (ch_width) {
678 case 2:
679 fn = fsi_dma_soft_pop16;
680 break;
681 case 4:
682 fn = fsi_dma_soft_pop32;
683 break;
684 default:
685 return -EINVAL;
686 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900687 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900688
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900689 data_num = min(data_num, data_num_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900690
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900691 fn(fsi, data_num);
692
693 /* update buff_offset */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900694 io->buff_offset += fsi_num2offset(data_num, ch_width);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900695
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900696 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900697 snd_pcm_period_elapsed(substream);
698
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900699 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900700}
701
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900702static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900703{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900704 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900705}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900706
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900707static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900708{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900709 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900710}
711
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900712static irqreturn_t fsi_interrupt(int irq, void *data)
713{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900714 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900715 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900716
717 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900718 fsi_master_mask_set(master, SOFT_RST, IR, 0);
719 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900720
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900721 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900722 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900723 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900724 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900725 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900726 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900727 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900728 fsi_data_pop(&master->fsib);
729
730 fsi_count_fifo_err(&master->fsia);
731 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900732
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900733 fsi_irq_clear_status(&master->fsia);
734 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900735
736 return IRQ_HANDLED;
737}
738
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900739/*
740 * dai ops
741 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900742
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900743static int fsi_dai_startup(struct snd_pcm_substream *substream,
744 struct snd_soc_dai *dai)
745{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900746 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900747 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900748 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900749 u32 fmt;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900750 u32 data;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900751 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900752
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900753 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900754
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900755
756 /* clock inversion (CKG2) */
757 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900758 if (SH_FSI_LRM_INV & flags)
759 data |= 1 << 12;
760 if (SH_FSI_BRM_INV & flags)
761 data |= 1 << 8;
762 if (SH_FSI_LRS_INV & flags)
763 data |= 1 << 4;
764 if (SH_FSI_BRS_INV & flags)
765 data |= 1 << 0;
766
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900767 fsi_reg_write(fsi, CKG2, data);
768
769 /* do fmt, di fmt */
770 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900771 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
772 switch (fmt) {
773 case SH_FSI_FMT_MONO:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900774 data = CR_MONO;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900775 fsi->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900776 break;
777 case SH_FSI_FMT_MONO_DELAY:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900778 data = CR_MONO_D;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900779 fsi->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900780 break;
781 case SH_FSI_FMT_PCM:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900782 data = CR_PCM;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900783 fsi->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900784 break;
785 case SH_FSI_FMT_I2S:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900786 data = CR_I2S;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900787 fsi->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900788 break;
789 case SH_FSI_FMT_TDM:
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900790 fsi->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900791 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900792 data = CR_TDM | (fsi->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900793 break;
794 case SH_FSI_FMT_TDM_DELAY:
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900795 fsi->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900796 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900797 data = CR_TDM_D | (fsi->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900798 break;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900799 case SH_FSI_FMT_SPDIF:
800 if (master->core->ver < 2) {
801 dev_err(dai->dev, "This FSI can not use SPDIF\n");
802 return -EINVAL;
803 }
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900804 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900805 fsi->chan_num = 2;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900806 fsi_spdif_clk_ctrl(fsi, 1);
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900807 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900808 break;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900809 default:
810 dev_err(dai->dev, "unknown format.\n");
811 return -EINVAL;
812 }
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900813 is_play ?
814 fsi_reg_write(fsi, DO_FMT, data) :
815 fsi_reg_write(fsi, DI_FMT, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900816
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900817 /* irq clear */
818 fsi_irq_disable(fsi, is_play);
819 fsi_irq_clear_status(fsi);
820
821 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900822 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900823
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900824 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900825}
826
827static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
828 struct snd_soc_dai *dai)
829{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900830 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900831 int is_play = fsi_is_play(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000832 struct fsi_master *master = fsi_get_master(fsi);
833 int (*set_rate)(struct device *dev, int is_porta, int rate, int enable);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900834
835 fsi_irq_disable(fsi, is_play);
836 fsi_clk_ctrl(fsi, 0);
837
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000838 set_rate = master->info->set_rate;
839 if (set_rate && fsi->rate)
840 set_rate(dai->dev, fsi_is_port_a(fsi), fsi->rate, 0);
841 fsi->rate = 0;
842
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900843 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900844}
845
846static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
847 struct snd_soc_dai *dai)
848{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900849 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900850 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900851 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900852 int ret = 0;
853
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900854 switch (cmd) {
855 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900856 fsi_stream_push(fsi, is_play, substream,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900857 frames_to_bytes(runtime, runtime->buffer_size),
858 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900859 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto9e261bb2010-12-17 12:52:56 +0900860 fsi_irq_enable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861 break;
862 case SNDRV_PCM_TRIGGER_STOP:
863 fsi_irq_disable(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900864 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900865 break;
866 }
867
868 return ret;
869}
870
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900871static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
872{
873 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
874 u32 data = 0;
875 int ret;
876
877 pm_runtime_get_sync(dai->dev);
878
879 /* set master/slave audio interface */
880 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
881 case SND_SOC_DAIFMT_CBM_CFM:
882 data = DIMD | DOMD;
883 break;
884 case SND_SOC_DAIFMT_CBS_CFS:
885 break;
886 default:
887 ret = -EINVAL;
888 goto set_fmt_exit;
889 }
890 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
891 ret = 0;
892
893set_fmt_exit:
894 pm_runtime_put_sync(dai->dev);
895
896 return ret;
897}
898
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900899static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
900 struct snd_pcm_hw_params *params,
901 struct snd_soc_dai *dai)
902{
903 struct fsi_priv *fsi = fsi_get_priv(substream);
904 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000905 int (*set_rate)(struct device *dev, int is_porta, int rate, int enable);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900906 int fsi_ver = master->core->ver;
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000907 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900908 int ret;
909
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000910 set_rate = master->info->set_rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900911 if (!set_rate)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900912 return 0;
913
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000914 ret = set_rate(dai->dev, fsi_is_port_a(fsi), rate, 1);
915 if (ret < 0) /* error */
916 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900917
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000918 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900919 if (ret > 0) {
920 u32 data = 0;
921
922 switch (ret & SH_FSI_ACKMD_MASK) {
923 default:
924 /* FALL THROUGH */
925 case SH_FSI_ACKMD_512:
926 data |= (0x0 << 12);
927 break;
928 case SH_FSI_ACKMD_256:
929 data |= (0x1 << 12);
930 break;
931 case SH_FSI_ACKMD_128:
932 data |= (0x2 << 12);
933 break;
934 case SH_FSI_ACKMD_64:
935 data |= (0x3 << 12);
936 break;
937 case SH_FSI_ACKMD_32:
938 if (fsi_ver < 2)
939 dev_err(dai->dev, "unsupported ACKMD\n");
940 else
941 data |= (0x4 << 12);
942 break;
943 }
944
945 switch (ret & SH_FSI_BPFMD_MASK) {
946 default:
947 /* FALL THROUGH */
948 case SH_FSI_BPFMD_32:
949 data |= (0x0 << 8);
950 break;
951 case SH_FSI_BPFMD_64:
952 data |= (0x1 << 8);
953 break;
954 case SH_FSI_BPFMD_128:
955 data |= (0x2 << 8);
956 break;
957 case SH_FSI_BPFMD_256:
958 data |= (0x3 << 8);
959 break;
960 case SH_FSI_BPFMD_512:
961 data |= (0x4 << 8);
962 break;
963 case SH_FSI_BPFMD_16:
964 if (fsi_ver < 2)
965 dev_err(dai->dev, "unsupported ACKMD\n");
966 else
967 data |= (0x7 << 8);
968 break;
969 }
970
971 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
972 udelay(10);
973 fsi_clk_ctrl(fsi, 1);
974 ret = 0;
975 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900976
977 return ret;
978
979}
980
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900981static struct snd_soc_dai_ops fsi_dai_ops = {
982 .startup = fsi_dai_startup,
983 .shutdown = fsi_dai_shutdown,
984 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900985 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900986 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900987};
988
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900989/*
990 * pcm ops
991 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900992
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900993static struct snd_pcm_hardware fsi_pcm_hardware = {
994 .info = SNDRV_PCM_INFO_INTERLEAVED |
995 SNDRV_PCM_INFO_MMAP |
996 SNDRV_PCM_INFO_MMAP_VALID |
997 SNDRV_PCM_INFO_PAUSE,
998 .formats = FSI_FMTS,
999 .rates = FSI_RATES,
1000 .rate_min = 8000,
1001 .rate_max = 192000,
1002 .channels_min = 1,
1003 .channels_max = 2,
1004 .buffer_bytes_max = 64 * 1024,
1005 .period_bytes_min = 32,
1006 .period_bytes_max = 8192,
1007 .periods_min = 1,
1008 .periods_max = 32,
1009 .fifo_size = 256,
1010};
1011
1012static int fsi_pcm_open(struct snd_pcm_substream *substream)
1013{
1014 struct snd_pcm_runtime *runtime = substream->runtime;
1015 int ret = 0;
1016
1017 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1018
1019 ret = snd_pcm_hw_constraint_integer(runtime,
1020 SNDRV_PCM_HW_PARAM_PERIODS);
1021
1022 return ret;
1023}
1024
1025static int fsi_hw_params(struct snd_pcm_substream *substream,
1026 struct snd_pcm_hw_params *hw_params)
1027{
1028 return snd_pcm_lib_malloc_pages(substream,
1029 params_buffer_bytes(hw_params));
1030}
1031
1032static int fsi_hw_free(struct snd_pcm_substream *substream)
1033{
1034 return snd_pcm_lib_free_pages(substream);
1035}
1036
1037static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1038{
1039 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001040 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001041 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001042 long location;
1043
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001044 location = (io->buff_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001045 if (location < 0)
1046 location = 0;
1047
1048 return bytes_to_frames(runtime, location);
1049}
1050
1051static struct snd_pcm_ops fsi_pcm_ops = {
1052 .open = fsi_pcm_open,
1053 .ioctl = snd_pcm_lib_ioctl,
1054 .hw_params = fsi_hw_params,
1055 .hw_free = fsi_hw_free,
1056 .pointer = fsi_pointer,
1057};
1058
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001059/*
1060 * snd_soc_platform
1061 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001062
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001063#define PREALLOC_BUFFER (32 * 1024)
1064#define PREALLOC_BUFFER_MAX (32 * 1024)
1065
1066static void fsi_pcm_free(struct snd_pcm *pcm)
1067{
1068 snd_pcm_lib_preallocate_free_for_all(pcm);
1069}
1070
1071static int fsi_pcm_new(struct snd_card *card,
1072 struct snd_soc_dai *dai,
1073 struct snd_pcm *pcm)
1074{
1075 /*
1076 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1077 * in MMAP mode (i.e. aplay -M)
1078 */
1079 return snd_pcm_lib_preallocate_pages_for_all(
1080 pcm,
1081 SNDRV_DMA_TYPE_CONTINUOUS,
1082 snd_dma_continuous_data(GFP_KERNEL),
1083 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1084}
1085
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001086/*
1087 * alsa struct
1088 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001089
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001090static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001091 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001092 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001093 .playback = {
1094 .rates = FSI_RATES,
1095 .formats = FSI_FMTS,
1096 .channels_min = 1,
1097 .channels_max = 8,
1098 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001099 .capture = {
1100 .rates = FSI_RATES,
1101 .formats = FSI_FMTS,
1102 .channels_min = 1,
1103 .channels_max = 8,
1104 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001105 .ops = &fsi_dai_ops,
1106 },
1107 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001108 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001109 .playback = {
1110 .rates = FSI_RATES,
1111 .formats = FSI_FMTS,
1112 .channels_min = 1,
1113 .channels_max = 8,
1114 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001115 .capture = {
1116 .rates = FSI_RATES,
1117 .formats = FSI_FMTS,
1118 .channels_min = 1,
1119 .channels_max = 8,
1120 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001121 .ops = &fsi_dai_ops,
1122 },
1123};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001124
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001125static struct snd_soc_platform_driver fsi_soc_platform = {
1126 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001127 .pcm_new = fsi_pcm_new,
1128 .pcm_free = fsi_pcm_free,
1129};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001130
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001131/*
1132 * platform function
1133 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001134
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001135static int fsi_probe(struct platform_device *pdev)
1136{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001137 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001138 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001139 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001140 unsigned int irq;
1141 int ret;
1142
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001143 id_entry = pdev->id_entry;
1144 if (!id_entry) {
1145 dev_err(&pdev->dev, "unknown fsi device\n");
1146 return -ENODEV;
1147 }
1148
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1150 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001151 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001152 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1153 ret = -ENODEV;
1154 goto exit;
1155 }
1156
1157 master = kzalloc(sizeof(*master), GFP_KERNEL);
1158 if (!master) {
1159 dev_err(&pdev->dev, "Could not allocate master\n");
1160 ret = -ENOMEM;
1161 goto exit;
1162 }
1163
1164 master->base = ioremap_nocache(res->start, resource_size(res));
1165 if (!master->base) {
1166 ret = -ENXIO;
1167 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1168 goto exit_kfree;
1169 }
1170
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001171 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001172 master->irq = irq;
1173 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001174 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001175 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001176
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001177 /* FSI A setting */
1178 master->fsia.base = master->base;
1179 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001180
1181 /* FSI B setting */
1182 master->fsib.base = master->base + 0x40;
1183 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001184
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001185 pm_runtime_enable(&pdev->dev);
1186 pm_runtime_resume(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001187 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001188
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001189 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001190
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001191 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1192 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001193 if (ret) {
1194 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001195 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001196 }
1197
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001198 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001199 if (ret < 0) {
1200 dev_err(&pdev->dev, "cannot snd soc register\n");
1201 goto exit_free_irq;
1202 }
1203
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204 return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001205
1206exit_free_irq:
1207 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001208exit_iounmap:
1209 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001210 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001211exit_kfree:
1212 kfree(master);
1213 master = NULL;
1214exit:
1215 return ret;
1216}
1217
1218static int fsi_remove(struct platform_device *pdev)
1219{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001220 struct fsi_master *master;
1221
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001222 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001223
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1225 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001226
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001227 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001228
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001229 free_irq(master->irq, master);
1230
1231 iounmap(master->base);
1232 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001233
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001234 return 0;
1235}
1236
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001237static int fsi_runtime_nop(struct device *dev)
1238{
1239 /* Runtime PM callback shared between ->runtime_suspend()
1240 * and ->runtime_resume(). Simply returns success.
1241 *
1242 * This driver re-initializes all registers after
1243 * pm_runtime_get_sync() anyway so there is no need
1244 * to save and restore registers here.
1245 */
1246 return 0;
1247}
1248
1249static struct dev_pm_ops fsi_pm_ops = {
1250 .runtime_suspend = fsi_runtime_nop,
1251 .runtime_resume = fsi_runtime_nop,
1252};
1253
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001254static struct fsi_core fsi1_core = {
1255 .ver = 1,
1256
1257 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001258 .int_st = INT_ST,
1259 .iemsk = IEMSK,
1260 .imsk = IMSK,
1261};
1262
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001263static struct fsi_core fsi2_core = {
1264 .ver = 2,
1265
1266 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001267 .int_st = CPU_INT_ST,
1268 .iemsk = CPU_IEMSK,
1269 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001270 .a_mclk = A_MST_CTLR,
1271 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001272};
1273
1274static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001275 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1276 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001277 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001278};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001279MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001280
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001281static struct platform_driver fsi_driver = {
1282 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001283 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001284 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001285 },
1286 .probe = fsi_probe,
1287 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001288 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001289};
1290
1291static int __init fsi_mobile_init(void)
1292{
1293 return platform_driver_register(&fsi_driver);
1294}
1295
1296static void __exit fsi_mobile_exit(void)
1297{
1298 platform_driver_unregister(&fsi_driver);
1299}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001300
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001301module_init(fsi_mobile_init);
1302module_exit(fsi_mobile_exit);
1303
1304MODULE_LICENSE("GPL");
1305MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1306MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");