blob: 1e10184af89a3921ce62b2ca9c52407b60556fde [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);
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800206 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
207 int enable);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800208};
209#define fsi_stream_handler_call(io, func, args...) \
210 (!(io) ? -ENODEV : \
211 !((io)->handler->func) ? 0 : \
212 (io)->handler->func(args))
213
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900214struct fsi_core {
215 int ver;
216
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900217 u32 int_st;
218 u32 iemsk;
219 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900220 u32 a_mclk;
221 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900222};
223
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900224struct fsi_master {
225 void __iomem *base;
226 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900227 struct fsi_priv fsia;
228 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900229 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900230 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900231 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900232};
233
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800234static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
235
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900236/*
237 * basic read write function
238 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900239
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200240static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900241{
242 /* valid data area is 24bit */
243 data &= 0x00ffffff;
244
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100245 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900246}
247
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200248static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900249{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100250 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900251}
252
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200253static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900254{
255 u32 val = __fsi_reg_read(reg);
256
257 val &= ~mask;
258 val |= data & mask;
259
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100260 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900261}
262
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900263#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700264 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900265
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900266#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700267 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900268
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900269#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700270 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900271
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900272#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
273#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
274static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900275{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900276 u32 ret;
277 unsigned long flags;
278
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900279 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200280 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900281 spin_unlock_irqrestore(&master->lock, flags);
282
283 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900284}
285
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900286#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
287#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
288static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900289 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900290{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900291 unsigned long flags;
292
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900293 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200294 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900295 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900296}
297
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900298/*
299 * basic function
300 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900301
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900302static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900303{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900304 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900305}
306
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900307static int fsi_is_clk_master(struct fsi_priv *fsi)
308{
309 return fsi->clk_master;
310}
311
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900312static int fsi_is_port_a(struct fsi_priv *fsi)
313{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900314 return fsi->master->base == fsi->base;
315}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900316
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900317static int fsi_is_spdif(struct fsi_priv *fsi)
318{
319 return fsi->spdif;
320}
321
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800322static int fsi_is_play(struct snd_pcm_substream *substream)
323{
324 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
325}
326
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900327static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900328{
329 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900330
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000331 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900332}
333
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900334static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900335{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000336 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900337
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000338 if (dai->id == 0)
339 return &master->fsia;
340 else
341 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900342}
343
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900344static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
345{
346 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
347}
348
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900349static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
350{
351 if (!master->info)
352 return NULL;
353
354 return master->info->set_rate;
355}
356
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900357static u32 fsi_get_info_flags(struct fsi_priv *fsi)
358{
359 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900360 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900361
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900362 if (!master->info)
363 return 0;
364
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900365 return is_porta ? master->info->porta_flags :
366 master->info->portb_flags;
367}
368
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800369static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900370{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800371 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900372 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900373 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900374
375 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900376 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900377 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900378 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900379
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900380 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900381}
382
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900383static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
384{
385 return frames * fsi->chan_num;
386}
387
388static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
389{
390 return samples / fsi->chan_num;
391}
392
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800393static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
394 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900395{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800396 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900397 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900398 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900399
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900400 status = is_play ?
401 fsi_reg_read(fsi, DOFF_ST) :
402 fsi_reg_read(fsi, DIFF_ST);
403
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900404 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900405
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900406 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900407}
408
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900409static void fsi_count_fifo_err(struct fsi_priv *fsi)
410{
411 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
412 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
413
414 if (ostatus & ERR_OVER)
415 fsi->playback.oerr_num++;
416
417 if (ostatus & ERR_UNDER)
418 fsi->playback.uerr_num++;
419
420 if (istatus & ERR_OVER)
421 fsi->capture.oerr_num++;
422
423 if (istatus & ERR_UNDER)
424 fsi->capture.uerr_num++;
425
426 fsi_reg_write(fsi, DOFF_ST, 0);
427 fsi_reg_write(fsi, DIFF_ST, 0);
428}
429
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900430/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800431 * fsi_stream_xx() function
432 */
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800433static inline int fsi_stream_is_play(struct fsi_priv *fsi,
434 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800435{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800436 return &fsi->playback == io;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800437}
438
439static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800440 struct snd_pcm_substream *substream)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800441{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800442 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800443}
444
445static int fsi_stream_is_working(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800446 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800447{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800448 struct fsi_master *master = fsi_get_master(fsi);
449 unsigned long flags;
450 int ret;
451
452 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800453 ret = !!(io->substream && io->substream->runtime);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800454 spin_unlock_irqrestore(&master->lock, flags);
455
456 return ret;
457}
458
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800459static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
460{
461 return io->priv;
462}
463
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800464static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800465 struct fsi_stream *io,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800466 struct snd_pcm_substream *substream)
467{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800468 struct snd_pcm_runtime *runtime = substream->runtime;
469 struct fsi_master *master = fsi_get_master(fsi);
470 unsigned long flags;
471
472 spin_lock_irqsave(&master->lock, flags);
473 io->substream = substream;
474 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
475 io->buff_sample_pos = 0;
476 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
477 io->period_pos = 0;
478 io->sample_width = samples_to_bytes(runtime, 1);
479 io->oerr_num = -1; /* ignore 1st err */
480 io->uerr_num = -1; /* ignore 1st err */
481 spin_unlock_irqrestore(&master->lock, flags);
482}
483
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800484static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800485{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800486 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
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800518#define fsi_stream_start(fsi, io)\
519 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
520
521#define fsi_stream_stop(fsi, io)\
522 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
523
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800524static int fsi_stream_probe(struct fsi_priv *fsi)
525{
526 struct fsi_stream *io;
527 int ret1, ret2;
528
529 io = &fsi->playback;
530 ret1 = fsi_stream_handler_call(io, probe, fsi, io);
531
532 io = &fsi->capture;
533 ret2 = fsi_stream_handler_call(io, probe, fsi, io);
534
535 if (ret1 < 0)
536 return ret1;
537 if (ret2 < 0)
538 return ret2;
539
540 return 0;
541}
542
543static int fsi_stream_remove(struct fsi_priv *fsi)
544{
545 struct fsi_stream *io;
546 int ret1, ret2;
547
548 io = &fsi->playback;
549 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
550
551 io = &fsi->capture;
552 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
553
554 if (ret1 < 0)
555 return ret1;
556 if (ret2 < 0)
557 return ret2;
558
559 return 0;
560}
561
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800562/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900563 * irq function
564 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900565
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800566static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900567{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800568 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900569 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900570
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900571 fsi_core_mask_set(master, imsk, data, data);
572 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900573}
574
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800575static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900576{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800577 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900578 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900579
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900580 fsi_core_mask_set(master, imsk, data, 0);
581 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900582}
583
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900584static u32 fsi_irq_get_status(struct fsi_master *master)
585{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900586 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900587}
588
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900589static void fsi_irq_clear_status(struct fsi_priv *fsi)
590{
591 u32 data = 0;
592 struct fsi_master *master = fsi_get_master(fsi);
593
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800594 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
595 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900596
597 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900598 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900599}
600
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900601/*
602 * SPDIF master clock function
603 *
604 * These functions are used later FSI2
605 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900606static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
607{
608 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900609 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900610
611 if (master->core->ver < 2) {
612 pr_err("fsi: register access err (%s)\n", __func__);
613 return;
614 }
615
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900616 mask = BP | SE;
617 val = enable ? mask : 0;
618
619 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900620 fsi_core_mask_set(master, a_mclk, mask, val) :
621 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900622}
623
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900624/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900625 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900626 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900627static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
628 long rate, int enable)
629{
630 struct fsi_master *master = fsi_get_master(fsi);
631 set_rate_func set_rate = fsi_get_info_set_rate(master);
632 int fsi_ver = master->core->ver;
633 int ret;
634
635 ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
636 if (ret < 0) /* error */
637 return ret;
638
639 if (!enable)
640 return 0;
641
642 if (ret > 0) {
643 u32 data = 0;
644
645 switch (ret & SH_FSI_ACKMD_MASK) {
646 default:
647 /* FALL THROUGH */
648 case SH_FSI_ACKMD_512:
649 data |= (0x0 << 12);
650 break;
651 case SH_FSI_ACKMD_256:
652 data |= (0x1 << 12);
653 break;
654 case SH_FSI_ACKMD_128:
655 data |= (0x2 << 12);
656 break;
657 case SH_FSI_ACKMD_64:
658 data |= (0x3 << 12);
659 break;
660 case SH_FSI_ACKMD_32:
661 if (fsi_ver < 2)
662 dev_err(dev, "unsupported ACKMD\n");
663 else
664 data |= (0x4 << 12);
665 break;
666 }
667
668 switch (ret & SH_FSI_BPFMD_MASK) {
669 default:
670 /* FALL THROUGH */
671 case SH_FSI_BPFMD_32:
672 data |= (0x0 << 8);
673 break;
674 case SH_FSI_BPFMD_64:
675 data |= (0x1 << 8);
676 break;
677 case SH_FSI_BPFMD_128:
678 data |= (0x2 << 8);
679 break;
680 case SH_FSI_BPFMD_256:
681 data |= (0x3 << 8);
682 break;
683 case SH_FSI_BPFMD_512:
684 data |= (0x4 << 8);
685 break;
686 case SH_FSI_BPFMD_16:
687 if (fsi_ver < 2)
688 dev_err(dev, "unsupported ACKMD\n");
689 else
690 data |= (0x7 << 8);
691 break;
692 }
693
694 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
695 udelay(10);
696 ret = 0;
697 }
698
699 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900700}
701
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900702/*
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800703 * pio data transfer handler
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900704 */
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800705static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
706{
707 u16 *buf = (u16 *)_buf;
708 int i;
709
710 for (i = 0; i < samples; i++)
711 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
712}
713
714static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
715{
716 u16 *buf = (u16 *)_buf;
717 int i;
718
719 for (i = 0; i < samples; i++)
720 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
721}
722
723static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
724{
725 u32 *buf = (u32 *)_buf;
726 int i;
727
728 for (i = 0; i < samples; i++)
729 fsi_reg_write(fsi, DODT, *(buf + i));
730}
731
732static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
733{
734 u32 *buf = (u32 *)_buf;
735 int i;
736
737 for (i = 0; i < samples; i++)
738 *(buf + i) = fsi_reg_read(fsi, DIDT);
739}
740
741static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
742{
743 struct snd_pcm_runtime *runtime = io->substream->runtime;
744
745 return runtime->dma_area +
746 samples_to_bytes(runtime, io->buff_sample_pos);
747}
748
749static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800750 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
751 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
752 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900753{
754 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800755 struct snd_pcm_substream *substream;
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800756 u8 *buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900757 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900758
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800759 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900760 return -EINVAL;
761
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900762 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900763 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900764 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900765
766 /* FSI FIFO has limit.
767 * So, this driver can not send periods data at a time
768 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900769 if (io->buff_sample_pos >=
770 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900771
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900772 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900773 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900774
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900775 if (0 == io->period_pos)
776 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900777 }
778
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800779 buf = fsi_pio_get_area(fsi, io);
780
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800781 switch (io->sample_width) {
782 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800783 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800784 break;
785 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800786 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800787 break;
788 default:
789 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900790 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900791
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900792 /* update buff_sample_pos */
793 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900794
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900795 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900796 snd_pcm_period_elapsed(substream);
797
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900798 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900799}
800
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800801static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900802{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800803 int sample_residues; /* samples in FSI fifo */
804 int sample_space; /* ALSA free samples space */
805 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800806
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800807 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800808 sample_space = io->buff_sample_capa - io->buff_sample_pos;
809
810 samples = min(sample_residues, sample_space);
811
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800812 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800813 fsi_pio_pop16,
814 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800815 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900816}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900817
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800818static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900819{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800820 int sample_residues; /* ALSA residue samples */
821 int sample_space; /* FSI fifo free samples space */
822 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800823
824 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
825 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800826 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800827
828 samples = min(sample_residues, sample_space);
829
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800830 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800831 fsi_pio_push16,
832 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800833 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900834}
835
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800836static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
837 int enable)
838{
839 struct fsi_master *master = fsi_get_master(fsi);
840 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
841
842 if (enable)
843 fsi_irq_enable(fsi, io);
844 else
845 fsi_irq_disable(fsi, io);
846
847 if (fsi_is_clk_master(fsi))
848 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
849}
850
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800851static struct fsi_stream_handler fsi_pio_push_handler = {
852 .transfer = fsi_pio_push,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800853 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800854};
855
856static struct fsi_stream_handler fsi_pio_pop_handler = {
857 .transfer = fsi_pio_pop,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800858 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800859};
860
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861static irqreturn_t fsi_interrupt(int irq, void *data)
862{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900863 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900864 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900865
866 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900867 fsi_master_mask_set(master, SOFT_RST, IR, 0);
868 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900869
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900870 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800871 fsi_stream_transfer(&master->fsia.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900872 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800873 fsi_stream_transfer(&master->fsib.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900874 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800875 fsi_stream_transfer(&master->fsia.capture);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900876 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800877 fsi_stream_transfer(&master->fsib.capture);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900878
879 fsi_count_fifo_err(&master->fsia);
880 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900881
Kuninori Morimoto48d78e52010-12-03 17:37:31 +0900882 fsi_irq_clear_status(&master->fsia);
883 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900884
885 return IRQ_HANDLED;
886}
887
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900888/*
889 * dai ops
890 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -0800891static void fsi_fifo_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800892 struct fsi_stream *io,
Kuninori Morimotob49e8022012-02-03 00:51:29 -0800893 struct device *dev)
894{
895 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800896 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -0800897 u32 shift, i;
898 int frame_capa;
899
900 /* get on-chip RAM capacity */
901 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800902 shift >>= fsi_get_port_shift(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -0800903 shift &= FIFO_SZ_MASK;
904 frame_capa = 256 << shift;
905 dev_dbg(dev, "fifo = %d words\n", frame_capa);
906
907 /*
908 * The maximum number of sample data varies depending
909 * on the number of channels selected for the format.
910 *
911 * FIFOs are used in 4-channel units in 3-channel mode
912 * and in 8-channel units in 5- to 7-channel mode
913 * meaning that more FIFOs than the required size of DPRAM
914 * are used.
915 *
916 * ex) if 256 words of DP-RAM is connected
917 * 1 channel: 256 (256 x 1 = 256)
918 * 2 channels: 128 (128 x 2 = 256)
919 * 3 channels: 64 ( 64 x 3 = 192)
920 * 4 channels: 64 ( 64 x 4 = 256)
921 * 5 channels: 32 ( 32 x 5 = 160)
922 * 6 channels: 32 ( 32 x 6 = 192)
923 * 7 channels: 32 ( 32 x 7 = 224)
924 * 8 channels: 32 ( 32 x 8 = 256)
925 */
926 for (i = 1; i < fsi->chan_num; i <<= 1)
927 frame_capa >>= 1;
928 dev_dbg(dev, "%d channel %d store\n",
929 fsi->chan_num, frame_capa);
930
931 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
932
933 /*
934 * set interrupt generation factor
935 * clear FIFO
936 */
937 if (is_play) {
938 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
939 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
940 } else {
941 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
942 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
943 }
944}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900945
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900946static int fsi_hw_startup(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800947 struct fsi_stream *io,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +0900948 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900949{
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800950 struct fsi_master *master = fsi_get_master(fsi);
951 int fsi_ver = master->core->ver;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900952 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900953 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900954
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900955 /* clock setting */
956 if (fsi_is_clk_master(fsi))
957 data = DIMD | DOMD;
958
959 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900960
961 /* clock inversion (CKG2) */
962 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900963 if (SH_FSI_LRM_INV & flags)
964 data |= 1 << 12;
965 if (SH_FSI_BRM_INV & flags)
966 data |= 1 << 8;
967 if (SH_FSI_LRS_INV & flags)
968 data |= 1 << 4;
969 if (SH_FSI_BRS_INV & flags)
970 data |= 1 << 0;
971
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900972 fsi_reg_write(fsi, CKG2, data);
973
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900974 /* set format */
975 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
976 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
977
978 /* spdif ? */
979 if (fsi_is_spdif(fsi)) {
980 fsi_spdif_clk_ctrl(fsi, 1);
981 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
982 }
983
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -0800984 /*
985 * FIXME
986 *
987 * FSI driver assumed that data package is in-back.
988 * FSI2 chip can select it.
989 */
990 if (fsi_ver >= 2) {
991 fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
992 fsi_reg_write(fsi, IN_DMAC, (1 << 4));
993 }
994
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900995 /* irq clear */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800996 fsi_irq_disable(fsi, io);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900997 fsi_irq_clear_status(fsi);
998
999 /* fifo init */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001000 fsi_fifo_init(fsi, io, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001001
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +09001002 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001003}
1004
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001005static void fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001006 struct device *dev)
1007{
1008 if (fsi_is_clk_master(fsi))
1009 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001010}
1011
1012static int fsi_dai_startup(struct snd_pcm_substream *substream,
1013 struct snd_soc_dai *dai)
1014{
1015 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001016
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001017 return fsi_hw_startup(fsi, fsi_stream_get(fsi, substream), dai->dev);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001018}
1019
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001020static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1021 struct snd_soc_dai *dai)
1022{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001023 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001024
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001025 fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001026 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001027}
1028
1029static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1030 struct snd_soc_dai *dai)
1031{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001032 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001033 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001034 int ret = 0;
1035
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001036 switch (cmd) {
1037 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001038 fsi_stream_init(fsi, io, substream);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001039 ret = fsi_stream_transfer(io);
1040 if (0 == ret)
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001041 fsi_stream_start(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001042 break;
1043 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001044 fsi_stream_stop(fsi, io);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001045 fsi_stream_quit(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001046 break;
1047 }
1048
1049 return ret;
1050}
1051
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001052static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1053{
1054 u32 data = 0;
1055
1056 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1057 case SND_SOC_DAIFMT_I2S:
1058 data = CR_I2S;
1059 fsi->chan_num = 2;
1060 break;
1061 case SND_SOC_DAIFMT_LEFT_J:
1062 data = CR_PCM;
1063 fsi->chan_num = 2;
1064 break;
1065 default:
1066 return -EINVAL;
1067 }
1068
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001069 fsi->do_fmt = data;
1070 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001071
1072 return 0;
1073}
1074
1075static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1076{
1077 struct fsi_master *master = fsi_get_master(fsi);
1078 u32 data = 0;
1079
1080 if (master->core->ver < 2)
1081 return -EINVAL;
1082
1083 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1084 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001085 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001086
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001087 fsi->do_fmt = data;
1088 fsi->di_fmt = data;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001089
1090 return 0;
1091}
1092
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001093static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1094{
1095 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001096 struct fsi_master *master = fsi_get_master(fsi);
1097 set_rate_func set_rate = fsi_get_info_set_rate(master);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001098 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001099 int ret;
1100
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001101 /* set master/slave audio interface */
1102 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1103 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001104 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001105 break;
1106 case SND_SOC_DAIFMT_CBS_CFS:
1107 break;
1108 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001109 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001110 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001111
1112 if (fsi_is_clk_master(fsi) && !set_rate) {
1113 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001114 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001115 }
1116
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001117 /* set format */
1118 switch (flags & SH_FSI_FMT_MASK) {
1119 case SH_FSI_FMT_DAI:
1120 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1121 break;
1122 case SH_FSI_FMT_SPDIF:
1123 ret = fsi_set_fmt_spdif(fsi);
1124 break;
1125 default:
1126 ret = -EINVAL;
1127 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001128
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001129 return ret;
1130}
1131
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001132static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1133 struct snd_pcm_hw_params *params,
1134 struct snd_soc_dai *dai)
1135{
1136 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001137 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001138 int ret;
1139
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001140 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001141 return 0;
1142
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001143 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1144 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001145 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001146
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001147 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001148
1149 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001150}
1151
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001152static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001153 .startup = fsi_dai_startup,
1154 .shutdown = fsi_dai_shutdown,
1155 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001156 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001157 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001158};
1159
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001160/*
1161 * pcm ops
1162 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001163
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001164static struct snd_pcm_hardware fsi_pcm_hardware = {
1165 .info = SNDRV_PCM_INFO_INTERLEAVED |
1166 SNDRV_PCM_INFO_MMAP |
1167 SNDRV_PCM_INFO_MMAP_VALID |
1168 SNDRV_PCM_INFO_PAUSE,
1169 .formats = FSI_FMTS,
1170 .rates = FSI_RATES,
1171 .rate_min = 8000,
1172 .rate_max = 192000,
1173 .channels_min = 1,
1174 .channels_max = 2,
1175 .buffer_bytes_max = 64 * 1024,
1176 .period_bytes_min = 32,
1177 .period_bytes_max = 8192,
1178 .periods_min = 1,
1179 .periods_max = 32,
1180 .fifo_size = 256,
1181};
1182
1183static int fsi_pcm_open(struct snd_pcm_substream *substream)
1184{
1185 struct snd_pcm_runtime *runtime = substream->runtime;
1186 int ret = 0;
1187
1188 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1189
1190 ret = snd_pcm_hw_constraint_integer(runtime,
1191 SNDRV_PCM_HW_PARAM_PERIODS);
1192
1193 return ret;
1194}
1195
1196static int fsi_hw_params(struct snd_pcm_substream *substream,
1197 struct snd_pcm_hw_params *hw_params)
1198{
1199 return snd_pcm_lib_malloc_pages(substream,
1200 params_buffer_bytes(hw_params));
1201}
1202
1203static int fsi_hw_free(struct snd_pcm_substream *substream)
1204{
1205 return snd_pcm_lib_free_pages(substream);
1206}
1207
1208static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1209{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001210 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001211 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001212 int samples_pos = io->buff_sample_pos - 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001213
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001214 if (samples_pos < 0)
1215 samples_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001216
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +09001217 return fsi_sample2frame(fsi, samples_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001218}
1219
1220static struct snd_pcm_ops fsi_pcm_ops = {
1221 .open = fsi_pcm_open,
1222 .ioctl = snd_pcm_lib_ioctl,
1223 .hw_params = fsi_hw_params,
1224 .hw_free = fsi_hw_free,
1225 .pointer = fsi_pointer,
1226};
1227
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001228/*
1229 * snd_soc_platform
1230 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001231
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001232#define PREALLOC_BUFFER (32 * 1024)
1233#define PREALLOC_BUFFER_MAX (32 * 1024)
1234
1235static void fsi_pcm_free(struct snd_pcm *pcm)
1236{
1237 snd_pcm_lib_preallocate_free_for_all(pcm);
1238}
1239
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001240static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001241{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001242 struct snd_pcm *pcm = rtd->pcm;
1243
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001244 /*
1245 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1246 * in MMAP mode (i.e. aplay -M)
1247 */
1248 return snd_pcm_lib_preallocate_pages_for_all(
1249 pcm,
1250 SNDRV_DMA_TYPE_CONTINUOUS,
1251 snd_dma_continuous_data(GFP_KERNEL),
1252 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1253}
1254
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001255/*
1256 * alsa struct
1257 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001258
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001259static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001260 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001261 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001262 .playback = {
1263 .rates = FSI_RATES,
1264 .formats = FSI_FMTS,
1265 .channels_min = 1,
1266 .channels_max = 8,
1267 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001268 .capture = {
1269 .rates = FSI_RATES,
1270 .formats = FSI_FMTS,
1271 .channels_min = 1,
1272 .channels_max = 8,
1273 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001274 .ops = &fsi_dai_ops,
1275 },
1276 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001278 .playback = {
1279 .rates = FSI_RATES,
1280 .formats = FSI_FMTS,
1281 .channels_min = 1,
1282 .channels_max = 8,
1283 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001284 .capture = {
1285 .rates = FSI_RATES,
1286 .formats = FSI_FMTS,
1287 .channels_min = 1,
1288 .channels_max = 8,
1289 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001290 .ops = &fsi_dai_ops,
1291 },
1292};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001293
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001294static struct snd_soc_platform_driver fsi_soc_platform = {
1295 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001296 .pcm_new = fsi_pcm_new,
1297 .pcm_free = fsi_pcm_free,
1298};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001299
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001300/*
1301 * platform function
1302 */
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001303static void fsi_handler_init(struct fsi_priv *fsi)
1304{
1305 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1306 fsi->playback.priv = fsi;
1307 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1308 fsi->capture.priv = fsi;
1309}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001310
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001311static int fsi_probe(struct platform_device *pdev)
1312{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001313 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001314 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001315 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001316 unsigned int irq;
1317 int ret;
1318
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001319 id_entry = pdev->id_entry;
1320 if (!id_entry) {
1321 dev_err(&pdev->dev, "unknown fsi device\n");
1322 return -ENODEV;
1323 }
1324
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001325 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1326 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001327 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001328 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1329 ret = -ENODEV;
1330 goto exit;
1331 }
1332
1333 master = kzalloc(sizeof(*master), GFP_KERNEL);
1334 if (!master) {
1335 dev_err(&pdev->dev, "Could not allocate master\n");
1336 ret = -ENOMEM;
1337 goto exit;
1338 }
1339
1340 master->base = ioremap_nocache(res->start, resource_size(res));
1341 if (!master->base) {
1342 ret = -ENXIO;
1343 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1344 goto exit_kfree;
1345 }
1346
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001347 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001348 master->irq = irq;
1349 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001350 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001351 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001352
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001353 /* FSI A setting */
1354 master->fsia.base = master->base;
1355 master->fsia.master = master;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001356 fsi_handler_init(&master->fsia);
1357 ret = fsi_stream_probe(&master->fsia);
1358 if (ret < 0) {
1359 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1360 goto exit_iounmap;
1361 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001362
1363 /* FSI B setting */
1364 master->fsib.base = master->base + 0x40;
1365 master->fsib.master = master;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001366 fsi_handler_init(&master->fsib);
1367 ret = fsi_stream_probe(&master->fsib);
1368 if (ret < 0) {
1369 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1370 goto exit_fsia;
1371 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001372
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001373 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001374 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001375
Yong Zhang88e24c32011-09-22 16:59:20 +08001376 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001377 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001378 if (ret) {
1379 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001380 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001381 }
1382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001383 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001384 if (ret < 0) {
1385 dev_err(&pdev->dev, "cannot snd soc register\n");
1386 goto exit_free_irq;
1387 }
1388
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001389 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1390 ARRAY_SIZE(fsi_soc_dai));
1391 if (ret < 0) {
1392 dev_err(&pdev->dev, "cannot snd dai register\n");
1393 goto exit_snd_soc;
1394 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001395
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001396 return ret;
1397
1398exit_snd_soc:
1399 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001400exit_free_irq:
1401 free_irq(irq, master);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001402exit_fsib:
1403 fsi_stream_remove(&master->fsib);
1404exit_fsia:
1405 fsi_stream_remove(&master->fsia);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001406exit_iounmap:
1407 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001408 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001409exit_kfree:
1410 kfree(master);
1411 master = NULL;
1412exit:
1413 return ret;
1414}
1415
1416static int fsi_remove(struct platform_device *pdev)
1417{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001418 struct fsi_master *master;
1419
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001420 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001421
Kuninori Morimotod985f272011-04-08 15:09:25 +09001422 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001423 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001424
Kuninori Morimotod985f272011-04-08 15:09:25 +09001425 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1426 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001427
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001428 fsi_stream_remove(&master->fsia);
1429 fsi_stream_remove(&master->fsib);
1430
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001431 iounmap(master->base);
1432 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001433
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001434 return 0;
1435}
1436
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001437static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001438 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001439 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001440{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001441 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001442 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001443
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001444 fsi_stream_stop(fsi, io);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001445 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001446}
1447
1448static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001449 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001450 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001451{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001452 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001453 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001454
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001455 fsi_hw_startup(fsi, io, dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001456
1457 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001458 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001459
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001460 fsi_stream_start(fsi, io);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001461}
1462
1463static int fsi_suspend(struct device *dev)
1464{
1465 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001466 struct fsi_priv *fsia = &master->fsia;
1467 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001468
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001469 __fsi_suspend(fsia, &fsia->playback, dev);
1470 __fsi_suspend(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001471
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001472 __fsi_suspend(fsib, &fsib->playback, dev);
1473 __fsi_suspend(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001474
1475 return 0;
1476}
1477
1478static int fsi_resume(struct device *dev)
1479{
1480 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001481 struct fsi_priv *fsia = &master->fsia;
1482 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001483
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001484 __fsi_resume(fsia, &fsia->playback, dev);
1485 __fsi_resume(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001486
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001487 __fsi_resume(fsib, &fsib->playback, dev);
1488 __fsi_resume(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001489
1490 return 0;
1491}
1492
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001493static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001494 .suspend = fsi_suspend,
1495 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001496};
1497
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001498static struct fsi_core fsi1_core = {
1499 .ver = 1,
1500
1501 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001502 .int_st = INT_ST,
1503 .iemsk = IEMSK,
1504 .imsk = IMSK,
1505};
1506
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001507static struct fsi_core fsi2_core = {
1508 .ver = 2,
1509
1510 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001511 .int_st = CPU_INT_ST,
1512 .iemsk = CPU_IEMSK,
1513 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001514 .a_mclk = A_MST_CTLR,
1515 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001516};
1517
1518static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001519 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1520 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001521 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001522};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001523MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001524
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001525static struct platform_driver fsi_driver = {
1526 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001528 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001529 },
1530 .probe = fsi_probe,
1531 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001532 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001533};
1534
Axel Lincb5e8732011-11-25 10:15:07 +08001535module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001536
1537MODULE_LICENSE("GPL");
1538MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1539MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001540MODULE_ALIAS("platform:fsi-pcm-audio");