blob: 4a9da6b5f4e1147921185b5a4d65856805d629b8 [file] [log] [blame]
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001/*
2 * Fifo-attached Serial Interface (FSI) support for SH7724
3 *
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on ssi.c
8 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090015#include <linux/delay.h>
Kuninori Morimoto785d1c42009-11-30 20:24:48 +090016#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090017#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090019#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090020#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090021
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090022/* PortA/PortB register */
23#define REG_DO_FMT 0x0000
24#define REG_DOFF_CTL 0x0004
25#define REG_DOFF_ST 0x0008
26#define REG_DI_FMT 0x000C
27#define REG_DIFF_CTL 0x0010
28#define REG_DIFF_ST 0x0014
29#define REG_CKG1 0x0018
30#define REG_CKG2 0x001C
31#define REG_DIDT 0x0020
32#define REG_DODT 0x0024
33#define REG_MUTE_ST 0x0028
34#define REG_OUT_SEL 0x0030
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090035
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090036/* master register */
37#define MST_CLK_RST 0x0210
38#define MST_SOFT_RST 0x0214
39#define MST_FIFO_SZ 0x0218
40
41/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090042#define A_MST_CTLR 0x0180
43#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090044#define CPU_INT_ST 0x01F4
45#define CPU_IEMSK 0x01F8
46#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090047#define INT_ST 0x0200
48#define IEMSK 0x0204
49#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090050
51/* DO_FMT */
52/* DI_FMT */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090053#define CR_BWS_24 (0x0 << 20) /* FSI2 */
54#define CR_BWS_16 (0x1 << 20) /* FSI2 */
55#define CR_BWS_20 (0x2 << 20) /* FSI2 */
56
57#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
58#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
59#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
60
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090061#define CR_MONO (0x0 << 4)
62#define CR_MONO_D (0x1 << 4)
63#define CR_PCM (0x2 << 4)
64#define CR_I2S (0x3 << 4)
65#define CR_TDM (0x4 << 4)
66#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090067
68/* DOFF_CTL */
69/* DIFF_CTL */
70#define IRQ_HALF 0x00100000
71#define FIFO_CLR 0x00000001
72
73/* DOFF_ST */
74#define ERR_OVER 0x00000010
75#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090076#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090077
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090078/* CKG1 */
79#define ACKMD_MASK 0x00007000
80#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090081#define DIMD (1 << 4)
82#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090083
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090084/* A/B MST_CTLR */
85#define BP (1 << 4) /* Fix the signal of Biphase output */
86#define SE (1 << 0) /* Fix the master clock */
87
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090088/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +090089#define CRB (1 << 4)
90#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090091
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090092/* IO SHIFT / MACRO */
93#define BI_SHIFT 12
94#define BO_SHIFT 8
95#define AI_SHIFT 4
96#define AO_SHIFT 0
97#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090098
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +090099/* SOFT_RST */
100#define PBSR (1 << 12) /* Port B Software Reset */
101#define PASR (1 << 8) /* Port A Software Reset */
102#define IR (1 << 4) /* Interrupt Reset */
103#define FSISR (1 << 0) /* Software Reset */
104
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900105/* OUT_SEL (FSI2) */
106#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
107 /* 1: Biphase and serial */
108
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900109/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900110#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900111
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900112#define FSI_RATES SNDRV_PCM_RATE_8000_96000
113
114#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
115
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900116typedef int (*set_rate_func)(struct device *dev, int is_porta, int rate, int enable);
117
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900118/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900119 * FSI driver use below type name for variable
120 *
121 * xxx_len : data length
122 * xxx_width : data width
123 * xxx_offset : data offset
124 * xxx_num : number of data
125 */
126
127/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900128 * struct
129 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900130
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900131struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900132 struct snd_pcm_substream *substream;
133
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900134 int fifo_max_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900135
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900136 int buff_offset;
137 int buff_len;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900138 int period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900139 int period_num;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900140
141 int uerr_num;
142 int oerr_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900143};
144
145struct fsi_priv {
146 void __iomem *base;
147 struct fsi_master *master;
148
149 struct fsi_stream playback;
150 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900151
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900152 int chan_num:16;
153 int clk_master:1;
154
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000155 long rate;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +0900156
157 /* for suspend/resume */
158 u32 saved_do_fmt;
159 u32 saved_di_fmt;
160 u32 saved_ckg1;
161 u32 saved_ckg2;
162 u32 saved_out_sel;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900163};
164
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900165struct fsi_core {
166 int ver;
167
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900168 u32 int_st;
169 u32 iemsk;
170 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900171 u32 a_mclk;
172 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900173};
174
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900175struct fsi_master {
176 void __iomem *base;
177 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900178 struct fsi_priv fsia;
179 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900180 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900181 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900182 spinlock_t lock;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +0900183
184 /* for suspend/resume */
185 u32 saved_a_mclk;
186 u32 saved_b_mclk;
187 u32 saved_iemsk;
188 u32 saved_imsk;
189 u32 saved_clk_rst;
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900190 u32 saved_soft_rst;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900191};
192
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900193/*
194 * basic read write function
195 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900196
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100197static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900198{
199 /* valid data area is 24bit */
200 data &= 0x00ffffff;
201
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100202 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900203}
204
205static u32 __fsi_reg_read(u32 reg)
206{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100207 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900208}
209
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100210static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900211{
212 u32 val = __fsi_reg_read(reg);
213
214 val &= ~mask;
215 val |= data & mask;
216
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100217 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900218}
219
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900220#define fsi_reg_write(p, r, d)\
221 __fsi_reg_write((u32)(p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900222
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900223#define fsi_reg_read(p, r)\
224 __fsi_reg_read((u32)(p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900225
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900226#define fsi_reg_mask_set(p, r, m, d)\
227 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900228
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900229#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
230#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
231static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900232{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900233 u32 ret;
234 unsigned long flags;
235
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900236 spin_lock_irqsave(&master->lock, flags);
237 ret = __fsi_reg_read((u32)(master->base + reg));
238 spin_unlock_irqrestore(&master->lock, flags);
239
240 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900241}
242
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900243#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
244#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
245static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900246 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900248 unsigned long flags;
249
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900250 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100251 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900252 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900253}
254
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900255/*
256 * basic function
257 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900258
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900259static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900260{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900261 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900262}
263
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900264static int fsi_is_clk_master(struct fsi_priv *fsi)
265{
266 return fsi->clk_master;
267}
268
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900269static int fsi_is_port_a(struct fsi_priv *fsi)
270{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900271 return fsi->master->base == fsi->base;
272}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900273
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900274static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900275{
276 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900277
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000278 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900279}
280
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900281static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900282{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000283 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900284
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000285 if (dai->id == 0)
286 return &master->fsia;
287 else
288 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900289}
290
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900291static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
292{
293 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
294}
295
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900296static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
297{
298 if (!master->info)
299 return NULL;
300
301 return master->info->set_rate;
302}
303
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900304static u32 fsi_get_info_flags(struct fsi_priv *fsi)
305{
306 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900307 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900308
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900309 if (!master->info)
310 return 0;
311
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900312 return is_porta ? master->info->porta_flags :
313 master->info->portb_flags;
314}
315
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900316static inline int fsi_stream_is_play(int stream)
317{
318 return stream == SNDRV_PCM_STREAM_PLAYBACK;
319}
320
Kuninori Morimoto00545782010-10-12 18:30:14 +0900321static inline int fsi_is_play(struct snd_pcm_substream *substream)
322{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900323 return fsi_stream_is_play(substream->stream);
324}
325
326static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
327 int is_play)
328{
329 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900330}
331
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900332static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900333{
334 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900335 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900336
337 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900338 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900339 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900340 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900341
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900342 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900343}
344
345static void fsi_stream_push(struct fsi_priv *fsi,
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900346 int is_play,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900347 struct snd_pcm_substream *substream,
348 u32 buffer_len,
349 u32 period_len)
350{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900351 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
352
353 io->substream = substream;
354 io->buff_len = buffer_len;
355 io->buff_offset = 0;
356 io->period_len = period_len;
357 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900358 io->oerr_num = -1; /* ignore 1st err */
359 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900360}
361
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900362static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900363{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900364 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900365 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
366
367
368 if (io->oerr_num > 0)
369 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
370
371 if (io->uerr_num > 0)
372 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900373
374 io->substream = NULL;
375 io->buff_len = 0;
376 io->buff_offset = 0;
377 io->period_len = 0;
378 io->period_num = 0;
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900379 io->oerr_num = 0;
380 io->uerr_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900381}
382
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900383static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900384{
385 u32 status;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900386 int data_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900387
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900388 status = is_play ?
389 fsi_reg_read(fsi, DOFF_ST) :
390 fsi_reg_read(fsi, DIFF_ST);
391
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900392 data_num = 0x1ff & (status >> 8);
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900393 data_num *= fsi->chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900394
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900395 return data_num;
396}
397
398static int fsi_len2num(int len, int width)
399{
400 return len / width;
401}
402
403#define fsi_num2offset(a, b) fsi_num2len(a, b)
404static int fsi_num2len(int num, int width)
405{
406 return num * width;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900407}
408
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900409static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play)
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900410{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900411 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
412 struct snd_pcm_substream *substream = io->substream;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900413 struct snd_pcm_runtime *runtime = substream->runtime;
414
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900415 return frames_to_bytes(runtime, 1) / fsi->chan_num;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900416}
417
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900418static void fsi_count_fifo_err(struct fsi_priv *fsi)
419{
420 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
421 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
422
423 if (ostatus & ERR_OVER)
424 fsi->playback.oerr_num++;
425
426 if (ostatus & ERR_UNDER)
427 fsi->playback.uerr_num++;
428
429 if (istatus & ERR_OVER)
430 fsi->capture.oerr_num++;
431
432 if (istatus & ERR_UNDER)
433 fsi->capture.uerr_num++;
434
435 fsi_reg_write(fsi, DOFF_ST, 0);
436 fsi_reg_write(fsi, DIFF_ST, 0);
437}
438
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900439/*
440 * dma function
441 */
442
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900443static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900444{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900445 int is_play = fsi_stream_is_play(stream);
446 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
447
448 return io->substream->runtime->dma_area + io->buff_offset;
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900449}
450
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900451static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900452{
453 u16 *start;
454 int i;
455
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900456 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
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 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
460}
461
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900462static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900463{
464 u16 *start;
465 int i;
466
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900467 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
468
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900469
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900470 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900471 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
472}
473
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900474static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900475{
476 u32 *start;
477 int i;
478
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900479 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
480
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900481
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900482 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900483 fsi_reg_write(fsi, DODT, *(start + i));
484}
485
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900486static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900487{
488 u32 *start;
489 int i;
490
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900491 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900492
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900493 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900494 *(start + i) = fsi_reg_read(fsi, DIDT);
495}
496
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900497/*
498 * irq function
499 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900500
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900501static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
502{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900503 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900504 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900505
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900506 fsi_core_mask_set(master, imsk, data, data);
507 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900508}
509
510static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
511{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900512 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900513 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900514
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900515 fsi_core_mask_set(master, imsk, data, 0);
516 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900517}
518
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900519static u32 fsi_irq_get_status(struct fsi_master *master)
520{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900521 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900522}
523
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900524static void fsi_irq_clear_status(struct fsi_priv *fsi)
525{
526 u32 data = 0;
527 struct fsi_master *master = fsi_get_master(fsi);
528
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900529 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
530 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900531
532 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900533 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900534}
535
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900536/*
537 * SPDIF master clock function
538 *
539 * These functions are used later FSI2
540 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900541static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
542{
543 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900544 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900545
546 if (master->core->ver < 2) {
547 pr_err("fsi: register access err (%s)\n", __func__);
548 return;
549 }
550
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900551 mask = BP | SE;
552 val = enable ? mask : 0;
553
554 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900555 fsi_core_mask_set(master, a_mclk, mask, val) :
556 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900557}
558
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900559/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900560 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900561 */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900562#define fsi_module_init(m, d) __fsi_module_clk_ctrl(m, d, 1)
563#define fsi_module_kill(m, d) __fsi_module_clk_ctrl(m, d, 0)
564static void __fsi_module_clk_ctrl(struct fsi_master *master,
565 struct device *dev,
566 int enable)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900567{
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900568 pm_runtime_get_sync(dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900569
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900570 if (enable) {
571 /* enable only SR */
572 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
573 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
574 } else {
575 /* clear all registers */
576 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
577 }
578
579 pm_runtime_put_sync(dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900580}
581
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900582#define fsi_port_start(f) __fsi_port_clk_ctrl(f, 1)
583#define fsi_port_stop(f) __fsi_port_clk_ctrl(f, 0)
584static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int enable)
585{
586 struct fsi_master *master = fsi_get_master(fsi);
587 u32 soft = fsi_is_port_a(fsi) ? PASR : PBSR;
588 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
589 int is_master = fsi_is_clk_master(fsi);
590
591 fsi_master_mask_set(master, SOFT_RST, soft, (enable) ? soft : 0);
592 if (is_master)
593 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
594}
595
596/*
597 * ctrl function
598 */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900599static void fsi_fifo_init(struct fsi_priv *fsi,
600 int is_play,
601 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900602{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900603 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900604 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900605 u32 shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900606
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900607 /* get on-chip RAM capacity */
608 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900609 shift >>= fsi_get_port_shift(fsi, is_play);
610 shift &= FIFO_SZ_MASK;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900611 io->fifo_max_num = 256 << shift;
612 dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900613
614 /*
615 * The maximum number of sample data varies depending
616 * on the number of channels selected for the format.
617 *
618 * FIFOs are used in 4-channel units in 3-channel mode
619 * and in 8-channel units in 5- to 7-channel mode
620 * meaning that more FIFOs than the required size of DPRAM
621 * are used.
622 *
623 * ex) if 256 words of DP-RAM is connected
624 * 1 channel: 256 (256 x 1 = 256)
625 * 2 channels: 128 (128 x 2 = 256)
626 * 3 channels: 64 ( 64 x 3 = 192)
627 * 4 channels: 64 ( 64 x 4 = 256)
628 * 5 channels: 32 ( 32 x 5 = 160)
629 * 6 channels: 32 ( 32 x 6 = 192)
630 * 7 channels: 32 ( 32 x 7 = 224)
631 * 8 channels: 32 ( 32 x 8 = 256)
632 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900633 for (i = 1; i < fsi->chan_num; i <<= 1)
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900634 io->fifo_max_num >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900635 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900636 fsi->chan_num, io->fifo_max_num);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900637
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900638 /*
639 * set interrupt generation factor
640 * clear FIFO
641 */
642 if (is_play) {
643 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
644 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
645 } else {
646 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
647 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
648 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900649}
650
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900651static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900652{
653 struct snd_pcm_runtime *runtime;
654 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900655 int is_play = fsi_stream_is_play(stream);
656 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900657 int data_residue_num;
658 int data_num;
659 int data_num_max;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900660 int ch_width;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900661 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900662 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900663
664 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900665 !io->substream ||
666 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900667 return -EINVAL;
668
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900669 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900670 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900671 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900672
673 /* FSI FIFO has limit.
674 * So, this driver can not send periods data at a time
675 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900676 if (io->buff_offset >=
677 fsi_num2offset(io->period_num + 1, io->period_len)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900678
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900679 over_period = 1;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900680 io->period_num = (io->period_num + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900681
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900682 if (0 == io->period_num)
683 io->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900684 }
685
686 /* get 1 channel data width */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900687 ch_width = fsi_get_frame_width(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900688
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900689 /* get residue data number of alsa */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900690 data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900691 ch_width);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900692
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900693 if (is_play) {
694 /*
695 * for play-back
696 *
697 * data_num_max : number of FSI fifo free space
698 * data_num : number of ALSA residue data
699 */
Kuninori Morimoto160afa72011-01-24 10:42:08 +0900700 data_num_max = io->fifo_max_num * fsi->chan_num;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900701 data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900702
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900703 data_num = data_residue_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900704
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900705 switch (ch_width) {
706 case 2:
707 fn = fsi_dma_soft_push16;
708 break;
709 case 4:
710 fn = fsi_dma_soft_push32;
711 break;
712 default:
713 return -EINVAL;
714 }
715 } else {
716 /*
717 * for capture
718 *
719 * data_num_max : number of ALSA free space
720 * data_num : number of data in FSI fifo
721 */
722 data_num_max = data_residue_num;
723 data_num = fsi_get_fifo_data_num(fsi, is_play);
724
725 switch (ch_width) {
726 case 2:
727 fn = fsi_dma_soft_pop16;
728 break;
729 case 4:
730 fn = fsi_dma_soft_pop32;
731 break;
732 default:
733 return -EINVAL;
734 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900735 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900736
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900737 data_num = min(data_num, data_num_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900738
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900739 fn(fsi, data_num);
740
741 /* update buff_offset */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900742 io->buff_offset += fsi_num2offset(data_num, ch_width);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900743
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900744 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900745 snd_pcm_period_elapsed(substream);
746
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900747 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900748}
749
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900750static int fsi_data_pop(struct fsi_priv *fsi)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900751{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900752 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900753}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900754
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900755static int fsi_data_push(struct fsi_priv *fsi)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900756{
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900757 return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900758}
759
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900760static irqreturn_t fsi_interrupt(int irq, void *data)
761{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900762 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900763 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900764
765 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900766 fsi_master_mask_set(master, SOFT_RST, IR, 0);
767 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900768
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900769 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900770 fsi_data_push(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900771 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900772 fsi_data_push(&master->fsib);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900773 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900774 fsi_data_pop(&master->fsia);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900775 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900776 fsi_data_pop(&master->fsib);
777
778 fsi_count_fifo_err(&master->fsia);
779 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900780
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900781 fsi_irq_clear_status(&master->fsia);
782 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900783
784 return IRQ_HANDLED;
785}
786
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900787/*
788 * dai ops
789 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900790
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900791static int fsi_dai_startup(struct snd_pcm_substream *substream,
792 struct snd_soc_dai *dai)
793{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900794 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900795 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900796 u32 data;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900797 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900798
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900799 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900800
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900801
802 /* clock inversion (CKG2) */
803 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900804 if (SH_FSI_LRM_INV & flags)
805 data |= 1 << 12;
806 if (SH_FSI_BRM_INV & flags)
807 data |= 1 << 8;
808 if (SH_FSI_LRS_INV & flags)
809 data |= 1 << 4;
810 if (SH_FSI_BRS_INV & flags)
811 data |= 1 << 0;
812
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900813 fsi_reg_write(fsi, CKG2, data);
814
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900815 /* irq clear */
816 fsi_irq_disable(fsi, is_play);
817 fsi_irq_clear_status(fsi);
818
819 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900820 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900821
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900822 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900823}
824
825static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
826 struct snd_soc_dai *dai)
827{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900828 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900829 int is_play = fsi_is_play(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000830 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900831 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900832
833 fsi_irq_disable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900834
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900835 if (fsi_is_clk_master(fsi))
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000836 set_rate(dai->dev, fsi_is_port_a(fsi), fsi->rate, 0);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900837
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000838 fsi->rate = 0;
839
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900840 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900841}
842
843static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
844 struct snd_soc_dai *dai)
845{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900846 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900847 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900848 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900849 int ret = 0;
850
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900851 switch (cmd) {
852 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900853 fsi_stream_push(fsi, is_play, substream,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900854 frames_to_bytes(runtime, runtime->buffer_size),
855 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900856 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimoto9e261bb2010-12-17 12:52:56 +0900857 fsi_irq_enable(fsi, is_play);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900858 fsi_port_start(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900859 break;
860 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900861 fsi_port_stop(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900862 fsi_irq_disable(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900863 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900864 break;
865 }
866
867 return ret;
868}
869
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900870static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
871{
872 u32 data = 0;
873
874 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
875 case SND_SOC_DAIFMT_I2S:
876 data = CR_I2S;
877 fsi->chan_num = 2;
878 break;
879 case SND_SOC_DAIFMT_LEFT_J:
880 data = CR_PCM;
881 fsi->chan_num = 2;
882 break;
883 default:
884 return -EINVAL;
885 }
886
887 fsi_reg_write(fsi, DO_FMT, data);
888 fsi_reg_write(fsi, DI_FMT, data);
889
890 return 0;
891}
892
893static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
894{
895 struct fsi_master *master = fsi_get_master(fsi);
896 u32 data = 0;
897
898 if (master->core->ver < 2)
899 return -EINVAL;
900
901 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
902 fsi->chan_num = 2;
903 fsi_spdif_clk_ctrl(fsi, 1);
904 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
905
906 fsi_reg_write(fsi, DO_FMT, data);
907 fsi_reg_write(fsi, DI_FMT, data);
908
909 return 0;
910}
911
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900912static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
913{
914 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900915 struct fsi_master *master = fsi_get_master(fsi);
916 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900917 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900918 u32 data = 0;
919 int ret;
920
921 pm_runtime_get_sync(dai->dev);
922
923 /* set master/slave audio interface */
924 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
925 case SND_SOC_DAIFMT_CBM_CFM:
926 data = DIMD | DOMD;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900927 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900928 break;
929 case SND_SOC_DAIFMT_CBS_CFS:
930 break;
931 default:
932 ret = -EINVAL;
933 goto set_fmt_exit;
934 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900935
936 if (fsi_is_clk_master(fsi) && !set_rate) {
937 dev_err(dai->dev, "platform doesn't have set_rate\n");
938 ret = -EINVAL;
939 goto set_fmt_exit;
940 }
941
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900942 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +0900943
944 /* set format */
945 switch (flags & SH_FSI_FMT_MASK) {
946 case SH_FSI_FMT_DAI:
947 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
948 break;
949 case SH_FSI_FMT_SPDIF:
950 ret = fsi_set_fmt_spdif(fsi);
951 break;
952 default:
953 ret = -EINVAL;
954 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +0900955
956set_fmt_exit:
957 pm_runtime_put_sync(dai->dev);
958
959 return ret;
960}
961
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900962static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
963 struct snd_pcm_hw_params *params,
964 struct snd_soc_dai *dai)
965{
966 struct fsi_priv *fsi = fsi_get_priv(substream);
967 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900968 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900969 int fsi_ver = master->core->ver;
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000970 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900971 int ret;
972
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900973 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900974 return 0;
975
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000976 ret = set_rate(dai->dev, fsi_is_port_a(fsi), rate, 1);
977 if (ret < 0) /* error */
978 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900979
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000980 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900981 if (ret > 0) {
982 u32 data = 0;
983
984 switch (ret & SH_FSI_ACKMD_MASK) {
985 default:
986 /* FALL THROUGH */
987 case SH_FSI_ACKMD_512:
988 data |= (0x0 << 12);
989 break;
990 case SH_FSI_ACKMD_256:
991 data |= (0x1 << 12);
992 break;
993 case SH_FSI_ACKMD_128:
994 data |= (0x2 << 12);
995 break;
996 case SH_FSI_ACKMD_64:
997 data |= (0x3 << 12);
998 break;
999 case SH_FSI_ACKMD_32:
1000 if (fsi_ver < 2)
1001 dev_err(dai->dev, "unsupported ACKMD\n");
1002 else
1003 data |= (0x4 << 12);
1004 break;
1005 }
1006
1007 switch (ret & SH_FSI_BPFMD_MASK) {
1008 default:
1009 /* FALL THROUGH */
1010 case SH_FSI_BPFMD_32:
1011 data |= (0x0 << 8);
1012 break;
1013 case SH_FSI_BPFMD_64:
1014 data |= (0x1 << 8);
1015 break;
1016 case SH_FSI_BPFMD_128:
1017 data |= (0x2 << 8);
1018 break;
1019 case SH_FSI_BPFMD_256:
1020 data |= (0x3 << 8);
1021 break;
1022 case SH_FSI_BPFMD_512:
1023 data |= (0x4 << 8);
1024 break;
1025 case SH_FSI_BPFMD_16:
1026 if (fsi_ver < 2)
1027 dev_err(dai->dev, "unsupported ACKMD\n");
1028 else
1029 data |= (0x7 << 8);
1030 break;
1031 }
1032
1033 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
1034 udelay(10);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001035 ret = 0;
1036 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001037
1038 return ret;
1039
1040}
1041
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001042static struct snd_soc_dai_ops fsi_dai_ops = {
1043 .startup = fsi_dai_startup,
1044 .shutdown = fsi_dai_shutdown,
1045 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001046 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001047 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001048};
1049
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001050/*
1051 * pcm ops
1052 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001053
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001054static struct snd_pcm_hardware fsi_pcm_hardware = {
1055 .info = SNDRV_PCM_INFO_INTERLEAVED |
1056 SNDRV_PCM_INFO_MMAP |
1057 SNDRV_PCM_INFO_MMAP_VALID |
1058 SNDRV_PCM_INFO_PAUSE,
1059 .formats = FSI_FMTS,
1060 .rates = FSI_RATES,
1061 .rate_min = 8000,
1062 .rate_max = 192000,
1063 .channels_min = 1,
1064 .channels_max = 2,
1065 .buffer_bytes_max = 64 * 1024,
1066 .period_bytes_min = 32,
1067 .period_bytes_max = 8192,
1068 .periods_min = 1,
1069 .periods_max = 32,
1070 .fifo_size = 256,
1071};
1072
1073static int fsi_pcm_open(struct snd_pcm_substream *substream)
1074{
1075 struct snd_pcm_runtime *runtime = substream->runtime;
1076 int ret = 0;
1077
1078 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1079
1080 ret = snd_pcm_hw_constraint_integer(runtime,
1081 SNDRV_PCM_HW_PARAM_PERIODS);
1082
1083 return ret;
1084}
1085
1086static int fsi_hw_params(struct snd_pcm_substream *substream,
1087 struct snd_pcm_hw_params *hw_params)
1088{
1089 return snd_pcm_lib_malloc_pages(substream,
1090 params_buffer_bytes(hw_params));
1091}
1092
1093static int fsi_hw_free(struct snd_pcm_substream *substream)
1094{
1095 return snd_pcm_lib_free_pages(substream);
1096}
1097
1098static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1099{
1100 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001101 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001102 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001103 long location;
1104
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001105 location = (io->buff_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001106 if (location < 0)
1107 location = 0;
1108
1109 return bytes_to_frames(runtime, location);
1110}
1111
1112static struct snd_pcm_ops fsi_pcm_ops = {
1113 .open = fsi_pcm_open,
1114 .ioctl = snd_pcm_lib_ioctl,
1115 .hw_params = fsi_hw_params,
1116 .hw_free = fsi_hw_free,
1117 .pointer = fsi_pointer,
1118};
1119
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001120/*
1121 * snd_soc_platform
1122 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001123
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001124#define PREALLOC_BUFFER (32 * 1024)
1125#define PREALLOC_BUFFER_MAX (32 * 1024)
1126
1127static void fsi_pcm_free(struct snd_pcm *pcm)
1128{
1129 snd_pcm_lib_preallocate_free_for_all(pcm);
1130}
1131
1132static int fsi_pcm_new(struct snd_card *card,
1133 struct snd_soc_dai *dai,
1134 struct snd_pcm *pcm)
1135{
1136 /*
1137 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1138 * in MMAP mode (i.e. aplay -M)
1139 */
1140 return snd_pcm_lib_preallocate_pages_for_all(
1141 pcm,
1142 SNDRV_DMA_TYPE_CONTINUOUS,
1143 snd_dma_continuous_data(GFP_KERNEL),
1144 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1145}
1146
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001147/*
1148 * alsa struct
1149 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001150
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001151static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001152 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001154 .playback = {
1155 .rates = FSI_RATES,
1156 .formats = FSI_FMTS,
1157 .channels_min = 1,
1158 .channels_max = 8,
1159 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001160 .capture = {
1161 .rates = FSI_RATES,
1162 .formats = FSI_FMTS,
1163 .channels_min = 1,
1164 .channels_max = 8,
1165 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001166 .ops = &fsi_dai_ops,
1167 },
1168 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001169 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001170 .playback = {
1171 .rates = FSI_RATES,
1172 .formats = FSI_FMTS,
1173 .channels_min = 1,
1174 .channels_max = 8,
1175 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001176 .capture = {
1177 .rates = FSI_RATES,
1178 .formats = FSI_FMTS,
1179 .channels_min = 1,
1180 .channels_max = 8,
1181 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001182 .ops = &fsi_dai_ops,
1183 },
1184};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001185
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001186static struct snd_soc_platform_driver fsi_soc_platform = {
1187 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001188 .pcm_new = fsi_pcm_new,
1189 .pcm_free = fsi_pcm_free,
1190};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001191
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001192/*
1193 * platform function
1194 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001195
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001196static int fsi_probe(struct platform_device *pdev)
1197{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001198 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001199 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001200 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001201 unsigned int irq;
1202 int ret;
1203
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001204 id_entry = pdev->id_entry;
1205 if (!id_entry) {
1206 dev_err(&pdev->dev, "unknown fsi device\n");
1207 return -ENODEV;
1208 }
1209
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001210 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1211 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001212 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001213 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1214 ret = -ENODEV;
1215 goto exit;
1216 }
1217
1218 master = kzalloc(sizeof(*master), GFP_KERNEL);
1219 if (!master) {
1220 dev_err(&pdev->dev, "Could not allocate master\n");
1221 ret = -ENOMEM;
1222 goto exit;
1223 }
1224
1225 master->base = ioremap_nocache(res->start, resource_size(res));
1226 if (!master->base) {
1227 ret = -ENXIO;
1228 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1229 goto exit_kfree;
1230 }
1231
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001232 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001233 master->irq = irq;
1234 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001235 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001236 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001237
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001238 /* FSI A setting */
1239 master->fsia.base = master->base;
1240 master->fsia.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001241
1242 /* FSI B setting */
1243 master->fsib.base = master->base + 0x40;
1244 master->fsib.master = master;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001245
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001246 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001247 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001248
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001249 fsi_module_init(master, &pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001250
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001251 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1252 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001253 if (ret) {
1254 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001255 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001256 }
1257
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001258 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001259 if (ret < 0) {
1260 dev_err(&pdev->dev, "cannot snd soc register\n");
1261 goto exit_free_irq;
1262 }
1263
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001264 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1265 ARRAY_SIZE(fsi_soc_dai));
1266 if (ret < 0) {
1267 dev_err(&pdev->dev, "cannot snd dai register\n");
1268 goto exit_snd_soc;
1269 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001270
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001271 return ret;
1272
1273exit_snd_soc:
1274 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001275exit_free_irq:
1276 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001277exit_iounmap:
1278 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001279 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001280exit_kfree:
1281 kfree(master);
1282 master = NULL;
1283exit:
1284 return ret;
1285}
1286
1287static int fsi_remove(struct platform_device *pdev)
1288{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001289 struct fsi_master *master;
1290
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001291 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001292
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001293 fsi_module_kill(master, &pdev->dev);
1294
Kuninori Morimotod985f272011-04-08 15:09:25 +09001295 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001296 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001297
Kuninori Morimotod985f272011-04-08 15:09:25 +09001298 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1299 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001300
1301 iounmap(master->base);
1302 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001303
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001304 return 0;
1305}
1306
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001307static void __fsi_suspend(struct fsi_priv *fsi,
1308 struct device *dev,
1309 set_rate_func set_rate)
1310{
1311 fsi->saved_do_fmt = fsi_reg_read(fsi, DO_FMT);
1312 fsi->saved_di_fmt = fsi_reg_read(fsi, DI_FMT);
1313 fsi->saved_ckg1 = fsi_reg_read(fsi, CKG1);
1314 fsi->saved_ckg2 = fsi_reg_read(fsi, CKG2);
1315 fsi->saved_out_sel = fsi_reg_read(fsi, OUT_SEL);
1316
1317 if (fsi_is_clk_master(fsi))
1318 set_rate(dev, fsi_is_port_a(fsi), fsi->rate, 0);
1319}
1320
1321static void __fsi_resume(struct fsi_priv *fsi,
1322 struct device *dev,
1323 set_rate_func set_rate)
1324{
1325 fsi_reg_write(fsi, DO_FMT, fsi->saved_do_fmt);
1326 fsi_reg_write(fsi, DI_FMT, fsi->saved_di_fmt);
1327 fsi_reg_write(fsi, CKG1, fsi->saved_ckg1);
1328 fsi_reg_write(fsi, CKG2, fsi->saved_ckg2);
1329 fsi_reg_write(fsi, OUT_SEL, fsi->saved_out_sel);
1330
1331 if (fsi_is_clk_master(fsi))
1332 set_rate(dev, fsi_is_port_a(fsi), fsi->rate, 1);
1333}
1334
1335static int fsi_suspend(struct device *dev)
1336{
1337 struct fsi_master *master = dev_get_drvdata(dev);
1338 set_rate_func set_rate = fsi_get_info_set_rate(master);
1339
1340 pm_runtime_get_sync(dev);
1341
1342 __fsi_suspend(&master->fsia, dev, set_rate);
1343 __fsi_suspend(&master->fsib, dev, set_rate);
1344
1345 master->saved_a_mclk = fsi_core_read(master, a_mclk);
1346 master->saved_b_mclk = fsi_core_read(master, b_mclk);
1347 master->saved_iemsk = fsi_core_read(master, iemsk);
1348 master->saved_imsk = fsi_core_read(master, imsk);
1349 master->saved_clk_rst = fsi_master_read(master, CLK_RST);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001350 master->saved_soft_rst = fsi_master_read(master, SOFT_RST);
1351
1352 fsi_module_kill(master, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001353
1354 pm_runtime_put_sync(dev);
1355
1356 return 0;
1357}
1358
1359static int fsi_resume(struct device *dev)
1360{
1361 struct fsi_master *master = dev_get_drvdata(dev);
1362 set_rate_func set_rate = fsi_get_info_set_rate(master);
1363
1364 pm_runtime_get_sync(dev);
1365
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001366 fsi_module_init(master, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001367
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001368 fsi_master_mask_set(master, SOFT_RST, 0xffff, master->saved_soft_rst);
1369 fsi_master_mask_set(master, CLK_RST, 0xffff, master->saved_clk_rst);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001370 fsi_core_mask_set(master, a_mclk, 0xffff, master->saved_a_mclk);
1371 fsi_core_mask_set(master, b_mclk, 0xffff, master->saved_b_mclk);
1372 fsi_core_mask_set(master, iemsk, 0xffff, master->saved_iemsk);
1373 fsi_core_mask_set(master, imsk, 0xffff, master->saved_imsk);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001374
1375 __fsi_resume(&master->fsia, dev, set_rate);
1376 __fsi_resume(&master->fsib, dev, set_rate);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001377
1378 pm_runtime_put_sync(dev);
1379
1380 return 0;
1381}
1382
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001383static int fsi_runtime_nop(struct device *dev)
1384{
1385 /* Runtime PM callback shared between ->runtime_suspend()
1386 * and ->runtime_resume(). Simply returns success.
1387 *
1388 * This driver re-initializes all registers after
1389 * pm_runtime_get_sync() anyway so there is no need
1390 * to save and restore registers here.
1391 */
1392 return 0;
1393}
1394
1395static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001396 .suspend = fsi_suspend,
1397 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001398 .runtime_suspend = fsi_runtime_nop,
1399 .runtime_resume = fsi_runtime_nop,
1400};
1401
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001402static struct fsi_core fsi1_core = {
1403 .ver = 1,
1404
1405 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001406 .int_st = INT_ST,
1407 .iemsk = IEMSK,
1408 .imsk = IMSK,
1409};
1410
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001411static struct fsi_core fsi2_core = {
1412 .ver = 2,
1413
1414 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001415 .int_st = CPU_INT_ST,
1416 .iemsk = CPU_IEMSK,
1417 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001418 .a_mclk = A_MST_CTLR,
1419 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001420};
1421
1422static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001423 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1424 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001425 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001426};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001427MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001428
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001429static struct platform_driver fsi_driver = {
1430 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001431 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001432 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001433 },
1434 .probe = fsi_probe,
1435 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001436 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001437};
1438
1439static int __init fsi_mobile_init(void)
1440{
1441 return platform_driver_register(&fsi_driver);
1442}
1443
1444static void __exit fsi_mobile_exit(void)
1445{
1446 platform_driver_unregister(&fsi_driver);
1447}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001448
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001449module_init(fsi_mobile_init);
1450module_exit(fsi_mobile_exit);
1451
1452MODULE_LICENSE("GPL");
1453MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1454MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001455MODULE_ALIAS("platform:fsi-pcm-audio");