blob: 7c93b7c2fdbd9f4a2eb341d8b69a07fdddbd0d12 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040019#include <linux/module.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090020#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090021#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090022
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090023/* PortA/PortB register */
24#define REG_DO_FMT 0x0000
25#define REG_DOFF_CTL 0x0004
26#define REG_DOFF_ST 0x0008
27#define REG_DI_FMT 0x000C
28#define REG_DIFF_CTL 0x0010
29#define REG_DIFF_ST 0x0014
30#define REG_CKG1 0x0018
31#define REG_CKG2 0x001C
32#define REG_DIDT 0x0020
33#define REG_DODT 0x0024
34#define REG_MUTE_ST 0x0028
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080035#define REG_OUT_DMAC 0x002C
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090036#define REG_OUT_SEL 0x0030
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080037#define REG_IN_DMAC 0x0038
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090038
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090039/* master register */
40#define MST_CLK_RST 0x0210
41#define MST_SOFT_RST 0x0214
42#define MST_FIFO_SZ 0x0218
43
44/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090045#define A_MST_CTLR 0x0180
46#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090047#define CPU_INT_ST 0x01F4
48#define CPU_IEMSK 0x01F8
49#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090050#define INT_ST 0x0200
51#define IEMSK 0x0204
52#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090053
54/* DO_FMT */
55/* DI_FMT */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090056#define CR_BWS_24 (0x0 << 20) /* FSI2 */
57#define CR_BWS_16 (0x1 << 20) /* FSI2 */
58#define CR_BWS_20 (0x2 << 20) /* FSI2 */
59
60#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
61#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
62#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
63
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090064#define CR_MONO (0x0 << 4)
65#define CR_MONO_D (0x1 << 4)
66#define CR_PCM (0x2 << 4)
67#define CR_I2S (0x3 << 4)
68#define CR_TDM (0x4 << 4)
69#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090070
71/* DOFF_CTL */
72/* DIFF_CTL */
73#define IRQ_HALF 0x00100000
74#define FIFO_CLR 0x00000001
75
76/* DOFF_ST */
77#define ERR_OVER 0x00000010
78#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090079#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090080
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090081/* CKG1 */
82#define ACKMD_MASK 0x00007000
83#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090084#define DIMD (1 << 4)
85#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090086
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090087/* A/B MST_CTLR */
88#define BP (1 << 4) /* Fix the signal of Biphase output */
89#define SE (1 << 0) /* Fix the master clock */
90
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090091/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +090092#define CRB (1 << 4)
93#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090094
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090095/* IO SHIFT / MACRO */
96#define BI_SHIFT 12
97#define BO_SHIFT 8
98#define AI_SHIFT 4
99#define AO_SHIFT 0
100#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900101
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900102/* SOFT_RST */
103#define PBSR (1 << 12) /* Port B Software Reset */
104#define PASR (1 << 8) /* Port A Software Reset */
105#define IR (1 << 4) /* Interrupt Reset */
106#define FSISR (1 << 0) /* Software Reset */
107
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900108/* OUT_SEL (FSI2) */
109#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
110 /* 1: Biphase and serial */
111
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900112/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900113#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900114
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900115#define FSI_RATES SNDRV_PCM_RATE_8000_96000
116
117#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
118
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900119typedef int (*set_rate_func)(struct device *dev, int is_porta, int rate, int enable);
120
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900121/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900122 * FSI driver use below type name for variable
123 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900124 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900125 * xxx_pos : position of data
126 * xxx_capa : capacity of data
127 */
128
129/*
130 * period/frame/sample image
131 *
132 * ex) PCM (2ch)
133 *
134 * period pos period pos
135 * [n] [n + 1]
136 * |<-------------------- period--------------------->|
137 * ==|============================================ ... =|==
138 * | |
139 * ||<----- frame ----->|<------ frame ----->| ... |
140 * |+--------------------+--------------------+- ... |
141 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
142 * |+--------------------+--------------------+- ... |
143 * ==|============================================ ... =|==
144 */
145
146/*
147 * FSI FIFO image
148 *
149 * | |
150 * | |
151 * | [ sample ] |
152 * | [ sample ] |
153 * | [ sample ] |
154 * | [ sample ] |
155 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900156 */
157
158/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900159 * struct
160 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900161
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800162struct fsi_stream_handler;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900163struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900164
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800165 /*
166 * these are initialized by fsi_stream_init()
167 */
168 struct snd_pcm_substream *substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900169 int fifo_sample_capa; /* sample capacity of FSI FIFO */
170 int buff_sample_capa; /* sample capacity of ALSA buffer */
171 int buff_sample_pos; /* sample position of ALSA buffer */
172 int period_samples; /* sample number / 1 period */
173 int period_pos; /* current period position */
Kuninori Morimotoc1e6f102012-02-03 00:50:09 -0800174 int sample_width; /* sample width */
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900175 int uerr_num;
176 int oerr_num;
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800177
178 /*
179 * thse are initialized by fsi_handler_init()
180 */
181 struct fsi_stream_handler *handler;
182 struct fsi_priv *priv;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900183};
184
185struct fsi_priv {
186 void __iomem *base;
187 struct fsi_master *master;
188
189 struct fsi_stream playback;
190 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900191
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900192 u32 do_fmt;
193 u32 di_fmt;
194
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900195 int chan_num:16;
196 int clk_master:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900197 int spdif:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900198
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000199 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900200};
201
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800202struct fsi_stream_handler {
203 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io);
204 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
205 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
206};
207#define fsi_stream_handler_call(io, func, args...) \
208 (!(io) ? -ENODEV : \
209 !((io)->handler->func) ? 0 : \
210 (io)->handler->func(args))
211
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900212struct fsi_core {
213 int ver;
214
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900215 u32 int_st;
216 u32 iemsk;
217 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900218 u32 a_mclk;
219 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900220};
221
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900222struct fsi_master {
223 void __iomem *base;
224 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900225 struct fsi_priv fsia;
226 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900227 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900228 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900229 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900230};
231
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800232static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
233
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900234/*
235 * basic read write function
236 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900237
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200238static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900239{
240 /* valid data area is 24bit */
241 data &= 0x00ffffff;
242
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100243 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900244}
245
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200246static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100248 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900249}
250
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200251static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900252{
253 u32 val = __fsi_reg_read(reg);
254
255 val &= ~mask;
256 val |= data & mask;
257
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100258 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900259}
260
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900261#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700262 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900263
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900264#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700265 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900266
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900267#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700268 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900269
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900270#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
271#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
272static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900273{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900274 u32 ret;
275 unsigned long flags;
276
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900277 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200278 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900279 spin_unlock_irqrestore(&master->lock, flags);
280
281 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900282}
283
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900284#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
285#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
286static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900287 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900288{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900289 unsigned long flags;
290
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900291 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200292 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900293 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900294}
295
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900296/*
297 * basic function
298 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900299
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900300static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900301{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900302 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900303}
304
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900305static int fsi_is_clk_master(struct fsi_priv *fsi)
306{
307 return fsi->clk_master;
308}
309
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900310static int fsi_is_port_a(struct fsi_priv *fsi)
311{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900312 return fsi->master->base == fsi->base;
313}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900314
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900315static int fsi_is_spdif(struct fsi_priv *fsi)
316{
317 return fsi->spdif;
318}
319
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800320static int fsi_is_play(struct snd_pcm_substream *substream)
321{
322 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
323}
324
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900325static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900326{
327 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900328
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000329 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900330}
331
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900332static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900333{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000334 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900335
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000336 if (dai->id == 0)
337 return &master->fsia;
338 else
339 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900340}
341
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900342static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
343{
344 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
345}
346
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900347static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
348{
349 if (!master->info)
350 return NULL;
351
352 return master->info->set_rate;
353}
354
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900355static u32 fsi_get_info_flags(struct fsi_priv *fsi)
356{
357 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900358 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900359
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900360 if (!master->info)
361 return 0;
362
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900363 return is_porta ? master->info->porta_flags :
364 master->info->portb_flags;
365}
366
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900367static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900368{
369 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900370 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900371
372 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900373 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900374 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900375 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900376
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900377 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900378}
379
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900380static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
381{
382 return frames * fsi->chan_num;
383}
384
385static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
386{
387 return samples / fsi->chan_num;
388}
389
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800390static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
391 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900392{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800393 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900394 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900395 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900396
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900397 status = is_play ?
398 fsi_reg_read(fsi, DOFF_ST) :
399 fsi_reg_read(fsi, DIFF_ST);
400
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900401 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900402
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900403 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900404}
405
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900406static void fsi_count_fifo_err(struct fsi_priv *fsi)
407{
408 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
409 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
410
411 if (ostatus & ERR_OVER)
412 fsi->playback.oerr_num++;
413
414 if (ostatus & ERR_UNDER)
415 fsi->playback.uerr_num++;
416
417 if (istatus & ERR_OVER)
418 fsi->capture.oerr_num++;
419
420 if (istatus & ERR_UNDER)
421 fsi->capture.uerr_num++;
422
423 fsi_reg_write(fsi, DOFF_ST, 0);
424 fsi_reg_write(fsi, DIFF_ST, 0);
425}
426
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900427/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800428 * fsi_stream_xx() function
429 */
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800430static inline int fsi_stream_is_play(struct fsi_priv *fsi,
431 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800432{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800433 return &fsi->playback == io;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800434}
435
436static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
437 int is_play)
438{
439 return is_play ? &fsi->playback : &fsi->capture;
440}
441
442static int fsi_stream_is_working(struct fsi_priv *fsi,
443 int is_play)
444{
445 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
446 struct fsi_master *master = fsi_get_master(fsi);
447 unsigned long flags;
448 int ret;
449
450 spin_lock_irqsave(&master->lock, flags);
451 ret = !!io->substream;
452 spin_unlock_irqrestore(&master->lock, flags);
453
454 return ret;
455}
456
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800457static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
458{
459 return io->priv;
460}
461
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800462static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800463 int is_play,
464 struct snd_pcm_substream *substream)
465{
466 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
467 struct snd_pcm_runtime *runtime = substream->runtime;
468 struct fsi_master *master = fsi_get_master(fsi);
469 unsigned long flags;
470
471 spin_lock_irqsave(&master->lock, flags);
472 io->substream = substream;
473 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
474 io->buff_sample_pos = 0;
475 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
476 io->period_pos = 0;
477 io->sample_width = samples_to_bytes(runtime, 1);
478 io->oerr_num = -1; /* ignore 1st err */
479 io->uerr_num = -1; /* ignore 1st err */
480 spin_unlock_irqrestore(&master->lock, flags);
481}
482
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800483static void fsi_stream_quit(struct fsi_priv *fsi, int is_play)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800484{
485 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
486 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
487 struct fsi_master *master = fsi_get_master(fsi);
488 unsigned long flags;
489
490 spin_lock_irqsave(&master->lock, flags);
491
492 if (io->oerr_num > 0)
493 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
494
495 if (io->uerr_num > 0)
496 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
497
498 io->substream = NULL;
499 io->buff_sample_capa = 0;
500 io->buff_sample_pos = 0;
501 io->period_samples = 0;
502 io->period_pos = 0;
503 io->sample_width = 0;
504 io->oerr_num = 0;
505 io->uerr_num = 0;
506 spin_unlock_irqrestore(&master->lock, flags);
507}
508
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800509static int fsi_stream_transfer(struct fsi_stream *io)
510{
511 struct fsi_priv *fsi = fsi_stream_to_priv(io);
512 if (!fsi)
513 return -EIO;
514
515 return fsi_stream_handler_call(io, transfer, fsi, io);
516}
517
518static int fsi_stream_probe(struct fsi_priv *fsi)
519{
520 struct fsi_stream *io;
521 int ret1, ret2;
522
523 io = &fsi->playback;
524 ret1 = fsi_stream_handler_call(io, probe, fsi, io);
525
526 io = &fsi->capture;
527 ret2 = fsi_stream_handler_call(io, probe, fsi, io);
528
529 if (ret1 < 0)
530 return ret1;
531 if (ret2 < 0)
532 return ret2;
533
534 return 0;
535}
536
537static int fsi_stream_remove(struct fsi_priv *fsi)
538{
539 struct fsi_stream *io;
540 int ret1, ret2;
541
542 io = &fsi->playback;
543 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
544
545 io = &fsi->capture;
546 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
547
548 if (ret1 < 0)
549 return ret1;
550 if (ret2 < 0)
551 return ret2;
552
553 return 0;
554}
555
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800556/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900557 * irq function
558 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900559
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900560static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
561{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900562 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900563 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900564
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900565 fsi_core_mask_set(master, imsk, data, data);
566 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900567}
568
569static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
570{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900571 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900572 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900573
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900574 fsi_core_mask_set(master, imsk, data, 0);
575 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900576}
577
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900578static u32 fsi_irq_get_status(struct fsi_master *master)
579{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900580 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900581}
582
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900583static void fsi_irq_clear_status(struct fsi_priv *fsi)
584{
585 u32 data = 0;
586 struct fsi_master *master = fsi_get_master(fsi);
587
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900588 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
589 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900590
591 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900592 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900593}
594
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900595/*
596 * SPDIF master clock function
597 *
598 * These functions are used later FSI2
599 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900600static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
601{
602 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900603 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900604
605 if (master->core->ver < 2) {
606 pr_err("fsi: register access err (%s)\n", __func__);
607 return;
608 }
609
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900610 mask = BP | SE;
611 val = enable ? mask : 0;
612
613 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900614 fsi_core_mask_set(master, a_mclk, mask, val) :
615 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900616}
617
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900618/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900619 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900620 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900621static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
622 long rate, int enable)
623{
624 struct fsi_master *master = fsi_get_master(fsi);
625 set_rate_func set_rate = fsi_get_info_set_rate(master);
626 int fsi_ver = master->core->ver;
627 int ret;
628
629 ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
630 if (ret < 0) /* error */
631 return ret;
632
633 if (!enable)
634 return 0;
635
636 if (ret > 0) {
637 u32 data = 0;
638
639 switch (ret & SH_FSI_ACKMD_MASK) {
640 default:
641 /* FALL THROUGH */
642 case SH_FSI_ACKMD_512:
643 data |= (0x0 << 12);
644 break;
645 case SH_FSI_ACKMD_256:
646 data |= (0x1 << 12);
647 break;
648 case SH_FSI_ACKMD_128:
649 data |= (0x2 << 12);
650 break;
651 case SH_FSI_ACKMD_64:
652 data |= (0x3 << 12);
653 break;
654 case SH_FSI_ACKMD_32:
655 if (fsi_ver < 2)
656 dev_err(dev, "unsupported ACKMD\n");
657 else
658 data |= (0x4 << 12);
659 break;
660 }
661
662 switch (ret & SH_FSI_BPFMD_MASK) {
663 default:
664 /* FALL THROUGH */
665 case SH_FSI_BPFMD_32:
666 data |= (0x0 << 8);
667 break;
668 case SH_FSI_BPFMD_64:
669 data |= (0x1 << 8);
670 break;
671 case SH_FSI_BPFMD_128:
672 data |= (0x2 << 8);
673 break;
674 case SH_FSI_BPFMD_256:
675 data |= (0x3 << 8);
676 break;
677 case SH_FSI_BPFMD_512:
678 data |= (0x4 << 8);
679 break;
680 case SH_FSI_BPFMD_16:
681 if (fsi_ver < 2)
682 dev_err(dev, "unsupported ACKMD\n");
683 else
684 data |= (0x7 << 8);
685 break;
686 }
687
688 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
689 udelay(10);
690 ret = 0;
691 }
692
693 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900694}
695
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900696#define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
697#define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
698static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900699{
700 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900701 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900702
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +0900703 if (enable)
704 fsi_irq_enable(fsi, is_play);
705 else
706 fsi_irq_disable(fsi, is_play);
707
Kuninori Morimotocda828c2011-05-23 20:46:35 +0900708 if (fsi_is_clk_master(fsi))
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900709 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
710}
711
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800712
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900713/*
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800714 * pio data transfer handler
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900715 */
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800716static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
717{
718 u16 *buf = (u16 *)_buf;
719 int i;
720
721 for (i = 0; i < samples; i++)
722 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
723}
724
725static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
726{
727 u16 *buf = (u16 *)_buf;
728 int i;
729
730 for (i = 0; i < samples; i++)
731 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
732}
733
734static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
735{
736 u32 *buf = (u32 *)_buf;
737 int i;
738
739 for (i = 0; i < samples; i++)
740 fsi_reg_write(fsi, DODT, *(buf + i));
741}
742
743static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
744{
745 u32 *buf = (u32 *)_buf;
746 int i;
747
748 for (i = 0; i < samples; i++)
749 *(buf + i) = fsi_reg_read(fsi, DIDT);
750}
751
752static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
753{
754 struct snd_pcm_runtime *runtime = io->substream->runtime;
755
756 return runtime->dma_area +
757 samples_to_bytes(runtime, io->buff_sample_pos);
758}
759
760static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800761 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
762 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
763 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900764{
765 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800766 struct snd_pcm_substream *substream;
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800767 u8 *buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900768 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900769
770 if (!fsi ||
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900771 !io->substream ||
772 !io->substream->runtime)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900773 return -EINVAL;
774
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900775 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900776 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900777 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900778
779 /* FSI FIFO has limit.
780 * So, this driver can not send periods data at a time
781 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900782 if (io->buff_sample_pos >=
783 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900784
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900785 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900786 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900787
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900788 if (0 == io->period_pos)
789 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900790 }
791
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800792 buf = fsi_pio_get_area(fsi, io);
793
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800794 switch (io->sample_width) {
795 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800796 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800797 break;
798 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800799 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800800 break;
801 default:
802 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900803 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900804
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900805 /* update buff_sample_pos */
806 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900807
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900808 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900809 snd_pcm_period_elapsed(substream);
810
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900811 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900812}
813
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800814static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900815{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800816 int sample_residues; /* samples in FSI fifo */
817 int sample_space; /* ALSA free samples space */
818 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800819
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800820 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800821 sample_space = io->buff_sample_capa - io->buff_sample_pos;
822
823 samples = min(sample_residues, sample_space);
824
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800825 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800826 fsi_pio_pop16,
827 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800828 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900829}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900830
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800831static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900832{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800833 int sample_residues; /* ALSA residue samples */
834 int sample_space; /* FSI fifo free samples space */
835 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800836
837 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
838 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800839 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800840
841 samples = min(sample_residues, sample_space);
842
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800843 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800844 fsi_pio_push16,
845 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800846 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900847}
848
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800849static struct fsi_stream_handler fsi_pio_push_handler = {
850 .transfer = fsi_pio_push,
851};
852
853static struct fsi_stream_handler fsi_pio_pop_handler = {
854 .transfer = fsi_pio_pop,
855};
856
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900857static irqreturn_t fsi_interrupt(int irq, void *data)
858{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900859 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900860 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861
862 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900863 fsi_master_mask_set(master, SOFT_RST, IR, 0);
864 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900865
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900866 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800867 fsi_stream_transfer(&master->fsia.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900868 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800869 fsi_stream_transfer(&master->fsib.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900870 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800871 fsi_stream_transfer(&master->fsia.capture);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900872 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800873 fsi_stream_transfer(&master->fsib.capture);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900874
875 fsi_count_fifo_err(&master->fsia);
876 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900877
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900878 fsi_irq_clear_status(&master->fsia);
879 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900880
881 return IRQ_HANDLED;
882}
883
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900884/*
885 * dai ops
886 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -0800887static void fsi_fifo_init(struct fsi_priv *fsi,
888 int is_play,
889 struct device *dev)
890{
891 struct fsi_master *master = fsi_get_master(fsi);
892 struct fsi_stream *io = fsi_stream_get(fsi, is_play);
893 u32 shift, i;
894 int frame_capa;
895
896 /* get on-chip RAM capacity */
897 shift = fsi_master_read(master, FIFO_SZ);
898 shift >>= fsi_get_port_shift(fsi, is_play);
899 shift &= FIFO_SZ_MASK;
900 frame_capa = 256 << shift;
901 dev_dbg(dev, "fifo = %d words\n", frame_capa);
902
903 /*
904 * The maximum number of sample data varies depending
905 * on the number of channels selected for the format.
906 *
907 * FIFOs are used in 4-channel units in 3-channel mode
908 * and in 8-channel units in 5- to 7-channel mode
909 * meaning that more FIFOs than the required size of DPRAM
910 * are used.
911 *
912 * ex) if 256 words of DP-RAM is connected
913 * 1 channel: 256 (256 x 1 = 256)
914 * 2 channels: 128 (128 x 2 = 256)
915 * 3 channels: 64 ( 64 x 3 = 192)
916 * 4 channels: 64 ( 64 x 4 = 256)
917 * 5 channels: 32 ( 32 x 5 = 160)
918 * 6 channels: 32 ( 32 x 6 = 192)
919 * 7 channels: 32 ( 32 x 7 = 224)
920 * 8 channels: 32 ( 32 x 8 = 256)
921 */
922 for (i = 1; i < fsi->chan_num; i <<= 1)
923 frame_capa >>= 1;
924 dev_dbg(dev, "%d channel %d store\n",
925 fsi->chan_num, frame_capa);
926
927 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
928
929 /*
930 * set interrupt generation factor
931 * clear FIFO
932 */
933 if (is_play) {
934 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
935 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
936 } else {
937 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
938 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
939 }
940}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900941
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900942static int fsi_hw_startup(struct fsi_priv *fsi,
943 int is_play,
944 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900945{
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800946 struct fsi_master *master = fsi_get_master(fsi);
947 int fsi_ver = master->core->ver;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900948 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900949 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900950
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900951 /* clock setting */
952 if (fsi_is_clk_master(fsi))
953 data = DIMD | DOMD;
954
955 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900956
957 /* clock inversion (CKG2) */
958 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900959 if (SH_FSI_LRM_INV & flags)
960 data |= 1 << 12;
961 if (SH_FSI_BRM_INV & flags)
962 data |= 1 << 8;
963 if (SH_FSI_LRS_INV & flags)
964 data |= 1 << 4;
965 if (SH_FSI_BRS_INV & flags)
966 data |= 1 << 0;
967
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900968 fsi_reg_write(fsi, CKG2, data);
969
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900970 /* set format */
971 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
972 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
973
974 /* spdif ? */
975 if (fsi_is_spdif(fsi)) {
976 fsi_spdif_clk_ctrl(fsi, 1);
977 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
978 }
979
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800980 /*
981 * FIXME
982 *
983 * FSI driver assumed that data package is in-back.
984 * FSI2 chip can select it.
985 */
986 if (fsi_ver >= 2) {
987 fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
988 fsi_reg_write(fsi, IN_DMAC, (1 << 4));
989 }
990
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900991 /* irq clear */
992 fsi_irq_disable(fsi, is_play);
993 fsi_irq_clear_status(fsi);
994
995 /* fifo init */
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900996 fsi_fifo_init(fsi, is_play, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900997
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900998 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900999}
1000
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001001static void fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001002 struct device *dev)
1003{
1004 if (fsi_is_clk_master(fsi))
1005 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001006}
1007
1008static int fsi_dai_startup(struct snd_pcm_substream *substream,
1009 struct snd_soc_dai *dai)
1010{
1011 struct fsi_priv *fsi = fsi_get_priv(substream);
1012 int is_play = fsi_is_play(substream);
1013
1014 return fsi_hw_startup(fsi, is_play, dai->dev);
1015}
1016
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001017static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1018 struct snd_soc_dai *dai)
1019{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001020 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001021
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001022 fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001023 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001024}
1025
1026static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1027 struct snd_soc_dai *dai)
1028{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001029 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001030 struct fsi_stream *io = fsi_stream_get(fsi, fsi_is_play(substream));
Kuninori Morimoto00545782010-10-12 18:30:14 +09001031 int is_play = fsi_is_play(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001032 int ret = 0;
1033
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001034 switch (cmd) {
1035 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto8c415292012-02-03 00:52:07 -08001036 fsi_stream_init(fsi, is_play, substream);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001037 ret = fsi_stream_transfer(io);
1038 if (0 == ret)
1039 fsi_port_start(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001040 break;
1041 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto1ddddd32011-05-23 20:46:23 +09001042 fsi_port_stop(fsi, is_play);
Kuninori Morimoto8c415292012-02-03 00:52:07 -08001043 fsi_stream_quit(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001044 break;
1045 }
1046
1047 return ret;
1048}
1049
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001050static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1051{
1052 u32 data = 0;
1053
1054 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1055 case SND_SOC_DAIFMT_I2S:
1056 data = CR_I2S;
1057 fsi->chan_num = 2;
1058 break;
1059 case SND_SOC_DAIFMT_LEFT_J:
1060 data = CR_PCM;
1061 fsi->chan_num = 2;
1062 break;
1063 default:
1064 return -EINVAL;
1065 }
1066
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001067 fsi->do_fmt = data;
1068 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001069
1070 return 0;
1071}
1072
1073static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1074{
1075 struct fsi_master *master = fsi_get_master(fsi);
1076 u32 data = 0;
1077
1078 if (master->core->ver < 2)
1079 return -EINVAL;
1080
1081 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1082 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001083 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001084
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001085 fsi->do_fmt = data;
1086 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001087
1088 return 0;
1089}
1090
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001091static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1092{
1093 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001094 struct fsi_master *master = fsi_get_master(fsi);
1095 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001096 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001097 int ret;
1098
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001099 /* set master/slave audio interface */
1100 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1101 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001102 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001103 break;
1104 case SND_SOC_DAIFMT_CBS_CFS:
1105 break;
1106 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001107 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001108 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001109
1110 if (fsi_is_clk_master(fsi) && !set_rate) {
1111 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001112 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001113 }
1114
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001115 /* set format */
1116 switch (flags & SH_FSI_FMT_MASK) {
1117 case SH_FSI_FMT_DAI:
1118 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1119 break;
1120 case SH_FSI_FMT_SPDIF:
1121 ret = fsi_set_fmt_spdif(fsi);
1122 break;
1123 default:
1124 ret = -EINVAL;
1125 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001126
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001127 return ret;
1128}
1129
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001130static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1131 struct snd_pcm_hw_params *params,
1132 struct snd_soc_dai *dai)
1133{
1134 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001135 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001136 int ret;
1137
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001138 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001139 return 0;
1140
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001141 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1142 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001143 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001144
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001145 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001146
1147 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001148}
1149
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001150static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001151 .startup = fsi_dai_startup,
1152 .shutdown = fsi_dai_shutdown,
1153 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001154 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001155 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001156};
1157
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001158/*
1159 * pcm ops
1160 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001161
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001162static struct snd_pcm_hardware fsi_pcm_hardware = {
1163 .info = SNDRV_PCM_INFO_INTERLEAVED |
1164 SNDRV_PCM_INFO_MMAP |
1165 SNDRV_PCM_INFO_MMAP_VALID |
1166 SNDRV_PCM_INFO_PAUSE,
1167 .formats = FSI_FMTS,
1168 .rates = FSI_RATES,
1169 .rate_min = 8000,
1170 .rate_max = 192000,
1171 .channels_min = 1,
1172 .channels_max = 2,
1173 .buffer_bytes_max = 64 * 1024,
1174 .period_bytes_min = 32,
1175 .period_bytes_max = 8192,
1176 .periods_min = 1,
1177 .periods_max = 32,
1178 .fifo_size = 256,
1179};
1180
1181static int fsi_pcm_open(struct snd_pcm_substream *substream)
1182{
1183 struct snd_pcm_runtime *runtime = substream->runtime;
1184 int ret = 0;
1185
1186 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1187
1188 ret = snd_pcm_hw_constraint_integer(runtime,
1189 SNDRV_PCM_HW_PARAM_PERIODS);
1190
1191 return ret;
1192}
1193
1194static int fsi_hw_params(struct snd_pcm_substream *substream,
1195 struct snd_pcm_hw_params *hw_params)
1196{
1197 return snd_pcm_lib_malloc_pages(substream,
1198 params_buffer_bytes(hw_params));
1199}
1200
1201static int fsi_hw_free(struct snd_pcm_substream *substream)
1202{
1203 return snd_pcm_lib_free_pages(substream);
1204}
1205
1206static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1207{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001208 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -08001209 struct fsi_stream *io = fsi_stream_get(fsi, fsi_is_play(substream));
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001210 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001211
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001212 if (samples_pos < 0)
1213 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001214
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001215 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001216}
1217
1218static struct snd_pcm_ops fsi_pcm_ops = {
1219 .open = fsi_pcm_open,
1220 .ioctl = snd_pcm_lib_ioctl,
1221 .hw_params = fsi_hw_params,
1222 .hw_free = fsi_hw_free,
1223 .pointer = fsi_pointer,
1224};
1225
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001226/*
1227 * snd_soc_platform
1228 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001229
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001230#define PREALLOC_BUFFER (32 * 1024)
1231#define PREALLOC_BUFFER_MAX (32 * 1024)
1232
1233static void fsi_pcm_free(struct snd_pcm *pcm)
1234{
1235 snd_pcm_lib_preallocate_free_for_all(pcm);
1236}
1237
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001238static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001239{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001240 struct snd_pcm *pcm = rtd->pcm;
1241
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001242 /*
1243 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1244 * in MMAP mode (i.e. aplay -M)
1245 */
1246 return snd_pcm_lib_preallocate_pages_for_all(
1247 pcm,
1248 SNDRV_DMA_TYPE_CONTINUOUS,
1249 snd_dma_continuous_data(GFP_KERNEL),
1250 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1251}
1252
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001253/*
1254 * alsa struct
1255 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001256
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001257static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001258 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001259 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001260 .playback = {
1261 .rates = FSI_RATES,
1262 .formats = FSI_FMTS,
1263 .channels_min = 1,
1264 .channels_max = 8,
1265 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001266 .capture = {
1267 .rates = FSI_RATES,
1268 .formats = FSI_FMTS,
1269 .channels_min = 1,
1270 .channels_max = 8,
1271 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001272 .ops = &fsi_dai_ops,
1273 },
1274 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001275 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001276 .playback = {
1277 .rates = FSI_RATES,
1278 .formats = FSI_FMTS,
1279 .channels_min = 1,
1280 .channels_max = 8,
1281 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001282 .capture = {
1283 .rates = FSI_RATES,
1284 .formats = FSI_FMTS,
1285 .channels_min = 1,
1286 .channels_max = 8,
1287 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001288 .ops = &fsi_dai_ops,
1289 },
1290};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001291
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001292static struct snd_soc_platform_driver fsi_soc_platform = {
1293 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001294 .pcm_new = fsi_pcm_new,
1295 .pcm_free = fsi_pcm_free,
1296};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001297
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001298/*
1299 * platform function
1300 */
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001301static void fsi_handler_init(struct fsi_priv *fsi)
1302{
1303 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1304 fsi->playback.priv = fsi;
1305 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1306 fsi->capture.priv = fsi;
1307}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001308
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001309static int fsi_probe(struct platform_device *pdev)
1310{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001311 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001312 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001313 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001314 unsigned int irq;
1315 int ret;
1316
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001317 id_entry = pdev->id_entry;
1318 if (!id_entry) {
1319 dev_err(&pdev->dev, "unknown fsi device\n");
1320 return -ENODEV;
1321 }
1322
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001323 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1324 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001325 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001326 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1327 ret = -ENODEV;
1328 goto exit;
1329 }
1330
1331 master = kzalloc(sizeof(*master), GFP_KERNEL);
1332 if (!master) {
1333 dev_err(&pdev->dev, "Could not allocate master\n");
1334 ret = -ENOMEM;
1335 goto exit;
1336 }
1337
1338 master->base = ioremap_nocache(res->start, resource_size(res));
1339 if (!master->base) {
1340 ret = -ENXIO;
1341 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1342 goto exit_kfree;
1343 }
1344
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001345 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001346 master->irq = irq;
1347 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001348 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001349 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001350
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001351 /* FSI A setting */
1352 master->fsia.base = master->base;
1353 master->fsia.master = master;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001354 fsi_handler_init(&master->fsia);
1355 ret = fsi_stream_probe(&master->fsia);
1356 if (ret < 0) {
1357 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1358 goto exit_iounmap;
1359 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001360
1361 /* FSI B setting */
1362 master->fsib.base = master->base + 0x40;
1363 master->fsib.master = master;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001364 fsi_handler_init(&master->fsib);
1365 ret = fsi_stream_probe(&master->fsib);
1366 if (ret < 0) {
1367 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1368 goto exit_fsia;
1369 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001370
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001371 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001372 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001373
Yong Zhang88e24c32011-09-22 16:59:20 +08001374 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001375 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001376 if (ret) {
1377 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001378 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001379 }
1380
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001381 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001382 if (ret < 0) {
1383 dev_err(&pdev->dev, "cannot snd soc register\n");
1384 goto exit_free_irq;
1385 }
1386
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001387 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1388 ARRAY_SIZE(fsi_soc_dai));
1389 if (ret < 0) {
1390 dev_err(&pdev->dev, "cannot snd dai register\n");
1391 goto exit_snd_soc;
1392 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001393
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001394 return ret;
1395
1396exit_snd_soc:
1397 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001398exit_free_irq:
1399 free_irq(irq, master);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001400exit_fsib:
1401 fsi_stream_remove(&master->fsib);
1402exit_fsia:
1403 fsi_stream_remove(&master->fsia);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001404exit_iounmap:
1405 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001406 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001407exit_kfree:
1408 kfree(master);
1409 master = NULL;
1410exit:
1411 return ret;
1412}
1413
1414static int fsi_remove(struct platform_device *pdev)
1415{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001416 struct fsi_master *master;
1417
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001418 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001419
Kuninori Morimotod985f272011-04-08 15:09:25 +09001420 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001421 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001422
Kuninori Morimotod985f272011-04-08 15:09:25 +09001423 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1424 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001425
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001426 fsi_stream_remove(&master->fsia);
1427 fsi_stream_remove(&master->fsib);
1428
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001429 iounmap(master->base);
1430 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001431
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001432 return 0;
1433}
1434
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001435static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001436 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001437 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001438{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001439 if (!fsi_stream_is_working(fsi, is_play))
1440 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001441
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001442 fsi_port_stop(fsi, is_play);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001443 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001444}
1445
1446static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001447 int is_play,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001448 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001449{
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001450 if (!fsi_stream_is_working(fsi, is_play))
1451 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001452
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001453 fsi_hw_startup(fsi, is_play, dev);
1454
1455 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001456 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001457
1458 fsi_port_start(fsi, is_play);
1459
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001460}
1461
1462static int fsi_suspend(struct device *dev)
1463{
1464 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001465 struct fsi_priv *fsia = &master->fsia;
1466 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001467
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001468 __fsi_suspend(fsia, 1, dev);
1469 __fsi_suspend(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001470
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001471 __fsi_suspend(fsib, 1, dev);
1472 __fsi_suspend(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001473
1474 return 0;
1475}
1476
1477static int fsi_resume(struct device *dev)
1478{
1479 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001480 struct fsi_priv *fsia = &master->fsia;
1481 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001482
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001483 __fsi_resume(fsia, 1, dev);
1484 __fsi_resume(fsia, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001485
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001486 __fsi_resume(fsib, 1, dev);
1487 __fsi_resume(fsib, 0, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001488
1489 return 0;
1490}
1491
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001492static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001493 .suspend = fsi_suspend,
1494 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001495};
1496
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001497static struct fsi_core fsi1_core = {
1498 .ver = 1,
1499
1500 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001501 .int_st = INT_ST,
1502 .iemsk = IEMSK,
1503 .imsk = IMSK,
1504};
1505
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001506static struct fsi_core fsi2_core = {
1507 .ver = 2,
1508
1509 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001510 .int_st = CPU_INT_ST,
1511 .iemsk = CPU_IEMSK,
1512 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001513 .a_mclk = A_MST_CTLR,
1514 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001515};
1516
1517static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001518 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1519 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001520 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001521};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001522MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001523
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001524static struct platform_driver fsi_driver = {
1525 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001527 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001528 },
1529 .probe = fsi_probe,
1530 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001531 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001532};
1533
Axel Lincb5e8732011-11-25 10:15:07 +08001534module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001535
1536MODULE_LICENSE("GPL");
1537MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1538MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001539MODULE_ALIAS("platform:fsi-pcm-audio");