blob: 3c53693d726688c4e5546b2d87d639eb57e2f640 [file] [log] [blame]
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001/*
2 * Fifo-attached Serial Interface (FSI) support for SH7724
3 *
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on ssi.c
8 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090015#include <linux/delay.h>
Kuninori Morimoto785d1c42009-11-30 20:24:48 +090016#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090017#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090019#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090020#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090021
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090022/* PortA/PortB register */
23#define REG_DO_FMT 0x0000
24#define REG_DOFF_CTL 0x0004
25#define REG_DOFF_ST 0x0008
26#define REG_DI_FMT 0x000C
27#define REG_DIFF_CTL 0x0010
28#define REG_DIFF_ST 0x0014
29#define REG_CKG1 0x0018
30#define REG_CKG2 0x001C
31#define REG_DIDT 0x0020
32#define REG_DODT 0x0024
33#define REG_MUTE_ST 0x0028
34#define REG_OUT_SEL 0x0030
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090035
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090036/* master register */
37#define MST_CLK_RST 0x0210
38#define MST_SOFT_RST 0x0214
39#define MST_FIFO_SZ 0x0218
40
41/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090042#define A_MST_CTLR 0x0180
43#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090044#define CPU_INT_ST 0x01F4
45#define CPU_IEMSK 0x01F8
46#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090047#define INT_ST 0x0200
48#define IEMSK 0x0204
49#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090050
51/* DO_FMT */
52/* DI_FMT */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090053#define CR_BWS_24 (0x0 << 20) /* FSI2 */
54#define CR_BWS_16 (0x1 << 20) /* FSI2 */
55#define CR_BWS_20 (0x2 << 20) /* FSI2 */
56
57#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
58#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
59#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
60
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090061#define CR_MONO (0x0 << 4)
62#define CR_MONO_D (0x1 << 4)
63#define CR_PCM (0x2 << 4)
64#define CR_I2S (0x3 << 4)
65#define CR_TDM (0x4 << 4)
66#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090067
68/* DOFF_CTL */
69/* DIFF_CTL */
70#define IRQ_HALF 0x00100000
71#define FIFO_CLR 0x00000001
72
73/* DOFF_ST */
74#define ERR_OVER 0x00000010
75#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090076#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090077
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090078/* CKG1 */
79#define ACKMD_MASK 0x00007000
80#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090081#define DIMD (1 << 4)
82#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090083
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090084/* A/B MST_CTLR */
85#define BP (1 << 4) /* Fix the signal of Biphase output */
86#define SE (1 << 0) /* Fix the master clock */
87
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090088/* CLK_RST */
89#define B_CLK 0x00000010
90#define A_CLK 0x00000001
91
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090092/* IO SHIFT / MACRO */
93#define BI_SHIFT 12
94#define BO_SHIFT 8
95#define AI_SHIFT 4
96#define AO_SHIFT 0
97#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090098
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +090099/* SOFT_RST */
100#define PBSR (1 << 12) /* Port B Software Reset */
101#define PASR (1 << 8) /* Port A Software Reset */
102#define IR (1 << 4) /* Interrupt Reset */
103#define FSISR (1 << 0) /* Software Reset */
104
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900105/* OUT_SEL (FSI2) */
106#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
107 /* 1: Biphase and serial */
108
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900109/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900110#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900111
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900112#define FSI_RATES SNDRV_PCM_RATE_8000_96000
113
114#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
115
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900116typedef int (*set_rate_func)(struct device *dev, int is_porta, int rate, int enable);
117
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900118/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900119 * FSI driver use below type name for variable
120 *
121 * xxx_len : data length
122 * xxx_width : data width
123 * xxx_offset : data offset
124 * xxx_num : number of data
125 */
126
127/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900128 * struct
129 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900130
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900131struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900132 struct snd_pcm_substream *substream;
133
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900134 int fifo_max_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900135
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900136 int buff_offset;
137 int buff_len;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900138 int period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900139 int period_num;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900140
141 int uerr_num;
142 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900143};
144
145struct fsi_priv {
146 void __iomem *base;
147 struct fsi_master *master;
148
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900149 int chan_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900150 struct fsi_stream playback;
151 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900152
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000153 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900154};
155
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900156struct fsi_core {
157 int ver;
158
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900159 u32 int_st;
160 u32 iemsk;
161 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900162 u32 a_mclk;
163 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900164};
165
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900166struct fsi_master {
167 void __iomem *base;
168 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900169 struct fsi_priv fsia;
170 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900171 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900172 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900173 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900174};
175
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900176/*
177 * basic read write function
178 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900179
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100180static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900181{
182 /* valid data area is 24bit */
183 data &= 0x00ffffff;
184
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100185 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900186}
187
188static u32 __fsi_reg_read(u32 reg)
189{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100190 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900191}
192
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100193static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900194{
195 u32 val = __fsi_reg_read(reg);
196
197 val &= ~mask;
198 val |= data & mask;
199
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100200 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900201}
202
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900203#define fsi_reg_write(p, r, d)\
204 __fsi_reg_write((u32)(p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900205
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900206#define fsi_reg_read(p, r)\
207 __fsi_reg_read((u32)(p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900208
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900209#define fsi_reg_mask_set(p, r, m, d)\
210 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900211
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900212#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
213#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
214static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900215{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900216 u32 ret;
217 unsigned long flags;
218
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900219 spin_lock_irqsave(&master->lock, flags);
220 ret = __fsi_reg_read((u32)(master->base + reg));
221 spin_unlock_irqrestore(&master->lock, flags);
222
223 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900224}
225
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900226#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
227#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
228static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900229 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900230{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900231 unsigned long flags;
232
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900233 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100234 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900235 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900236}
237
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900238/*
239 * basic function
240 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900241
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900242static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900243{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900244 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900245}
246
247static int fsi_is_port_a(struct fsi_priv *fsi)
248{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900249 return fsi->master->base == fsi->base;
250}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900251
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900252static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900253{
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900255
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000256 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900257}
258
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900259static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900260{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000261 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900262
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000263 if (dai->id == 0)
264 return &master->fsia;
265 else
266 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900267}
268
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900269static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
270{
271 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
272}
273
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900274static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
275{
276 if (!master->info)
277 return NULL;
278
279 return master->info->set_rate;
280}
281
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900282static u32 fsi_get_info_flags(struct fsi_priv *fsi)
283{
284 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900285 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900286
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900287 if (!master->info)
288 return 0;
289
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900290 return is_porta ? master->info->porta_flags :
291 master->info->portb_flags;
292}
293
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900294static inline int fsi_stream_is_play(int stream)
295{
296 return stream == SNDRV_PCM_STREAM_PLAYBACK;
297}
298
Kuninori Morimoto00545782010-10-12 18:30:14 +0900299static inline int fsi_is_play(struct snd_pcm_substream *substream)
300{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900301 return fsi_stream_is_play(substream->stream);
302}
303
304static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
305 int is_play)
306{
307 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900308}
309
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900310static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900311{
312 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900313 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900314
315 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900316 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900317 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900318 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900319
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900320 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900321}
322
323static void fsi_stream_push(struct fsi_priv *fsi,
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900324 int is_play,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900325 struct snd_pcm_substream *substream,
326 u32 buffer_len,
327 u32 period_len)
328{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900329 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
330
331 io->substream = substream;
332 io->buff_len = buffer_len;
333 io->buff_offset = 0;
334 io->period_len = period_len;
335 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900336 io->oerr_num = -1; /* ignore 1st err */
337 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900338}
339
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900340static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900341{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900342 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900343 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
344
345
346 if (io->oerr_num > 0)
347 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
348
349 if (io->uerr_num > 0)
350 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900351
352 io->substream = NULL;
353 io->buff_len = 0;
354 io->buff_offset = 0;
355 io->period_len = 0;
356 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900357 io->oerr_num = 0;
358 io->uerr_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900359}
360
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900361static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900362{
363 u32 status;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900364 int data_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900365
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900366 status = is_play ?
367 fsi_reg_read(fsi, DOFF_ST) :
368 fsi_reg_read(fsi, DIFF_ST);
369
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900370 data_num = 0x1ff & (status >> 8);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900371 data_num *= fsi->chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900372
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900373 return data_num;
374}
375
376static int fsi_len2num(int len, int width)
377{
378 return len / width;
379}
380
381#define fsi_num2offset(a, b) fsi_num2len(a, b)
382static int fsi_num2len(int num, int width)
383{
384 return num * width;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900385}
386
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900387static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play)
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900388{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900389 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
390 struct snd_pcm_substream *substream = io->substream;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900391 struct snd_pcm_runtime *runtime = substream->runtime;
392
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900393 return frames_to_bytes(runtime, 1) / fsi->chan_num;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900394}
395
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900396static void fsi_count_fifo_err(struct fsi_priv *fsi)
397{
398 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
399 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
400
401 if (ostatus & ERR_OVER)
402 fsi->playback.oerr_num++;
403
404 if (ostatus & ERR_UNDER)
405 fsi->playback.uerr_num++;
406
407 if (istatus & ERR_OVER)
408 fsi->capture.oerr_num++;
409
410 if (istatus & ERR_UNDER)
411 fsi->capture.uerr_num++;
412
413 fsi_reg_write(fsi, DOFF_ST, 0);
414 fsi_reg_write(fsi, DIFF_ST, 0);
415}
416
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900417/*
418 * dma function
419 */
420
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900421static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900422{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900423 int is_play = fsi_stream_is_play(stream);
424 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
425
426 return io->substream->runtime->dma_area + io->buff_offset;
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900427}
428
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900429static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900430{
431 u16 *start;
432 int i;
433
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900434 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
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 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
438}
439
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900440static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900441{
442 u16 *start;
443 int i;
444
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900445 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
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 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
450}
451
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900452static void fsi_dma_soft_push32(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_PLAYBACK);
458
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900459
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900460 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900461 fsi_reg_write(fsi, DODT, *(start + i));
462}
463
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900464static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900465{
466 u32 *start;
467 int i;
468
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900469 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900470
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900471 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900472 *(start + i) = fsi_reg_read(fsi, DIDT);
473}
474
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900475/*
476 * irq function
477 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900478
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900479static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
480{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900481 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900482 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900483
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900484 fsi_core_mask_set(master, imsk, data, data);
485 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900486}
487
488static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
489{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900490 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900491 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900492
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900493 fsi_core_mask_set(master, imsk, data, 0);
494 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900495}
496
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900497static u32 fsi_irq_get_status(struct fsi_master *master)
498{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900499 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900500}
501
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900502static void fsi_irq_clear_status(struct fsi_priv *fsi)
503{
504 u32 data = 0;
505 struct fsi_master *master = fsi_get_master(fsi);
506
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900507 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
508 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900509
510 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900511 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900512}
513
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900514/*
515 * SPDIF master clock function
516 *
517 * These functions are used later FSI2
518 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900519static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
520{
521 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900522 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900523
524 if (master->core->ver < 2) {
525 pr_err("fsi: register access err (%s)\n", __func__);
526 return;
527 }
528
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900529 mask = BP | SE;
530 val = enable ? mask : 0;
531
532 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900533 fsi_core_mask_set(master, a_mclk, mask, val) :
534 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900535}
536
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900537/*
538 * ctrl function
539 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900540
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900541static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
542{
543 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900544 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900545
546 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900547 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900548 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900549 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900550}
551
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900552static void fsi_fifo_init(struct fsi_priv *fsi,
553 int is_play,
554 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900555{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900556 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900557 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900558 u32 shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900559
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900560 /* get on-chip RAM capacity */
561 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900562 shift >>= fsi_get_port_shift(fsi, is_play);
563 shift &= FIFO_SZ_MASK;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900564 io->fifo_max_num = 256 << shift;
565 dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900566
567 /*
568 * The maximum number of sample data varies depending
569 * on the number of channels selected for the format.
570 *
571 * FIFOs are used in 4-channel units in 3-channel mode
572 * and in 8-channel units in 5- to 7-channel mode
573 * meaning that more FIFOs than the required size of DPRAM
574 * are used.
575 *
576 * ex) if 256 words of DP-RAM is connected
577 * 1 channel: 256 (256 x 1 = 256)
578 * 2 channels: 128 (128 x 2 = 256)
579 * 3 channels: 64 ( 64 x 3 = 192)
580 * 4 channels: 64 ( 64 x 4 = 256)
581 * 5 channels: 32 ( 32 x 5 = 160)
582 * 6 channels: 32 ( 32 x 6 = 192)
583 * 7 channels: 32 ( 32 x 7 = 224)
584 * 8 channels: 32 ( 32 x 8 = 256)
585 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900586 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900587 io->fifo_max_num >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900588 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900589 fsi->chan_num, io->fifo_max_num);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900590
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900591 /*
592 * set interrupt generation factor
593 * clear FIFO
594 */
595 if (is_play) {
596 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
597 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
598 } else {
599 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
600 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
601 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900602}
603
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900604static void fsi_soft_all_reset(struct fsi_master *master)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900605{
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900606 /* port AB reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900607 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900608 mdelay(10);
609
610 /* soft reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900611 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
612 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900613 mdelay(10);
614}
615
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900616static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900617{
618 struct snd_pcm_runtime *runtime;
619 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900620 int is_play = fsi_stream_is_play(stream);
621 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900622 int data_residue_num;
623 int data_num;
624 int data_num_max;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900625 int ch_width;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900626 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900627 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900628
629 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900630 !io->substream ||
631 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900632 return -EINVAL;
633
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900634 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900635 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900636 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900637
638 /* FSI FIFO has limit.
639 * So, this driver can not send periods data at a time
640 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900641 if (io->buff_offset >=
642 fsi_num2offset(io->period_num + 1, io->period_len)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900643
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900644 over_period = 1;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900645 io->period_num = (io->period_num + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900646
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900647 if (0 == io->period_num)
648 io->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900649 }
650
651 /* get 1 channel data width */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900652 ch_width = fsi_get_frame_width(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900653
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900654 /* get residue data number of alsa */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900655 data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900656 ch_width);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900657
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900658 if (is_play) {
659 /*
660 * for play-back
661 *
662 * data_num_max : number of FSI fifo free space
663 * data_num : number of ALSA residue data
664 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900665 data_num_max = io->fifo_max_num * fsi->chan_num;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900666 data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900667
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900668 data_num = data_residue_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900669
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900670 switch (ch_width) {
671 case 2:
672 fn = fsi_dma_soft_push16;
673 break;
674 case 4:
675 fn = fsi_dma_soft_push32;
676 break;
677 default:
678 return -EINVAL;
679 }
680 } else {
681 /*
682 * for capture
683 *
684 * data_num_max : number of ALSA free space
685 * data_num : number of data in FSI fifo
686 */
687 data_num_max = data_residue_num;
688 data_num = fsi_get_fifo_data_num(fsi, is_play);
689
690 switch (ch_width) {
691 case 2:
692 fn = fsi_dma_soft_pop16;
693 break;
694 case 4:
695 fn = fsi_dma_soft_pop32;
696 break;
697 default:
698 return -EINVAL;
699 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900700 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900701
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900702 data_num = min(data_num, data_num_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900703
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900704 fn(fsi, data_num);
705
706 /* update buff_offset */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900707 io->buff_offset += fsi_num2offset(data_num, ch_width);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900708
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900709 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900710 snd_pcm_period_elapsed(substream);
711
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900712 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900713}
714
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900715static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900716{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900717 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900718}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900719
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900720static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900721{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900722 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900723}
724
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900725static irqreturn_t fsi_interrupt(int irq, void *data)
726{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900727 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900728 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900729
730 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900731 fsi_master_mask_set(master, SOFT_RST, IR, 0);
732 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900733
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900734 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900735 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900736 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900737 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900738 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900739 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900740 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900741 fsi_data_pop(&master->fsib);
742
743 fsi_count_fifo_err(&master->fsia);
744 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900745
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900746 fsi_irq_clear_status(&master->fsia);
747 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900748
749 return IRQ_HANDLED;
750}
751
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900752/*
753 * dai ops
754 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900755
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900756static int fsi_dai_startup(struct snd_pcm_substream *substream,
757 struct snd_soc_dai *dai)
758{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900759 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900760 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900761 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900762 u32 fmt;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900763 u32 data;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900764 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900765
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900766 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900767
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900768
769 /* clock inversion (CKG2) */
770 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900771 if (SH_FSI_LRM_INV & flags)
772 data |= 1 << 12;
773 if (SH_FSI_BRM_INV & flags)
774 data |= 1 << 8;
775 if (SH_FSI_LRS_INV & flags)
776 data |= 1 << 4;
777 if (SH_FSI_BRS_INV & flags)
778 data |= 1 << 0;
779
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900780 fsi_reg_write(fsi, CKG2, data);
781
782 /* do fmt, di fmt */
783 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900784 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
785 switch (fmt) {
786 case SH_FSI_FMT_MONO:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900787 data = CR_MONO;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900788 fsi->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900789 break;
790 case SH_FSI_FMT_MONO_DELAY:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900791 data = CR_MONO_D;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900792 fsi->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900793 break;
794 case SH_FSI_FMT_PCM:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900795 data = CR_PCM;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900796 fsi->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900797 break;
798 case SH_FSI_FMT_I2S:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900799 data = CR_I2S;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900800 fsi->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900801 break;
802 case SH_FSI_FMT_TDM:
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900803 fsi->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900804 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900805 data = CR_TDM | (fsi->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900806 break;
807 case SH_FSI_FMT_TDM_DELAY:
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900808 fsi->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900809 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900810 data = CR_TDM_D | (fsi->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900811 break;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900812 case SH_FSI_FMT_SPDIF:
813 if (master->core->ver < 2) {
814 dev_err(dai->dev, "This FSI can not use SPDIF\n");
815 return -EINVAL;
816 }
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900817 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900818 fsi->chan_num = 2;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900819 fsi_spdif_clk_ctrl(fsi, 1);
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900820 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900821 break;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900822 default:
823 dev_err(dai->dev, "unknown format.\n");
824 return -EINVAL;
825 }
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900826 is_play ?
827 fsi_reg_write(fsi, DO_FMT, data) :
828 fsi_reg_write(fsi, DI_FMT, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900829
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900830 /* irq clear */
831 fsi_irq_disable(fsi, is_play);
832 fsi_irq_clear_status(fsi);
833
834 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900835 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900836
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900837 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900838}
839
840static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
841 struct snd_soc_dai *dai)
842{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900843 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900844 int is_play = fsi_is_play(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000845 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900846 set_rate_func set_rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900847
848 fsi_irq_disable(fsi, is_play);
849 fsi_clk_ctrl(fsi, 0);
850
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900851 set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000852 if (set_rate && fsi->rate)
853 set_rate(dai->dev, fsi_is_port_a(fsi), fsi->rate, 0);
854 fsi->rate = 0;
855
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900856 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900857}
858
859static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
860 struct snd_soc_dai *dai)
861{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900862 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900863 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900864 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900865 int ret = 0;
866
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900867 switch (cmd) {
868 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900869 fsi_stream_push(fsi, is_play, substream,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900870 frames_to_bytes(runtime, runtime->buffer_size),
871 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900872 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto9e261bb2010-12-17 12:52:56 +0900873 fsi_irq_enable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900874 break;
875 case SNDRV_PCM_TRIGGER_STOP:
876 fsi_irq_disable(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900877 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900878 break;
879 }
880
881 return ret;
882}
883
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900884static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
885{
886 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
887 u32 data = 0;
888 int ret;
889
890 pm_runtime_get_sync(dai->dev);
891
892 /* set master/slave audio interface */
893 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
894 case SND_SOC_DAIFMT_CBM_CFM:
895 data = DIMD | DOMD;
896 break;
897 case SND_SOC_DAIFMT_CBS_CFS:
898 break;
899 default:
900 ret = -EINVAL;
901 goto set_fmt_exit;
902 }
903 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
904 ret = 0;
905
906set_fmt_exit:
907 pm_runtime_put_sync(dai->dev);
908
909 return ret;
910}
911
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900912static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
913 struct snd_pcm_hw_params *params,
914 struct snd_soc_dai *dai)
915{
916 struct fsi_priv *fsi = fsi_get_priv(substream);
917 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900918 set_rate_func set_rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900919 int fsi_ver = master->core->ver;
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000920 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900921 int ret;
922
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900923 set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900924 if (!set_rate)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900925 return 0;
926
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000927 ret = set_rate(dai->dev, fsi_is_port_a(fsi), rate, 1);
928 if (ret < 0) /* error */
929 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900930
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000931 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900932 if (ret > 0) {
933 u32 data = 0;
934
935 switch (ret & SH_FSI_ACKMD_MASK) {
936 default:
937 /* FALL THROUGH */
938 case SH_FSI_ACKMD_512:
939 data |= (0x0 << 12);
940 break;
941 case SH_FSI_ACKMD_256:
942 data |= (0x1 << 12);
943 break;
944 case SH_FSI_ACKMD_128:
945 data |= (0x2 << 12);
946 break;
947 case SH_FSI_ACKMD_64:
948 data |= (0x3 << 12);
949 break;
950 case SH_FSI_ACKMD_32:
951 if (fsi_ver < 2)
952 dev_err(dai->dev, "unsupported ACKMD\n");
953 else
954 data |= (0x4 << 12);
955 break;
956 }
957
958 switch (ret & SH_FSI_BPFMD_MASK) {
959 default:
960 /* FALL THROUGH */
961 case SH_FSI_BPFMD_32:
962 data |= (0x0 << 8);
963 break;
964 case SH_FSI_BPFMD_64:
965 data |= (0x1 << 8);
966 break;
967 case SH_FSI_BPFMD_128:
968 data |= (0x2 << 8);
969 break;
970 case SH_FSI_BPFMD_256:
971 data |= (0x3 << 8);
972 break;
973 case SH_FSI_BPFMD_512:
974 data |= (0x4 << 8);
975 break;
976 case SH_FSI_BPFMD_16:
977 if (fsi_ver < 2)
978 dev_err(dai->dev, "unsupported ACKMD\n");
979 else
980 data |= (0x7 << 8);
981 break;
982 }
983
984 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
985 udelay(10);
986 fsi_clk_ctrl(fsi, 1);
987 ret = 0;
988 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900989
990 return ret;
991
992}
993
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900994static struct snd_soc_dai_ops fsi_dai_ops = {
995 .startup = fsi_dai_startup,
996 .shutdown = fsi_dai_shutdown,
997 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900998 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900999 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001000};
1001
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001002/*
1003 * pcm ops
1004 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001005
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001006static struct snd_pcm_hardware fsi_pcm_hardware = {
1007 .info = SNDRV_PCM_INFO_INTERLEAVED |
1008 SNDRV_PCM_INFO_MMAP |
1009 SNDRV_PCM_INFO_MMAP_VALID |
1010 SNDRV_PCM_INFO_PAUSE,
1011 .formats = FSI_FMTS,
1012 .rates = FSI_RATES,
1013 .rate_min = 8000,
1014 .rate_max = 192000,
1015 .channels_min = 1,
1016 .channels_max = 2,
1017 .buffer_bytes_max = 64 * 1024,
1018 .period_bytes_min = 32,
1019 .period_bytes_max = 8192,
1020 .periods_min = 1,
1021 .periods_max = 32,
1022 .fifo_size = 256,
1023};
1024
1025static int fsi_pcm_open(struct snd_pcm_substream *substream)
1026{
1027 struct snd_pcm_runtime *runtime = substream->runtime;
1028 int ret = 0;
1029
1030 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1031
1032 ret = snd_pcm_hw_constraint_integer(runtime,
1033 SNDRV_PCM_HW_PARAM_PERIODS);
1034
1035 return ret;
1036}
1037
1038static int fsi_hw_params(struct snd_pcm_substream *substream,
1039 struct snd_pcm_hw_params *hw_params)
1040{
1041 return snd_pcm_lib_malloc_pages(substream,
1042 params_buffer_bytes(hw_params));
1043}
1044
1045static int fsi_hw_free(struct snd_pcm_substream *substream)
1046{
1047 return snd_pcm_lib_free_pages(substream);
1048}
1049
1050static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1051{
1052 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001053 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001054 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001055 long location;
1056
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001057 location = (io->buff_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001058 if (location < 0)
1059 location = 0;
1060
1061 return bytes_to_frames(runtime, location);
1062}
1063
1064static struct snd_pcm_ops fsi_pcm_ops = {
1065 .open = fsi_pcm_open,
1066 .ioctl = snd_pcm_lib_ioctl,
1067 .hw_params = fsi_hw_params,
1068 .hw_free = fsi_hw_free,
1069 .pointer = fsi_pointer,
1070};
1071
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001072/*
1073 * snd_soc_platform
1074 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001075
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001076#define PREALLOC_BUFFER (32 * 1024)
1077#define PREALLOC_BUFFER_MAX (32 * 1024)
1078
1079static void fsi_pcm_free(struct snd_pcm *pcm)
1080{
1081 snd_pcm_lib_preallocate_free_for_all(pcm);
1082}
1083
1084static int fsi_pcm_new(struct snd_card *card,
1085 struct snd_soc_dai *dai,
1086 struct snd_pcm *pcm)
1087{
1088 /*
1089 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1090 * in MMAP mode (i.e. aplay -M)
1091 */
1092 return snd_pcm_lib_preallocate_pages_for_all(
1093 pcm,
1094 SNDRV_DMA_TYPE_CONTINUOUS,
1095 snd_dma_continuous_data(GFP_KERNEL),
1096 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1097}
1098
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001099/*
1100 * alsa struct
1101 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001102
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001103static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001104 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001105 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001106 .playback = {
1107 .rates = FSI_RATES,
1108 .formats = FSI_FMTS,
1109 .channels_min = 1,
1110 .channels_max = 8,
1111 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001112 .capture = {
1113 .rates = FSI_RATES,
1114 .formats = FSI_FMTS,
1115 .channels_min = 1,
1116 .channels_max = 8,
1117 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001118 .ops = &fsi_dai_ops,
1119 },
1120 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001121 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001122 .playback = {
1123 .rates = FSI_RATES,
1124 .formats = FSI_FMTS,
1125 .channels_min = 1,
1126 .channels_max = 8,
1127 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001128 .capture = {
1129 .rates = FSI_RATES,
1130 .formats = FSI_FMTS,
1131 .channels_min = 1,
1132 .channels_max = 8,
1133 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001134 .ops = &fsi_dai_ops,
1135 },
1136};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001137
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001138static struct snd_soc_platform_driver fsi_soc_platform = {
1139 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001140 .pcm_new = fsi_pcm_new,
1141 .pcm_free = fsi_pcm_free,
1142};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001143
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001144/*
1145 * platform function
1146 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001147
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001148static int fsi_probe(struct platform_device *pdev)
1149{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001150 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001151 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001152 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001153 unsigned int irq;
1154 int ret;
1155
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001156 id_entry = pdev->id_entry;
1157 if (!id_entry) {
1158 dev_err(&pdev->dev, "unknown fsi device\n");
1159 return -ENODEV;
1160 }
1161
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001162 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1163 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001164 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001165 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1166 ret = -ENODEV;
1167 goto exit;
1168 }
1169
1170 master = kzalloc(sizeof(*master), GFP_KERNEL);
1171 if (!master) {
1172 dev_err(&pdev->dev, "Could not allocate master\n");
1173 ret = -ENOMEM;
1174 goto exit;
1175 }
1176
1177 master->base = ioremap_nocache(res->start, resource_size(res));
1178 if (!master->base) {
1179 ret = -ENXIO;
1180 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1181 goto exit_kfree;
1182 }
1183
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001184 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001185 master->irq = irq;
1186 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001187 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001188 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001189
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001190 /* FSI A setting */
1191 master->fsia.base = master->base;
1192 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001193
1194 /* FSI B setting */
1195 master->fsib.base = master->base + 0x40;
1196 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001197
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001198 pm_runtime_enable(&pdev->dev);
1199 pm_runtime_resume(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001200 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001201
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001202 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001203
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001204 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1205 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001206 if (ret) {
1207 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001208 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001209 }
1210
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001211 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001212 if (ret < 0) {
1213 dev_err(&pdev->dev, "cannot snd soc register\n");
1214 goto exit_free_irq;
1215 }
1216
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001217 return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001218
1219exit_free_irq:
1220 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001221exit_iounmap:
1222 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001223 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001224exit_kfree:
1225 kfree(master);
1226 master = NULL;
1227exit:
1228 return ret;
1229}
1230
1231static int fsi_remove(struct platform_device *pdev)
1232{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001233 struct fsi_master *master;
1234
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001235 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1238 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001239
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001240 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001241
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001242 free_irq(master->irq, master);
1243
1244 iounmap(master->base);
1245 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001246
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001247 return 0;
1248}
1249
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001250static int fsi_runtime_nop(struct device *dev)
1251{
1252 /* Runtime PM callback shared between ->runtime_suspend()
1253 * and ->runtime_resume(). Simply returns success.
1254 *
1255 * This driver re-initializes all registers after
1256 * pm_runtime_get_sync() anyway so there is no need
1257 * to save and restore registers here.
1258 */
1259 return 0;
1260}
1261
1262static struct dev_pm_ops fsi_pm_ops = {
1263 .runtime_suspend = fsi_runtime_nop,
1264 .runtime_resume = fsi_runtime_nop,
1265};
1266
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001267static struct fsi_core fsi1_core = {
1268 .ver = 1,
1269
1270 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001271 .int_st = INT_ST,
1272 .iemsk = IEMSK,
1273 .imsk = IMSK,
1274};
1275
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001276static struct fsi_core fsi2_core = {
1277 .ver = 2,
1278
1279 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001280 .int_st = CPU_INT_ST,
1281 .iemsk = CPU_IEMSK,
1282 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001283 .a_mclk = A_MST_CTLR,
1284 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001285};
1286
1287static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001288 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1289 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001290 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001291};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001292MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001293
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001294static struct platform_driver fsi_driver = {
1295 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001296 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001297 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001298 },
1299 .probe = fsi_probe,
1300 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001301 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001302};
1303
1304static int __init fsi_mobile_init(void)
1305{
1306 return platform_driver_register(&fsi_driver);
1307}
1308
1309static void __exit fsi_mobile_exit(void)
1310{
1311 platform_driver_unregister(&fsi_driver);
1312}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001313
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001314module_init(fsi_mobile_init);
1315module_exit(fsi_mobile_exit);
1316
1317MODULE_LICENSE("GPL");
1318MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1319MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");