blob: 76ef3f34dd022171ff7a195e1cbbf594b858085b [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
22#define DO_FMT 0x0000
23#define DOFF_CTL 0x0004
24#define DOFF_ST 0x0008
25#define DI_FMT 0x000C
26#define DIFF_CTL 0x0010
27#define DIFF_ST 0x0014
28#define CKG1 0x0018
29#define CKG2 0x001C
30#define DIDT 0x0020
31#define DODT 0x0024
32#define MUTE_ST 0x0028
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090033#define OUT_SEL 0x0030
34#define REG_END OUT_SEL
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090035
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090036#define A_MST_CTLR 0x0180
37#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090038#define CPU_INT_ST 0x01F4
39#define CPU_IEMSK 0x01F8
40#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090041#define INT_ST 0x0200
42#define IEMSK 0x0204
43#define IMSK 0x0208
44#define MUTE 0x020C
45#define CLK_RST 0x0210
46#define SOFT_RST 0x0214
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090047#define FIFO_SZ 0x0218
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090048#define MREG_START A_MST_CTLR
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090049#define MREG_END FIFO_SZ
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
81
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090082/* A/B MST_CTLR */
83#define BP (1 << 4) /* Fix the signal of Biphase output */
84#define SE (1 << 0) /* Fix the master clock */
85
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090086/* CLK_RST */
87#define B_CLK 0x00000010
88#define A_CLK 0x00000001
89
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090090/* IO SHIFT / MACRO */
91#define BI_SHIFT 12
92#define BO_SHIFT 8
93#define AI_SHIFT 4
94#define AO_SHIFT 0
95#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090096
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +090097/* SOFT_RST */
98#define PBSR (1 << 12) /* Port B Software Reset */
99#define PASR (1 << 8) /* Port A Software Reset */
100#define IR (1 << 4) /* Interrupt Reset */
101#define FSISR (1 << 0) /* Software Reset */
102
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900103/* OUT_SEL (FSI2) */
104#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
105 /* 1: Biphase and serial */
106
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900107/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900108#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900109
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900110#define FSI_RATES SNDRV_PCM_RATE_8000_96000
111
112#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
113
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900114/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900115 * FSI driver use below type name for variable
116 *
117 * xxx_len : data length
118 * xxx_width : data width
119 * xxx_offset : data offset
120 * xxx_num : number of data
121 */
122
123/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900124 * struct
125 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900126
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900127struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900128 struct snd_pcm_substream *substream;
129
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900130 int fifo_max_num;
131 int chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900132
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900133 int buff_offset;
134 int buff_len;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900135 int period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900136 int period_num;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900137};
138
139struct fsi_priv {
140 void __iomem *base;
141 struct fsi_master *master;
142
143 struct fsi_stream playback;
144 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900145
146 u32 mst_ctrl;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900147};
148
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900149struct fsi_core {
150 int ver;
151
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900152 u32 int_st;
153 u32 iemsk;
154 u32 imsk;
155};
156
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900157struct fsi_master {
158 void __iomem *base;
159 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900160 struct fsi_priv fsia;
161 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900162 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900163 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900164 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900165};
166
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900167/*
168 * basic read write function
169 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900170
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100171static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900172{
173 /* valid data area is 24bit */
174 data &= 0x00ffffff;
175
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100176 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900177}
178
179static u32 __fsi_reg_read(u32 reg)
180{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100181 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900182}
183
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100184static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900185{
186 u32 val = __fsi_reg_read(reg);
187
188 val &= ~mask;
189 val |= data & mask;
190
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100191 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900192}
193
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100194static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900195{
Kuninori Morimotod7854142010-07-13 12:13:09 +0900196 if (reg > REG_END) {
197 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100198 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900199 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900200
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100201 __fsi_reg_write((u32)(fsi->base + reg), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900202}
203
204static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
205{
Kuninori Morimotod7854142010-07-13 12:13:09 +0900206 if (reg > REG_END) {
207 pr_err("fsi: register access err (%s)\n", __func__);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900208 return 0;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900209 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900210
211 return __fsi_reg_read((u32)(fsi->base + reg));
212}
213
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100214static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900215{
Kuninori Morimotod7854142010-07-13 12:13:09 +0900216 if (reg > REG_END) {
217 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100218 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900219 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900220
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100221 __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900222}
223
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100224static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900225{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900226 unsigned long flags;
227
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900228 if ((reg < MREG_START) ||
Kuninori Morimotod7854142010-07-13 12:13:09 +0900229 (reg > MREG_END)) {
230 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100231 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900232 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900233
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900234 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100235 __fsi_reg_write((u32)(master->base + reg), data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900236 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900237}
238
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900239static u32 fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900240{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900241 u32 ret;
242 unsigned long flags;
243
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900244 if ((reg < MREG_START) ||
Kuninori Morimotod7854142010-07-13 12:13:09 +0900245 (reg > MREG_END)) {
246 pr_err("fsi: register access err (%s)\n", __func__);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247 return 0;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900248 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900249
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900250 spin_lock_irqsave(&master->lock, flags);
251 ret = __fsi_reg_read((u32)(master->base + reg));
252 spin_unlock_irqrestore(&master->lock, flags);
253
254 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900255}
256
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100257static void fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900258 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900259{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900260 unsigned long flags;
261
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900262 if ((reg < MREG_START) ||
Kuninori Morimotod7854142010-07-13 12:13:09 +0900263 (reg > MREG_END)) {
264 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100265 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900266 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900267
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900268 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100269 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900270 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900271}
272
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900273/*
274 * basic function
275 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900276
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900277static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900278{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900279 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900280}
281
282static int fsi_is_port_a(struct fsi_priv *fsi)
283{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900284 return fsi->master->base == fsi->base;
285}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900286
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900287static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900288{
289 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900290
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000291 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900292}
293
294static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
295{
296 struct snd_soc_dai *dai = fsi_get_dai(substream);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000297 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900298
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299 if (dai->id == 0)
300 return &master->fsia;
301 else
302 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900303}
304
305static u32 fsi_get_info_flags(struct fsi_priv *fsi)
306{
307 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900308 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900309
310 return is_porta ? master->info->porta_flags :
311 master->info->portb_flags;
312}
313
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900314static inline int fsi_stream_is_play(int stream)
315{
316 return stream == SNDRV_PCM_STREAM_PLAYBACK;
317}
318
Kuninori Morimoto00545782010-10-12 18:30:14 +0900319static inline int fsi_is_play(struct snd_pcm_substream *substream)
320{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900321 return fsi_stream_is_play(substream->stream);
322}
323
324static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
325 int is_play)
326{
327 return is_play ? &fsi->playback : &fsi->capture;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900328}
329
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900330static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
331{
332 u32 mode;
333 u32 flags = fsi_get_info_flags(fsi);
334
335 mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
336
337 /* return
338 * 1 : master mode
339 * 0 : slave mode
340 */
341
342 return (mode & flags) != mode;
343}
344
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900345static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900346{
347 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900348 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900349
350 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900351 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900352 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900353 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900354
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900355 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900356}
357
358static void fsi_stream_push(struct fsi_priv *fsi,
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900359 int is_play,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900360 struct snd_pcm_substream *substream,
361 u32 buffer_len,
362 u32 period_len)
363{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900364 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
365
366 io->substream = substream;
367 io->buff_len = buffer_len;
368 io->buff_offset = 0;
369 io->period_len = period_len;
370 io->period_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900371}
372
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900373static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900374{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900375 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
376
377 io->substream = NULL;
378 io->buff_len = 0;
379 io->buff_offset = 0;
380 io->period_len = 0;
381 io->period_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900382}
383
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900384static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900385{
386 u32 status;
387 u32 reg = is_play ? DOFF_ST : DIFF_ST;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900388 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900389 int data_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900390
391 status = fsi_reg_read(fsi, reg);
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900392 data_num = 0x1ff & (status >> 8);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900393 data_num *= io->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 Morimoto93193c22010-10-12 19:19:28 +0900415 return frames_to_bytes(runtime, 1) / io->chan_num;
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900416}
417
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900418/*
419 * dma function
420 */
421
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900422static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900423{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900424 int is_play = fsi_stream_is_play(stream);
425 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
426
427 return io->substream->runtime->dma_area + io->buff_offset;
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900428}
429
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900430static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900431{
432 u16 *start;
433 int i;
434
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900435 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900436
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900437 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900438 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
439}
440
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900441static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900442{
443 u16 *start;
444 int i;
445
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900446 start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
447
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900448
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900449 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900450 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
451}
452
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900453static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900454{
455 u32 *start;
456 int i;
457
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900458 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
459
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900460
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900461 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900462 fsi_reg_write(fsi, DODT, *(start + i));
463}
464
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900465static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900466{
467 u32 *start;
468 int i;
469
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900470 start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900471
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900472 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900473 *(start + i) = fsi_reg_read(fsi, DIDT);
474}
475
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900476/*
477 * irq function
478 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900479
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900480static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
481{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900482 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900483 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900484
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900485 fsi_master_mask_set(master, master->core->imsk, data, data);
486 fsi_master_mask_set(master, master->core->iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900487}
488
489static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
490{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900491 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900492 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900493
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900494 fsi_master_mask_set(master, master->core->imsk, data, 0);
495 fsi_master_mask_set(master, master->core->iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900496}
497
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900498static u32 fsi_irq_get_status(struct fsi_master *master)
499{
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900500 return fsi_master_read(master, master->core->int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900501}
502
503static void fsi_irq_clear_all_status(struct fsi_master *master)
504{
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900505 fsi_master_write(master, master->core->int_st, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900506}
507
508static void fsi_irq_clear_status(struct fsi_priv *fsi)
509{
510 u32 data = 0;
511 struct fsi_master *master = fsi_get_master(fsi);
512
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900513 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
514 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900515
516 /* clear interrupt factor */
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900517 fsi_master_mask_set(master, master->core->int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900518}
519
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900520/*
521 * SPDIF master clock function
522 *
523 * These functions are used later FSI2
524 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900525static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
526{
527 struct fsi_master *master = fsi_get_master(fsi);
528 u32 val = BP | SE;
529
530 if (master->core->ver < 2) {
531 pr_err("fsi: register access err (%s)\n", __func__);
532 return;
533 }
534
535 if (enable)
536 fsi_master_mask_set(master, fsi->mst_ctrl, val, val);
537 else
538 fsi_master_mask_set(master, fsi->mst_ctrl, val, 0);
539}
540
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900541/*
542 * ctrl function
543 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900544
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900545static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
546{
547 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900548 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900549
550 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900551 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900552 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900553 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900554}
555
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900556static void fsi_fifo_init(struct fsi_priv *fsi,
557 int is_play,
558 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900559{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900560 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900561 struct fsi_stream *io = fsi_get_stream(fsi, is_play);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900562 u32 ctrl, shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900563
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900564 /* get on-chip RAM capacity */
565 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900566 shift >>= fsi_get_port_shift(fsi, is_play);
567 shift &= FIFO_SZ_MASK;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900568 io->fifo_max_num = 256 << shift;
569 dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900570
571 /*
572 * The maximum number of sample data varies depending
573 * on the number of channels selected for the format.
574 *
575 * FIFOs are used in 4-channel units in 3-channel mode
576 * and in 8-channel units in 5- to 7-channel mode
577 * meaning that more FIFOs than the required size of DPRAM
578 * are used.
579 *
580 * ex) if 256 words of DP-RAM is connected
581 * 1 channel: 256 (256 x 1 = 256)
582 * 2 channels: 128 (128 x 2 = 256)
583 * 3 channels: 64 ( 64 x 3 = 192)
584 * 4 channels: 64 ( 64 x 4 = 256)
585 * 5 channels: 32 ( 32 x 5 = 160)
586 * 6 channels: 32 ( 32 x 6 = 192)
587 * 7 channels: 32 ( 32 x 7 = 224)
588 * 8 channels: 32 ( 32 x 8 = 256)
589 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900590 for (i = 1; i < io->chan_num; i <<= 1)
591 io->fifo_max_num >>= 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900592 dev_dbg(dai->dev, "%d channel %d store\n",
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900593 io->chan_num, io->fifo_max_num);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900594
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900595 ctrl = is_play ? DOFF_CTL : DIFF_CTL;
596
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900597 /* set interrupt generation factor */
598 fsi_reg_write(fsi, ctrl, IRQ_HALF);
599
600 /* clear FIFO */
601 fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
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 Morimoto93193c22010-10-12 19:19:28 +0900616static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int startup, 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 u32 status_reg = is_play ? DOFF_ST : DIFF_ST;
623 int data_residue_num;
624 int data_num;
625 int data_num_max;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900626 int ch_width;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900627 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900628 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900629
630 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900631 !io->substream ||
632 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900633 return -EINVAL;
634
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900635 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900636 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900637 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900638
639 /* FSI FIFO has limit.
640 * So, this driver can not send periods data at a time
641 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900642 if (io->buff_offset >=
643 fsi_num2offset(io->period_num + 1, io->period_len)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900644
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900645 over_period = 1;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900646 io->period_num = (io->period_num + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900647
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900648 if (0 == io->period_num)
649 io->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900650 }
651
652 /* get 1 channel data width */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900653 ch_width = fsi_get_frame_width(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900654
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900655 /* get residue data number of alsa */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900656 data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900657 ch_width);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900658
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900659 if (is_play) {
660 /*
661 * for play-back
662 *
663 * data_num_max : number of FSI fifo free space
664 * data_num : number of ALSA residue data
665 */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900666 data_num_max = io->fifo_max_num * io->chan_num;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900667 data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900668
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900669 data_num = data_residue_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900670
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900671 switch (ch_width) {
672 case 2:
673 fn = fsi_dma_soft_push16;
674 break;
675 case 4:
676 fn = fsi_dma_soft_push32;
677 break;
678 default:
679 return -EINVAL;
680 }
681 } else {
682 /*
683 * for capture
684 *
685 * data_num_max : number of ALSA free space
686 * data_num : number of data in FSI fifo
687 */
688 data_num_max = data_residue_num;
689 data_num = fsi_get_fifo_data_num(fsi, is_play);
690
691 switch (ch_width) {
692 case 2:
693 fn = fsi_dma_soft_pop16;
694 break;
695 case 4:
696 fn = fsi_dma_soft_pop32;
697 break;
698 default:
699 return -EINVAL;
700 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900701 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900702
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900703 data_num = min(data_num, data_num_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900704
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900705 fn(fsi, data_num);
706
707 /* update buff_offset */
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900708 io->buff_offset += fsi_num2offset(data_num, ch_width);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900709
710 /* check fifo status */
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900711 if (!startup) {
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900712 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimoto75eda9682010-10-12 11:40:14 +0900713 u32 status = fsi_reg_read(fsi, status_reg);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900714
715 if (status & ERR_OVER)
716 dev_err(dai->dev, "over run\n");
717 if (status & ERR_UNDER)
718 dev_err(dai->dev, "under run\n");
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900719 }
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900720 fsi_reg_write(fsi, status_reg, 0);
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900721
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900722 /* re-enable irq */
723 fsi_irq_enable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900724
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900725 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900726 snd_pcm_period_elapsed(substream);
727
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900728 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900729}
730
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900731static int fsi_data_pop(struct fsi_priv *fsi, int startup)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900732{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900733 return fsi_fifo_data_ctrl(fsi, startup, SNDRV_PCM_STREAM_CAPTURE);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900734}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900735
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900736static int fsi_data_push(struct fsi_priv *fsi, int startup)
737{
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900738 return fsi_fifo_data_ctrl(fsi, startup, SNDRV_PCM_STREAM_PLAYBACK);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900739}
740
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900741static irqreturn_t fsi_interrupt(int irq, void *data)
742{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900743 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900744 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900745
746 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900747 fsi_master_mask_set(master, SOFT_RST, IR, 0);
748 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900749
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900750 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900751 fsi_data_push(&master->fsia, 0);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900752 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900753 fsi_data_push(&master->fsib, 0);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900754 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900755 fsi_data_pop(&master->fsia, 0);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900756 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900757 fsi_data_pop(&master->fsib, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900758
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900759 fsi_irq_clear_all_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900760
761 return IRQ_HANDLED;
762}
763
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900764/*
765 * dai ops
766 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900767
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900768static int fsi_dai_startup(struct snd_pcm_substream *substream,
769 struct snd_soc_dai *dai)
770{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900771 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900772 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900773 struct fsi_stream *io;
774 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900775 u32 fmt;
776 u32 reg;
777 u32 data;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900778 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900779 int is_master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900780
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900781 io = fsi_get_stream(fsi, is_play);
782
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900783 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900784
785 /* CKG1 */
786 data = is_play ? (1 << 0) : (1 << 4);
787 is_master = fsi_is_master_mode(fsi, is_play);
788 if (is_master)
789 fsi_reg_mask_set(fsi, CKG1, data, data);
790 else
791 fsi_reg_mask_set(fsi, CKG1, data, 0);
792
793 /* clock inversion (CKG2) */
794 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900795 if (SH_FSI_LRM_INV & flags)
796 data |= 1 << 12;
797 if (SH_FSI_BRM_INV & flags)
798 data |= 1 << 8;
799 if (SH_FSI_LRS_INV & flags)
800 data |= 1 << 4;
801 if (SH_FSI_BRS_INV & flags)
802 data |= 1 << 0;
803
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900804 fsi_reg_write(fsi, CKG2, data);
805
806 /* do fmt, di fmt */
807 data = 0;
808 reg = is_play ? DO_FMT : DI_FMT;
809 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
810 switch (fmt) {
811 case SH_FSI_FMT_MONO:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900812 data = CR_MONO;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900813 io->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900814 break;
815 case SH_FSI_FMT_MONO_DELAY:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900816 data = CR_MONO_D;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900817 io->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900818 break;
819 case SH_FSI_FMT_PCM:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900820 data = CR_PCM;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900821 io->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900822 break;
823 case SH_FSI_FMT_I2S:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900824 data = CR_I2S;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900825 io->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900826 break;
827 case SH_FSI_FMT_TDM:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900828 io->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900829 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900830 data = CR_TDM | (io->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900831 break;
832 case SH_FSI_FMT_TDM_DELAY:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900833 io->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900834 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900835 data = CR_TDM_D | (io->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900836 break;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900837 case SH_FSI_FMT_SPDIF:
838 if (master->core->ver < 2) {
839 dev_err(dai->dev, "This FSI can not use SPDIF\n");
840 return -EINVAL;
841 }
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900842 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900843 io->chan_num = 2;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900844 fsi_spdif_clk_ctrl(fsi, 1);
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900845 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900846 break;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900847 default:
848 dev_err(dai->dev, "unknown format.\n");
849 return -EINVAL;
850 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900851 fsi_reg_write(fsi, reg, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900852
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900853 /* irq clear */
854 fsi_irq_disable(fsi, is_play);
855 fsi_irq_clear_status(fsi);
856
857 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900858 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900859
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900860 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861}
862
863static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
864 struct snd_soc_dai *dai)
865{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900866 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto00545782010-10-12 18:30:14 +0900867 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900868
869 fsi_irq_disable(fsi, is_play);
870 fsi_clk_ctrl(fsi, 0);
871
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900872 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900873}
874
875static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
876 struct snd_soc_dai *dai)
877{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900878 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900879 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900880 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900881 int ret = 0;
882
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900883 switch (cmd) {
884 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900885 fsi_stream_push(fsi, is_play, substream,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900886 frames_to_bytes(runtime, runtime->buffer_size),
887 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900888 ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900889 break;
890 case SNDRV_PCM_TRIGGER_STOP:
891 fsi_irq_disable(fsi, is_play);
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900892 fsi_stream_pop(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900893 break;
894 }
895
896 return ret;
897}
898
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900899static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
900 struct snd_pcm_hw_params *params,
901 struct snd_soc_dai *dai)
902{
903 struct fsi_priv *fsi = fsi_get_priv(substream);
904 struct fsi_master *master = fsi_get_master(fsi);
905 int (*set_rate)(int is_porta, int rate) = master->info->set_rate;
906 int fsi_ver = master->core->ver;
Kuninori Morimoto00545782010-10-12 18:30:14 +0900907 int is_play = fsi_is_play(substream);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900908 int ret;
909
910 /* if slave mode, set_rate is not needed */
911 if (!fsi_is_master_mode(fsi, is_play))
912 return 0;
913
914 /* it is error if no set_rate */
915 if (!set_rate)
916 return -EIO;
917
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900918 ret = set_rate(fsi_is_port_a(fsi), params_rate(params));
919 if (ret > 0) {
920 u32 data = 0;
921
922 switch (ret & SH_FSI_ACKMD_MASK) {
923 default:
924 /* FALL THROUGH */
925 case SH_FSI_ACKMD_512:
926 data |= (0x0 << 12);
927 break;
928 case SH_FSI_ACKMD_256:
929 data |= (0x1 << 12);
930 break;
931 case SH_FSI_ACKMD_128:
932 data |= (0x2 << 12);
933 break;
934 case SH_FSI_ACKMD_64:
935 data |= (0x3 << 12);
936 break;
937 case SH_FSI_ACKMD_32:
938 if (fsi_ver < 2)
939 dev_err(dai->dev, "unsupported ACKMD\n");
940 else
941 data |= (0x4 << 12);
942 break;
943 }
944
945 switch (ret & SH_FSI_BPFMD_MASK) {
946 default:
947 /* FALL THROUGH */
948 case SH_FSI_BPFMD_32:
949 data |= (0x0 << 8);
950 break;
951 case SH_FSI_BPFMD_64:
952 data |= (0x1 << 8);
953 break;
954 case SH_FSI_BPFMD_128:
955 data |= (0x2 << 8);
956 break;
957 case SH_FSI_BPFMD_256:
958 data |= (0x3 << 8);
959 break;
960 case SH_FSI_BPFMD_512:
961 data |= (0x4 << 8);
962 break;
963 case SH_FSI_BPFMD_16:
964 if (fsi_ver < 2)
965 dev_err(dai->dev, "unsupported ACKMD\n");
966 else
967 data |= (0x7 << 8);
968 break;
969 }
970
971 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
972 udelay(10);
973 fsi_clk_ctrl(fsi, 1);
974 ret = 0;
975 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900976
977 return ret;
978
979}
980
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900981static struct snd_soc_dai_ops fsi_dai_ops = {
982 .startup = fsi_dai_startup,
983 .shutdown = fsi_dai_shutdown,
984 .trigger = fsi_dai_trigger,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900985 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900986};
987
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900988/*
989 * pcm ops
990 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900991
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900992static struct snd_pcm_hardware fsi_pcm_hardware = {
993 .info = SNDRV_PCM_INFO_INTERLEAVED |
994 SNDRV_PCM_INFO_MMAP |
995 SNDRV_PCM_INFO_MMAP_VALID |
996 SNDRV_PCM_INFO_PAUSE,
997 .formats = FSI_FMTS,
998 .rates = FSI_RATES,
999 .rate_min = 8000,
1000 .rate_max = 192000,
1001 .channels_min = 1,
1002 .channels_max = 2,
1003 .buffer_bytes_max = 64 * 1024,
1004 .period_bytes_min = 32,
1005 .period_bytes_max = 8192,
1006 .periods_min = 1,
1007 .periods_max = 32,
1008 .fifo_size = 256,
1009};
1010
1011static int fsi_pcm_open(struct snd_pcm_substream *substream)
1012{
1013 struct snd_pcm_runtime *runtime = substream->runtime;
1014 int ret = 0;
1015
1016 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1017
1018 ret = snd_pcm_hw_constraint_integer(runtime,
1019 SNDRV_PCM_HW_PARAM_PERIODS);
1020
1021 return ret;
1022}
1023
1024static int fsi_hw_params(struct snd_pcm_substream *substream,
1025 struct snd_pcm_hw_params *hw_params)
1026{
1027 return snd_pcm_lib_malloc_pages(substream,
1028 params_buffer_bytes(hw_params));
1029}
1030
1031static int fsi_hw_free(struct snd_pcm_substream *substream)
1032{
1033 return snd_pcm_lib_free_pages(substream);
1034}
1035
1036static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1037{
1038 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001039 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001040 struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001041 long location;
1042
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001043 location = (io->buff_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001044 if (location < 0)
1045 location = 0;
1046
1047 return bytes_to_frames(runtime, location);
1048}
1049
1050static struct snd_pcm_ops fsi_pcm_ops = {
1051 .open = fsi_pcm_open,
1052 .ioctl = snd_pcm_lib_ioctl,
1053 .hw_params = fsi_hw_params,
1054 .hw_free = fsi_hw_free,
1055 .pointer = fsi_pointer,
1056};
1057
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001058/*
1059 * snd_soc_platform
1060 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001061
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001062#define PREALLOC_BUFFER (32 * 1024)
1063#define PREALLOC_BUFFER_MAX (32 * 1024)
1064
1065static void fsi_pcm_free(struct snd_pcm *pcm)
1066{
1067 snd_pcm_lib_preallocate_free_for_all(pcm);
1068}
1069
1070static int fsi_pcm_new(struct snd_card *card,
1071 struct snd_soc_dai *dai,
1072 struct snd_pcm *pcm)
1073{
1074 /*
1075 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1076 * in MMAP mode (i.e. aplay -M)
1077 */
1078 return snd_pcm_lib_preallocate_pages_for_all(
1079 pcm,
1080 SNDRV_DMA_TYPE_CONTINUOUS,
1081 snd_dma_continuous_data(GFP_KERNEL),
1082 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1083}
1084
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001085/*
1086 * alsa struct
1087 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001089static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001090 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001091 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001092 .playback = {
1093 .rates = FSI_RATES,
1094 .formats = FSI_FMTS,
1095 .channels_min = 1,
1096 .channels_max = 8,
1097 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001098 .capture = {
1099 .rates = FSI_RATES,
1100 .formats = FSI_FMTS,
1101 .channels_min = 1,
1102 .channels_max = 8,
1103 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001104 .ops = &fsi_dai_ops,
1105 },
1106 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001107 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001108 .playback = {
1109 .rates = FSI_RATES,
1110 .formats = FSI_FMTS,
1111 .channels_min = 1,
1112 .channels_max = 8,
1113 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001114 .capture = {
1115 .rates = FSI_RATES,
1116 .formats = FSI_FMTS,
1117 .channels_min = 1,
1118 .channels_max = 8,
1119 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001120 .ops = &fsi_dai_ops,
1121 },
1122};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001123
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001124static struct snd_soc_platform_driver fsi_soc_platform = {
1125 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001126 .pcm_new = fsi_pcm_new,
1127 .pcm_free = fsi_pcm_free,
1128};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001129
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001130/*
1131 * platform function
1132 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001133
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001134static int fsi_probe(struct platform_device *pdev)
1135{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001136 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001137 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001138 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001139 unsigned int irq;
1140 int ret;
1141
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001142 id_entry = pdev->id_entry;
1143 if (!id_entry) {
1144 dev_err(&pdev->dev, "unknown fsi device\n");
1145 return -ENODEV;
1146 }
1147
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001148 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1149 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001150 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001151 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1152 ret = -ENODEV;
1153 goto exit;
1154 }
1155
1156 master = kzalloc(sizeof(*master), GFP_KERNEL);
1157 if (!master) {
1158 dev_err(&pdev->dev, "Could not allocate master\n");
1159 ret = -ENOMEM;
1160 goto exit;
1161 }
1162
1163 master->base = ioremap_nocache(res->start, resource_size(res));
1164 if (!master->base) {
1165 ret = -ENXIO;
1166 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1167 goto exit_kfree;
1168 }
1169
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001170 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001171 master->irq = irq;
1172 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001173 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001174 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001175
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001176 /* FSI A setting */
1177 master->fsia.base = master->base;
1178 master->fsia.master = master;
1179 master->fsia.mst_ctrl = A_MST_CTLR;
1180
1181 /* FSI B setting */
1182 master->fsib.base = master->base + 0x40;
1183 master->fsib.master = master;
1184 master->fsib.mst_ctrl = B_MST_CTLR;
1185
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001186 pm_runtime_enable(&pdev->dev);
1187 pm_runtime_resume(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001189
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001190 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001191
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001192 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1193 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001194 if (ret) {
1195 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001196 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001197 }
1198
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001199 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001200 if (ret < 0) {
1201 dev_err(&pdev->dev, "cannot snd soc register\n");
1202 goto exit_free_irq;
1203 }
1204
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001205 return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001206
1207exit_free_irq:
1208 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001209exit_iounmap:
1210 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001211 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001212exit_kfree:
1213 kfree(master);
1214 master = NULL;
1215exit:
1216 return ret;
1217}
1218
1219static int fsi_remove(struct platform_device *pdev)
1220{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001221 struct fsi_master *master;
1222
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001223 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001224
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001225 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1226 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001227
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001228 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001229
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001230 free_irq(master->irq, master);
1231
1232 iounmap(master->base);
1233 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001234
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001235 return 0;
1236}
1237
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001238static int fsi_runtime_nop(struct device *dev)
1239{
1240 /* Runtime PM callback shared between ->runtime_suspend()
1241 * and ->runtime_resume(). Simply returns success.
1242 *
1243 * This driver re-initializes all registers after
1244 * pm_runtime_get_sync() anyway so there is no need
1245 * to save and restore registers here.
1246 */
1247 return 0;
1248}
1249
1250static struct dev_pm_ops fsi_pm_ops = {
1251 .runtime_suspend = fsi_runtime_nop,
1252 .runtime_resume = fsi_runtime_nop,
1253};
1254
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001255static struct fsi_core fsi1_core = {
1256 .ver = 1,
1257
1258 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001259 .int_st = INT_ST,
1260 .iemsk = IEMSK,
1261 .imsk = IMSK,
1262};
1263
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001264static struct fsi_core fsi2_core = {
1265 .ver = 2,
1266
1267 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001268 .int_st = CPU_INT_ST,
1269 .iemsk = CPU_IEMSK,
1270 .imsk = CPU_IMSK,
1271};
1272
1273static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001274 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1275 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001276 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001277};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001278MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001279
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001280static struct platform_driver fsi_driver = {
1281 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001282 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001283 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001284 },
1285 .probe = fsi_probe,
1286 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001287 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001288};
1289
1290static int __init fsi_mobile_init(void)
1291{
1292 return platform_driver_register(&fsi_driver);
1293}
1294
1295static void __exit fsi_mobile_exit(void)
1296{
1297 platform_driver_unregister(&fsi_driver);
1298}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001299
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001300module_init(fsi_mobile_init);
1301module_exit(fsi_mobile_exit);
1302
1303MODULE_LICENSE("GPL");
1304MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1305MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");