blob: 9d7f30774a44d9524cb71f6697fc0ce96825295c [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 Morimoto7da9ced2012-02-03 00:59:33 -080016#include <linux/dma-mapping.h>
Kuninori Morimoto785d1c42009-11-30 20:24:48 +090017#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090018#include <linux/io.h>
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080019#include <linux/scatterlist.h>
20#include <linux/sh_dma.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040022#include <linux/module.h>
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +020023#include <linux/workqueue.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090024#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090025#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090026
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090027/* PortA/PortB register */
28#define REG_DO_FMT 0x0000
29#define REG_DOFF_CTL 0x0004
30#define REG_DOFF_ST 0x0008
31#define REG_DI_FMT 0x000C
32#define REG_DIFF_CTL 0x0010
33#define REG_DIFF_ST 0x0014
34#define REG_CKG1 0x0018
35#define REG_CKG2 0x001C
36#define REG_DIDT 0x0020
37#define REG_DODT 0x0024
38#define REG_MUTE_ST 0x0028
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080039#define REG_OUT_DMAC 0x002C
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090040#define REG_OUT_SEL 0x0030
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080041#define REG_IN_DMAC 0x0038
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090042
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090043/* master register */
44#define MST_CLK_RST 0x0210
45#define MST_SOFT_RST 0x0214
46#define MST_FIFO_SZ 0x0218
47
48/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090049#define A_MST_CTLR 0x0180
50#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090051#define CPU_INT_ST 0x01F4
52#define CPU_IEMSK 0x01F8
53#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090054#define INT_ST 0x0200
55#define IEMSK 0x0204
56#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090057
58/* DO_FMT */
59/* DI_FMT */
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080060#define CR_BWS_MASK (0x3 << 20) /* FSI2 */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090061#define CR_BWS_24 (0x0 << 20) /* FSI2 */
62#define CR_BWS_16 (0x1 << 20) /* FSI2 */
63#define CR_BWS_20 (0x2 << 20) /* FSI2 */
64
65#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
66#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
67#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
68
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090069#define CR_MONO (0x0 << 4)
70#define CR_MONO_D (0x1 << 4)
71#define CR_PCM (0x2 << 4)
72#define CR_I2S (0x3 << 4)
73#define CR_TDM (0x4 << 4)
74#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090075
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080076/* OUT_DMAC */
77/* IN_DMAC */
78#define VDMD_MASK (0x3 << 4)
79#define VDMD_FRONT (0x0 << 4) /* Package in front */
80#define VDMD_BACK (0x1 << 4) /* Package in back */
81#define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
82
83#define DMA_ON (0x1 << 0)
84
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090085/* DOFF_CTL */
86/* DIFF_CTL */
87#define IRQ_HALF 0x00100000
88#define FIFO_CLR 0x00000001
89
90/* DOFF_ST */
91#define ERR_OVER 0x00000010
92#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090093#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090094
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090095/* CKG1 */
96#define ACKMD_MASK 0x00007000
97#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090098#define DIMD (1 << 4)
99#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900100
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900101/* A/B MST_CTLR */
102#define BP (1 << 4) /* Fix the signal of Biphase output */
103#define SE (1 << 0) /* Fix the master clock */
104
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900105/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900106#define CRB (1 << 4)
107#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900108
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900109/* IO SHIFT / MACRO */
110#define BI_SHIFT 12
111#define BO_SHIFT 8
112#define AI_SHIFT 4
113#define AO_SHIFT 0
114#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900115
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900116/* SOFT_RST */
117#define PBSR (1 << 12) /* Port B Software Reset */
118#define PASR (1 << 8) /* Port A Software Reset */
119#define IR (1 << 4) /* Interrupt Reset */
120#define FSISR (1 << 0) /* Software Reset */
121
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900122/* OUT_SEL (FSI2) */
123#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
124 /* 1: Biphase and serial */
125
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900126/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900127#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900128
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900129#define FSI_RATES SNDRV_PCM_RATE_8000_96000
130
131#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
132
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800133typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900134
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900135/*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700136 * bus options
137 *
138 * 0x000000BA
139 *
140 * A : sample widtht 16bit setting
141 * B : sample widtht 24bit setting
142 */
143
144#define SHIFT_16DATA 0
145#define SHIFT_24DATA 4
146
147#define PACKAGE_24BITBUS_BACK 0
148#define PACKAGE_24BITBUS_FRONT 1
149#define PACKAGE_16BITBUS_STREAM 2
150
151#define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
152#define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
153
154/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900155 * FSI driver use below type name for variable
156 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900157 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900158 * xxx_pos : position of data
159 * xxx_capa : capacity of data
160 */
161
162/*
163 * period/frame/sample image
164 *
165 * ex) PCM (2ch)
166 *
167 * period pos period pos
168 * [n] [n + 1]
169 * |<-------------------- period--------------------->|
170 * ==|============================================ ... =|==
171 * | |
172 * ||<----- frame ----->|<------ frame ----->| ... |
173 * |+--------------------+--------------------+- ... |
174 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
175 * |+--------------------+--------------------+- ... |
176 * ==|============================================ ... =|==
177 */
178
179/*
180 * FSI FIFO image
181 *
182 * | |
183 * | |
184 * | [ sample ] |
185 * | [ sample ] |
186 * | [ sample ] |
187 * | [ sample ] |
188 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900189 */
190
191/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900192 * struct
193 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900194
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800195struct fsi_stream_handler;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900196struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900197
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800198 /*
199 * these are initialized by fsi_stream_init()
200 */
201 struct snd_pcm_substream *substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900202 int fifo_sample_capa; /* sample capacity of FSI FIFO */
203 int buff_sample_capa; /* sample capacity of ALSA buffer */
204 int buff_sample_pos; /* sample position of ALSA buffer */
205 int period_samples; /* sample number / 1 period */
206 int period_pos; /* current period position */
Kuninori Morimotoc1e6f102012-02-03 00:50:09 -0800207 int sample_width; /* sample width */
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900208 int uerr_num;
209 int oerr_num;
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800210
211 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700212 * bus options
213 */
214 u32 bus_option;
215
216 /*
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800217 * thse are initialized by fsi_handler_init()
218 */
219 struct fsi_stream_handler *handler;
220 struct fsi_priv *priv;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800221
222 /*
223 * these are for DMAEngine
224 */
225 struct dma_chan *chan;
226 struct sh_dmae_slave slave; /* see fsi_handler_init() */
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +0200227 struct work_struct work;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800228 dma_addr_t dma;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900229};
230
231struct fsi_priv {
232 void __iomem *base;
233 struct fsi_master *master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800234 struct sh_fsi_port_info *info;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900235
236 struct fsi_stream playback;
237 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900238
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -0700239 u32 fmt;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900240
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900241 int chan_num:16;
242 int clk_master:1;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900243 int spdif:1;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900244
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +0000245 long rate;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900246};
247
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800248struct fsi_stream_handler {
Kuninori Morimoto83344022012-02-03 00:59:02 -0800249 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
250 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700251 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800252 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
253 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800254 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
255 int enable);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800256};
257#define fsi_stream_handler_call(io, func, args...) \
258 (!(io) ? -ENODEV : \
259 !((io)->handler->func) ? 0 : \
260 (io)->handler->func(args))
261
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900262struct fsi_core {
263 int ver;
264
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900265 u32 int_st;
266 u32 iemsk;
267 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900268 u32 a_mclk;
269 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900270};
271
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900272struct fsi_master {
273 void __iomem *base;
274 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900275 struct fsi_priv fsia;
276 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900277 struct fsi_core *core;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900278 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900279};
280
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800281static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
282
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900283/*
284 * basic read write function
285 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900286
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200287static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900288{
289 /* valid data area is 24bit */
290 data &= 0x00ffffff;
291
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100292 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900293}
294
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200295static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900296{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100297 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900298}
299
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200300static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900301{
302 u32 val = __fsi_reg_read(reg);
303
304 val &= ~mask;
305 val |= data & mask;
306
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100307 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900308}
309
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900310#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700311 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900312
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900313#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700314 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900315
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900316#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700317 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900318
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900319#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
320#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
321static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900322{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900323 u32 ret;
324 unsigned long flags;
325
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900326 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200327 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900328 spin_unlock_irqrestore(&master->lock, flags);
329
330 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900331}
332
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900333#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
334#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
335static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900336 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900337{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900338 unsigned long flags;
339
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900340 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200341 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900342 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900343}
344
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900345/*
346 * basic function
347 */
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700348static int fsi_version(struct fsi_master *master)
349{
350 return master->core->ver;
351}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900352
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900353static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900354{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900355 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900356}
357
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900358static int fsi_is_clk_master(struct fsi_priv *fsi)
359{
360 return fsi->clk_master;
361}
362
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900363static int fsi_is_port_a(struct fsi_priv *fsi)
364{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900365 return fsi->master->base == fsi->base;
366}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900367
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900368static int fsi_is_spdif(struct fsi_priv *fsi)
369{
370 return fsi->spdif;
371}
372
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800373static int fsi_is_play(struct snd_pcm_substream *substream)
374{
375 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
376}
377
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900378static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900379{
380 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900381
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000382 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900383}
384
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900385static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900386{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000387 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900388
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000389 if (dai->id == 0)
390 return &master->fsia;
391 else
392 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900393}
394
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900395static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
396{
397 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
398}
399
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800400static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900401{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800402 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900403 return NULL;
404
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800405 return fsi->info->set_rate;
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900406}
407
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900408static u32 fsi_get_info_flags(struct fsi_priv *fsi)
409{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800410 if (!fsi->info)
Kuninori Morimotod7c57622011-01-24 10:42:33 +0900411 return 0;
412
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800413 return fsi->info->flags;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900414}
415
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800416static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900417{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800418 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900419 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900420 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900421
422 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900423 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900424 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900425 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900426
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900427 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900428}
429
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900430static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
431{
432 return frames * fsi->chan_num;
433}
434
435static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
436{
437 return samples / fsi->chan_num;
438}
439
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800440static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
441 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900442{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800443 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900444 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900445 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900446
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900447 status = is_play ?
448 fsi_reg_read(fsi, DOFF_ST) :
449 fsi_reg_read(fsi, DIFF_ST);
450
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900451 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900452
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900453 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900454}
455
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +0900456static void fsi_count_fifo_err(struct fsi_priv *fsi)
457{
458 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
459 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
460
461 if (ostatus & ERR_OVER)
462 fsi->playback.oerr_num++;
463
464 if (ostatus & ERR_UNDER)
465 fsi->playback.uerr_num++;
466
467 if (istatus & ERR_OVER)
468 fsi->capture.oerr_num++;
469
470 if (istatus & ERR_UNDER)
471 fsi->capture.uerr_num++;
472
473 fsi_reg_write(fsi, DOFF_ST, 0);
474 fsi_reg_write(fsi, DIFF_ST, 0);
475}
476
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900477/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800478 * fsi_stream_xx() function
479 */
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800480static inline int fsi_stream_is_play(struct fsi_priv *fsi,
481 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800482{
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800483 return &fsi->playback == io;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800484}
485
486static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800487 struct snd_pcm_substream *substream)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800488{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800489 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800490}
491
492static int fsi_stream_is_working(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800493 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800494{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800495 struct fsi_master *master = fsi_get_master(fsi);
496 unsigned long flags;
497 int ret;
498
499 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800500 ret = !!(io->substream && io->substream->runtime);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800501 spin_unlock_irqrestore(&master->lock, flags);
502
503 return ret;
504}
505
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800506static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
507{
508 return io->priv;
509}
510
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800511static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800512 struct fsi_stream *io,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800513 struct snd_pcm_substream *substream)
514{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800515 struct snd_pcm_runtime *runtime = substream->runtime;
516 struct fsi_master *master = fsi_get_master(fsi);
517 unsigned long flags;
518
519 spin_lock_irqsave(&master->lock, flags);
520 io->substream = substream;
521 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
522 io->buff_sample_pos = 0;
523 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
524 io->period_pos = 0;
525 io->sample_width = samples_to_bytes(runtime, 1);
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700526 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800527 io->oerr_num = -1; /* ignore 1st err */
528 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimoto83344022012-02-03 00:59:02 -0800529 fsi_stream_handler_call(io, init, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800530 spin_unlock_irqrestore(&master->lock, flags);
531}
532
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800533static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800534{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800535 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
536 struct fsi_master *master = fsi_get_master(fsi);
537 unsigned long flags;
538
539 spin_lock_irqsave(&master->lock, flags);
540
541 if (io->oerr_num > 0)
542 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
543
544 if (io->uerr_num > 0)
545 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
546
Kuninori Morimoto83344022012-02-03 00:59:02 -0800547 fsi_stream_handler_call(io, quit, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800548 io->substream = NULL;
549 io->buff_sample_capa = 0;
550 io->buff_sample_pos = 0;
551 io->period_samples = 0;
552 io->period_pos = 0;
553 io->sample_width = 0;
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700554 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800555 io->oerr_num = 0;
556 io->uerr_num = 0;
557 spin_unlock_irqrestore(&master->lock, flags);
558}
559
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800560static int fsi_stream_transfer(struct fsi_stream *io)
561{
562 struct fsi_priv *fsi = fsi_stream_to_priv(io);
563 if (!fsi)
564 return -EIO;
565
566 return fsi_stream_handler_call(io, transfer, fsi, io);
567}
568
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800569#define fsi_stream_start(fsi, io)\
570 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
571
572#define fsi_stream_stop(fsi, io)\
573 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
574
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700575static int fsi_stream_probe(struct fsi_priv *fsi, struct device *dev)
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800576{
577 struct fsi_stream *io;
578 int ret1, ret2;
579
580 io = &fsi->playback;
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700581 ret1 = fsi_stream_handler_call(io, probe, fsi, io, dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800582
583 io = &fsi->capture;
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700584 ret2 = fsi_stream_handler_call(io, probe, fsi, io, dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800585
586 if (ret1 < 0)
587 return ret1;
588 if (ret2 < 0)
589 return ret2;
590
591 return 0;
592}
593
594static int fsi_stream_remove(struct fsi_priv *fsi)
595{
596 struct fsi_stream *io;
597 int ret1, ret2;
598
599 io = &fsi->playback;
600 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
601
602 io = &fsi->capture;
603 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
604
605 if (ret1 < 0)
606 return ret1;
607 if (ret2 < 0)
608 return ret2;
609
610 return 0;
611}
612
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800613/*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700614 * format/bus/dma setting
615 */
616static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
617 u32 bus, struct device *dev)
618{
619 struct fsi_master *master = fsi_get_master(fsi);
620 int is_play = fsi_stream_is_play(fsi, io);
621 u32 fmt = fsi->fmt;
622
623 if (fsi_version(master) >= 2) {
624 u32 dma = 0;
625
626 /*
627 * FSI2 needs DMA/Bus setting
628 */
629 switch (bus) {
630 case PACKAGE_24BITBUS_FRONT:
631 fmt |= CR_BWS_24;
632 dma |= VDMD_FRONT;
633 dev_dbg(dev, "24bit bus / package in front\n");
634 break;
635 case PACKAGE_16BITBUS_STREAM:
636 fmt |= CR_BWS_16;
637 dma |= VDMD_STREAM;
638 dev_dbg(dev, "16bit bus / stream mode\n");
639 break;
640 case PACKAGE_24BITBUS_BACK:
641 default:
642 fmt |= CR_BWS_24;
643 dma |= VDMD_BACK;
644 dev_dbg(dev, "24bit bus / package in back\n");
645 break;
646 }
647
648 if (is_play)
649 fsi_reg_write(fsi, OUT_DMAC, dma);
650 else
651 fsi_reg_write(fsi, IN_DMAC, dma);
652 }
653
654 if (is_play)
655 fsi_reg_write(fsi, DO_FMT, fmt);
656 else
657 fsi_reg_write(fsi, DI_FMT, fmt);
658}
659
660/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900661 * irq function
662 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900663
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800664static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900665{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800666 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900667 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900668
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900669 fsi_core_mask_set(master, imsk, data, data);
670 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900671}
672
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800673static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900674{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800675 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900676 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900677
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900678 fsi_core_mask_set(master, imsk, data, 0);
679 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900680}
681
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900682static u32 fsi_irq_get_status(struct fsi_master *master)
683{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900684 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900685}
686
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900687static void fsi_irq_clear_status(struct fsi_priv *fsi)
688{
689 u32 data = 0;
690 struct fsi_master *master = fsi_get_master(fsi);
691
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800692 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
693 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900694
695 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900696 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900697}
698
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900699/*
700 * SPDIF master clock function
701 *
702 * These functions are used later FSI2
703 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900704static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
705{
706 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900707 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900708
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900709 mask = BP | SE;
710 val = enable ? mask : 0;
711
712 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900713 fsi_core_mask_set(master, a_mclk, mask, val) :
714 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900715}
716
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900717/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900718 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900719 */
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900720static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
721 long rate, int enable)
722{
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800723 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900724 int ret;
725
Kuninori Morimotofec691e2012-02-03 00:58:48 -0800726 if (!set_rate)
727 return 0;
728
729 ret = set_rate(dev, rate, enable);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900730 if (ret < 0) /* error */
731 return ret;
732
733 if (!enable)
734 return 0;
735
736 if (ret > 0) {
737 u32 data = 0;
738
739 switch (ret & SH_FSI_ACKMD_MASK) {
740 default:
741 /* FALL THROUGH */
742 case SH_FSI_ACKMD_512:
743 data |= (0x0 << 12);
744 break;
745 case SH_FSI_ACKMD_256:
746 data |= (0x1 << 12);
747 break;
748 case SH_FSI_ACKMD_128:
749 data |= (0x2 << 12);
750 break;
751 case SH_FSI_ACKMD_64:
752 data |= (0x3 << 12);
753 break;
754 case SH_FSI_ACKMD_32:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700755 data |= (0x4 << 12);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900756 break;
757 }
758
759 switch (ret & SH_FSI_BPFMD_MASK) {
760 default:
761 /* FALL THROUGH */
762 case SH_FSI_BPFMD_32:
763 data |= (0x0 << 8);
764 break;
765 case SH_FSI_BPFMD_64:
766 data |= (0x1 << 8);
767 break;
768 case SH_FSI_BPFMD_128:
769 data |= (0x2 << 8);
770 break;
771 case SH_FSI_BPFMD_256:
772 data |= (0x3 << 8);
773 break;
774 case SH_FSI_BPFMD_512:
775 data |= (0x4 << 8);
776 break;
777 case SH_FSI_BPFMD_16:
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700778 data |= (0x7 << 8);
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900779 break;
780 }
781
782 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
783 udelay(10);
784 ret = 0;
785 }
786
787 return ret;
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +0900788}
789
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900790/*
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800791 * pio data transfer handler
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900792 */
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800793static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
794{
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700795 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800796 int i;
797
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700798 if (enable_stream) {
799 /*
800 * stream mode
801 * see
802 * fsi_pio_push_init()
803 */
804 u32 *buf = (u32 *)_buf;
805
806 for (i = 0; i < samples / 2; i++)
807 fsi_reg_write(fsi, DODT, buf[i]);
808 } else {
809 /* normal mode */
810 u16 *buf = (u16 *)_buf;
811
812 for (i = 0; i < samples; i++)
813 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
814 }
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800815}
816
817static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
818{
819 u16 *buf = (u16 *)_buf;
820 int i;
821
822 for (i = 0; i < samples; i++)
823 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
824}
825
826static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
827{
828 u32 *buf = (u32 *)_buf;
829 int i;
830
831 for (i = 0; i < samples; i++)
832 fsi_reg_write(fsi, DODT, *(buf + i));
833}
834
835static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
836{
837 u32 *buf = (u32 *)_buf;
838 int i;
839
840 for (i = 0; i < samples; i++)
841 *(buf + i) = fsi_reg_read(fsi, DIDT);
842}
843
844static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
845{
846 struct snd_pcm_runtime *runtime = io->substream->runtime;
847
848 return runtime->dma_area +
849 samples_to_bytes(runtime, io->buff_sample_pos);
850}
851
852static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800853 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
854 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
855 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900856{
857 struct snd_pcm_runtime *runtime;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800858 struct snd_pcm_substream *substream;
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800859 u8 *buf;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900860 int over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800862 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900863 return -EINVAL;
864
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900865 over_period = 0;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900866 substream = io->substream;
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900867 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900868
869 /* FSI FIFO has limit.
870 * So, this driver can not send periods data at a time
871 */
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900872 if (io->buff_sample_pos >=
873 io->period_samples * (io->period_pos + 1)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900874
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900875 over_period = 1;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900876 io->period_pos = (io->period_pos + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900877
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900878 if (0 == io->period_pos)
879 io->buff_sample_pos = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900880 }
881
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800882 buf = fsi_pio_get_area(fsi, io);
883
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800884 switch (io->sample_width) {
885 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800886 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800887 break;
888 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -0800889 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800890 break;
891 default:
892 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900893 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900894
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900895 /* update buff_sample_pos */
896 io->buff_sample_pos += samples;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900897
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900898 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900899 snd_pcm_period_elapsed(substream);
900
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900901 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900902}
903
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800904static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900905{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800906 int sample_residues; /* samples in FSI fifo */
907 int sample_space; /* ALSA free samples space */
908 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800909
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800910 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800911 sample_space = io->buff_sample_capa - io->buff_sample_pos;
912
913 samples = min(sample_residues, sample_space);
914
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800915 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800916 fsi_pio_pop16,
917 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800918 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900919}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900920
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800921static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900922{
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800923 int sample_residues; /* ALSA residue samples */
924 int sample_space; /* FSI fifo free samples space */
925 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800926
927 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
928 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800929 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800930
931 samples = min(sample_residues, sample_space);
932
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -0800933 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -0800934 fsi_pio_push16,
935 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -0800936 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900937}
938
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800939static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
940 int enable)
941{
942 struct fsi_master *master = fsi_get_master(fsi);
943 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
944
945 if (enable)
946 fsi_irq_enable(fsi, io);
947 else
948 fsi_irq_disable(fsi, io);
949
950 if (fsi_is_clk_master(fsi))
951 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
952}
953
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700954static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
955{
956 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
957
958 /*
959 * we can use 16bit stream mode
960 * when "playback" and "16bit data"
961 * and platform allows "stream mode"
962 * see
963 * fsi_pio_push16()
964 */
965 if (enable_stream)
966 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
967 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
968 else
969 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
970 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
971 return 0;
972}
973
974static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
975{
976 /*
977 * always 24bit bus, package back when "capture"
978 */
979 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
980 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
981 return 0;
982}
983
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800984static struct fsi_stream_handler fsi_pio_push_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700985 .init = fsi_pio_push_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800986 .transfer = fsi_pio_push,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800987 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800988};
989
990static struct fsi_stream_handler fsi_pio_pop_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700991 .init = fsi_pio_pop_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800992 .transfer = fsi_pio_pop,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800993 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800994};
995
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900996static irqreturn_t fsi_interrupt(int irq, void *data)
997{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900998 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900999 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001000
1001 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +09001002 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1003 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001004
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001005 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001006 fsi_stream_transfer(&master->fsia.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001007 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001008 fsi_stream_transfer(&master->fsib.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001009 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001010 fsi_stream_transfer(&master->fsia.capture);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001011 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001012 fsi_stream_transfer(&master->fsib.capture);
Kuninori Morimoto1ec9bc32010-12-17 12:55:22 +09001013
1014 fsi_count_fifo_err(&master->fsia);
1015 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001016
Kuninori Morimoto48d78e52010-12-03 17:37:31 +09001017 fsi_irq_clear_status(&master->fsia);
1018 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001019
1020 return IRQ_HANDLED;
1021}
1022
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001023/*
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001024 * dma data transfer handler
1025 */
1026static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1027{
1028 struct snd_pcm_runtime *runtime = io->substream->runtime;
1029 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1030 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1031 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1032
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001033 /*
1034 * 24bit data : 24bit bus / package in back
1035 * 16bit data : 16bit bus / stream mode
1036 */
1037 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1038 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1039
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001040 io->dma = dma_map_single(dai->dev, runtime->dma_area,
1041 snd_pcm_lib_buffer_bytes(io->substream), dir);
1042 return 0;
1043}
1044
1045static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
1046{
1047 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1048 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1049 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1050
1051 dma_unmap_single(dai->dev, io->dma,
1052 snd_pcm_lib_buffer_bytes(io->substream), dir);
1053 return 0;
1054}
1055
Kuninori Morimoto4a1b09b2012-05-28 23:27:49 -07001056static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
1057{
1058 struct snd_pcm_runtime *runtime = io->substream->runtime;
1059
1060 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
1061}
1062
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001063static void fsi_dma_complete(void *data)
1064{
1065 struct fsi_stream *io = (struct fsi_stream *)data;
1066 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1067 struct snd_pcm_runtime *runtime = io->substream->runtime;
1068 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1069 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1070 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1071
Kuninori Morimoto4a1b09b2012-05-28 23:27:49 -07001072 dma_sync_single_for_cpu(dai->dev, fsi_dma_get_area(io),
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001073 samples_to_bytes(runtime, io->period_samples), dir);
1074
1075 io->buff_sample_pos += io->period_samples;
1076 io->period_pos++;
1077
1078 if (io->period_pos >= runtime->periods) {
1079 io->period_pos = 0;
1080 io->buff_sample_pos = 0;
1081 }
1082
1083 fsi_count_fifo_err(fsi);
1084 fsi_stream_transfer(io);
1085
1086 snd_pcm_period_elapsed(io->substream);
1087}
1088
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +02001089static void fsi_dma_do_work(struct work_struct *work)
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001090{
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +02001091 struct fsi_stream *io = container_of(work, struct fsi_stream, work);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001092 struct fsi_priv *fsi = fsi_stream_to_priv(io);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001093 struct snd_soc_dai *dai;
1094 struct dma_async_tx_descriptor *desc;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001095 struct snd_pcm_runtime *runtime;
1096 enum dma_data_direction dir;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001097 int is_play = fsi_stream_is_play(fsi, io);
1098 int len;
1099 dma_addr_t buf;
1100
1101 if (!fsi_stream_is_working(fsi, io))
1102 return;
1103
1104 dai = fsi_get_dai(io->substream);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001105 runtime = io->substream->runtime;
1106 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1107 len = samples_to_bytes(runtime, io->period_samples);
1108 buf = fsi_dma_get_area(io);
1109
Kuninori Morimoto4a1b09b2012-05-28 23:27:49 -07001110 dma_sync_single_for_device(dai->dev, buf, len, dir);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001111
Kuninori Morimoto5514efd2012-05-28 23:29:36 -07001112 desc = dmaengine_prep_slave_single(io->chan, buf, len, dir,
1113 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001114 if (!desc) {
Paul Mundtcdf27f32012-04-17 19:13:04 -07001115 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001116 return;
1117 }
1118
1119 desc->callback = fsi_dma_complete;
1120 desc->callback_param = io;
1121
Kuninori Morimoto5514efd2012-05-28 23:29:36 -07001122 if (dmaengine_submit(desc) < 0) {
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001123 dev_err(dai->dev, "tx_submit() fail\n");
1124 return;
1125 }
1126
Kuninori Morimoto5514efd2012-05-28 23:29:36 -07001127 dma_async_issue_pending(io->chan);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001128
1129 /*
1130 * FIXME
1131 *
1132 * In DMAEngine case, codec and FSI cannot be started simultaneously
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +02001133 * since FSI is using the scheduler work queue.
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001134 * Therefore, in capture case, probably FSI FIFO will have got
1135 * overflow error in this point.
1136 * in that case, DMA cannot start transfer until error was cleared.
1137 */
1138 if (!is_play) {
1139 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1140 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1141 fsi_reg_write(fsi, DIFF_ST, 0);
1142 }
1143 }
1144}
1145
1146static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1147{
1148 struct sh_dmae_slave *slave = param;
1149
1150 chan->private = slave;
1151
1152 return true;
1153}
1154
1155static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1156{
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +02001157 schedule_work(&io->work);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001158
1159 return 0;
1160}
1161
1162static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1163 int start)
1164{
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001165 struct fsi_master *master = fsi_get_master(fsi);
1166 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001167 u32 enable = start ? DMA_ON : 0;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001168
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001169 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001170
Kuninori Morimotofbe42f62012-05-28 23:28:22 -07001171 dmaengine_terminate_all(io->chan);
1172
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001173 if (fsi_is_clk_master(fsi))
1174 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001175}
1176
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001177static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev)
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001178{
1179 dma_cap_mask_t mask;
1180
1181 dma_cap_zero(mask);
1182 dma_cap_set(DMA_SLAVE, mask);
1183
1184 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001185 if (!io->chan) {
1186
1187 /* switch to PIO handler */
1188 if (fsi_stream_is_play(fsi, io))
1189 fsi->playback.handler = &fsi_pio_push_handler;
1190 else
1191 fsi->capture.handler = &fsi_pio_pop_handler;
1192
1193 dev_info(dev, "switch handler (dma => pio)\n");
1194
1195 /* probe again */
1196 return fsi_stream_probe(fsi, dev);
1197 }
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001198
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +02001199 INIT_WORK(&io->work, fsi_dma_do_work);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001200
1201 return 0;
1202}
1203
1204static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1205{
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +02001206 cancel_work_sync(&io->work);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001207
1208 fsi_stream_stop(fsi, io);
1209
1210 if (io->chan)
1211 dma_release_channel(io->chan);
1212
1213 io->chan = NULL;
1214 return 0;
1215}
1216
1217static struct fsi_stream_handler fsi_dma_push_handler = {
1218 .init = fsi_dma_init,
1219 .quit = fsi_dma_quit,
1220 .probe = fsi_dma_probe,
1221 .transfer = fsi_dma_transfer,
1222 .remove = fsi_dma_remove,
1223 .start_stop = fsi_dma_push_start_stop,
1224};
1225
1226/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001227 * dai ops
1228 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001229static void fsi_fifo_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001230 struct fsi_stream *io,
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001231 struct device *dev)
1232{
1233 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001234 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001235 u32 shift, i;
1236 int frame_capa;
1237
1238 /* get on-chip RAM capacity */
1239 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001240 shift >>= fsi_get_port_shift(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001241 shift &= FIFO_SZ_MASK;
1242 frame_capa = 256 << shift;
1243 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1244
1245 /*
1246 * The maximum number of sample data varies depending
1247 * on the number of channels selected for the format.
1248 *
1249 * FIFOs are used in 4-channel units in 3-channel mode
1250 * and in 8-channel units in 5- to 7-channel mode
1251 * meaning that more FIFOs than the required size of DPRAM
1252 * are used.
1253 *
1254 * ex) if 256 words of DP-RAM is connected
1255 * 1 channel: 256 (256 x 1 = 256)
1256 * 2 channels: 128 (128 x 2 = 256)
1257 * 3 channels: 64 ( 64 x 3 = 192)
1258 * 4 channels: 64 ( 64 x 4 = 256)
1259 * 5 channels: 32 ( 32 x 5 = 160)
1260 * 6 channels: 32 ( 32 x 6 = 192)
1261 * 7 channels: 32 ( 32 x 7 = 224)
1262 * 8 channels: 32 ( 32 x 8 = 256)
1263 */
1264 for (i = 1; i < fsi->chan_num; i <<= 1)
1265 frame_capa >>= 1;
1266 dev_dbg(dev, "%d channel %d store\n",
1267 fsi->chan_num, frame_capa);
1268
1269 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1270
1271 /*
1272 * set interrupt generation factor
1273 * clear FIFO
1274 */
1275 if (is_play) {
1276 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1277 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1278 } else {
1279 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1280 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1281 }
1282}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001283
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001284static int fsi_hw_startup(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001285 struct fsi_stream *io,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001286 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001287{
Kuninori Morimoto93193c22010-10-12 19:19:28 +09001288 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001289 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001290
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001291 /* clock setting */
1292 if (fsi_is_clk_master(fsi))
1293 data = DIMD | DOMD;
1294
1295 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001296
1297 /* clock inversion (CKG2) */
1298 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +09001299 if (SH_FSI_LRM_INV & flags)
1300 data |= 1 << 12;
1301 if (SH_FSI_BRM_INV & flags)
1302 data |= 1 << 8;
1303 if (SH_FSI_LRS_INV & flags)
1304 data |= 1 << 4;
1305 if (SH_FSI_BRS_INV & flags)
1306 data |= 1 << 0;
1307
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001308 fsi_reg_write(fsi, CKG2, data);
1309
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001310 /* spdif ? */
1311 if (fsi_is_spdif(fsi)) {
1312 fsi_spdif_clk_ctrl(fsi, 1);
1313 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1314 }
1315
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001316 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001317 * get bus settings
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001318 */
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001319 data = 0;
1320 switch (io->sample_width) {
1321 case 2:
1322 data = BUSOP_GET(16, io->bus_option);
1323 break;
1324 case 4:
1325 data = BUSOP_GET(24, io->bus_option);
1326 break;
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001327 }
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001328 fsi_format_bus_setup(fsi, io, data, dev);
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001329
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001330 /* irq clear */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001331 fsi_irq_disable(fsi, io);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001332 fsi_irq_clear_status(fsi);
1333
1334 /* fifo init */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001335 fsi_fifo_init(fsi, io, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001336
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +09001337 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001338}
1339
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001340static void fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001341 struct device *dev)
1342{
1343 if (fsi_is_clk_master(fsi))
1344 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001345}
1346
1347static int fsi_dai_startup(struct snd_pcm_substream *substream,
1348 struct snd_soc_dai *dai)
1349{
1350 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001351
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001352 fsi->rate = 0;
1353
1354 return 0;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001355}
1356
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001357static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1358 struct snd_soc_dai *dai)
1359{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001360 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001361
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001362 fsi->rate = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001363}
1364
1365static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1366 struct snd_soc_dai *dai)
1367{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001368 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001369 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001370 int ret = 0;
1371
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001372 switch (cmd) {
1373 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001374 fsi_stream_init(fsi, io, substream);
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001375 fsi_hw_startup(fsi, io, dai->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001376 ret = fsi_stream_transfer(io);
1377 if (0 == ret)
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001378 fsi_stream_start(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001379 break;
1380 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001381 fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001382 fsi_stream_stop(fsi, io);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001383 fsi_stream_quit(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001384 break;
1385 }
1386
1387 return ret;
1388}
1389
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001390static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1391{
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001392 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1393 case SND_SOC_DAIFMT_I2S:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001394 fsi->fmt = CR_I2S;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001395 fsi->chan_num = 2;
1396 break;
1397 case SND_SOC_DAIFMT_LEFT_J:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001398 fsi->fmt = CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001399 fsi->chan_num = 2;
1400 break;
1401 default:
1402 return -EINVAL;
1403 }
1404
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001405 return 0;
1406}
1407
1408static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1409{
1410 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001411
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001412 if (fsi_version(master) < 2)
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001413 return -EINVAL;
1414
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001415 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001416 fsi->chan_num = 2;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001417 fsi->spdif = 1;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001418
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001419 return 0;
1420}
1421
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001422static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1423{
1424 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001425 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001426 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001427 int ret;
1428
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001429 /* set master/slave audio interface */
1430 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1431 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001432 fsi->clk_master = 1;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001433 break;
1434 case SND_SOC_DAIFMT_CBS_CFS:
1435 break;
1436 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001437 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001438 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001439
1440 if (fsi_is_clk_master(fsi) && !set_rate) {
1441 dev_err(dai->dev, "platform doesn't have set_rate\n");
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001442 return -EINVAL;
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001443 }
1444
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001445 /* set format */
1446 switch (flags & SH_FSI_FMT_MASK) {
1447 case SH_FSI_FMT_DAI:
1448 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1449 break;
1450 case SH_FSI_FMT_SPDIF:
1451 ret = fsi_set_fmt_spdif(fsi);
1452 break;
1453 default:
1454 ret = -EINVAL;
1455 }
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001456
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001457 return ret;
1458}
1459
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001460static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1461 struct snd_pcm_hw_params *params,
1462 struct snd_soc_dai *dai)
1463{
1464 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001465 long rate = params_rate(params);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001466 int ret;
1467
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001468 if (!fsi_is_clk_master(fsi))
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001469 return 0;
1470
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001471 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1472 if (ret < 0)
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001473 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001474
Kuninori Morimotod4bc99b2010-11-24 02:44:06 +00001475 fsi->rate = rate;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001476
1477 return ret;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001478}
1479
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001480static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001481 .startup = fsi_dai_startup,
1482 .shutdown = fsi_dai_shutdown,
1483 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001484 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001485 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001486};
1487
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001488/*
1489 * pcm ops
1490 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001491
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001492static struct snd_pcm_hardware fsi_pcm_hardware = {
1493 .info = SNDRV_PCM_INFO_INTERLEAVED |
1494 SNDRV_PCM_INFO_MMAP |
1495 SNDRV_PCM_INFO_MMAP_VALID |
1496 SNDRV_PCM_INFO_PAUSE,
1497 .formats = FSI_FMTS,
1498 .rates = FSI_RATES,
1499 .rate_min = 8000,
1500 .rate_max = 192000,
1501 .channels_min = 1,
1502 .channels_max = 2,
1503 .buffer_bytes_max = 64 * 1024,
1504 .period_bytes_min = 32,
1505 .period_bytes_max = 8192,
1506 .periods_min = 1,
1507 .periods_max = 32,
1508 .fifo_size = 256,
1509};
1510
1511static int fsi_pcm_open(struct snd_pcm_substream *substream)
1512{
1513 struct snd_pcm_runtime *runtime = substream->runtime;
1514 int ret = 0;
1515
1516 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1517
1518 ret = snd_pcm_hw_constraint_integer(runtime,
1519 SNDRV_PCM_HW_PARAM_PERIODS);
1520
1521 return ret;
1522}
1523
1524static int fsi_hw_params(struct snd_pcm_substream *substream,
1525 struct snd_pcm_hw_params *hw_params)
1526{
1527 return snd_pcm_lib_malloc_pages(substream,
1528 params_buffer_bytes(hw_params));
1529}
1530
1531static int fsi_hw_free(struct snd_pcm_substream *substream)
1532{
1533 return snd_pcm_lib_free_pages(substream);
1534}
1535
1536static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1537{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001538 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001539 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001540
Kuninori Morimoto19878772012-02-08 16:57:29 -08001541 return fsi_sample2frame(fsi, io->buff_sample_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001542}
1543
1544static struct snd_pcm_ops fsi_pcm_ops = {
1545 .open = fsi_pcm_open,
1546 .ioctl = snd_pcm_lib_ioctl,
1547 .hw_params = fsi_hw_params,
1548 .hw_free = fsi_hw_free,
1549 .pointer = fsi_pointer,
1550};
1551
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001552/*
1553 * snd_soc_platform
1554 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001555
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001556#define PREALLOC_BUFFER (32 * 1024)
1557#define PREALLOC_BUFFER_MAX (32 * 1024)
1558
1559static void fsi_pcm_free(struct snd_pcm *pcm)
1560{
1561 snd_pcm_lib_preallocate_free_for_all(pcm);
1562}
1563
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001564static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001565{
Liam Girdwood552d1ef2011-06-07 16:08:33 +01001566 struct snd_pcm *pcm = rtd->pcm;
1567
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001568 /*
1569 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1570 * in MMAP mode (i.e. aplay -M)
1571 */
1572 return snd_pcm_lib_preallocate_pages_for_all(
1573 pcm,
1574 SNDRV_DMA_TYPE_CONTINUOUS,
1575 snd_dma_continuous_data(GFP_KERNEL),
1576 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1577}
1578
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001579/*
1580 * alsa struct
1581 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001584 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001585 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001586 .playback = {
1587 .rates = FSI_RATES,
1588 .formats = FSI_FMTS,
1589 .channels_min = 1,
1590 .channels_max = 8,
1591 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001592 .capture = {
1593 .rates = FSI_RATES,
1594 .formats = FSI_FMTS,
1595 .channels_min = 1,
1596 .channels_max = 8,
1597 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001598 .ops = &fsi_dai_ops,
1599 },
1600 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001602 .playback = {
1603 .rates = FSI_RATES,
1604 .formats = FSI_FMTS,
1605 .channels_min = 1,
1606 .channels_max = 8,
1607 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001608 .capture = {
1609 .rates = FSI_RATES,
1610 .formats = FSI_FMTS,
1611 .channels_min = 1,
1612 .channels_max = 8,
1613 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001614 .ops = &fsi_dai_ops,
1615 },
1616};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001618static struct snd_soc_platform_driver fsi_soc_platform = {
1619 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001620 .pcm_new = fsi_pcm_new,
1621 .pcm_free = fsi_pcm_free,
1622};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001623
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001624/*
1625 * platform function
1626 */
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001627static void fsi_handler_init(struct fsi_priv *fsi)
1628{
1629 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1630 fsi->playback.priv = fsi;
1631 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1632 fsi->capture.priv = fsi;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001633
1634 if (fsi->info->tx_id) {
Guennadi Liakhovetskib8373142012-05-09 17:09:20 +02001635 fsi->playback.slave.shdma_slave.slave_id = fsi->info->tx_id;
1636 fsi->playback.handler = &fsi_dma_push_handler;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001637 }
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001638}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001639
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001640static int fsi_probe(struct platform_device *pdev)
1641{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001642 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001643 const struct platform_device_id *id_entry;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001644 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001645 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001646 unsigned int irq;
1647 int ret;
1648
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001649 id_entry = pdev->id_entry;
1650 if (!id_entry) {
1651 dev_err(&pdev->dev, "unknown fsi device\n");
1652 return -ENODEV;
1653 }
1654
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001655 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1656 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001657 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001658 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001659 return -ENODEV;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001660 }
1661
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001662 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001663 if (!master) {
1664 dev_err(&pdev->dev, "Could not allocate master\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001665 return -ENOMEM;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001666 }
1667
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001668 master->base = devm_ioremap_nocache(&pdev->dev,
1669 res->start, resource_size(res));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001670 if (!master->base) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001671 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001672 return -ENXIO;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001673 }
1674
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001675 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001676 master->irq = irq;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001677 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001678 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001679
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001680 /* FSI A setting */
1681 master->fsia.base = master->base;
1682 master->fsia.master = master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001683 master->fsia.info = &info->port_a;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001684 fsi_handler_init(&master->fsia);
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001685 ret = fsi_stream_probe(&master->fsia, &pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001686 if (ret < 0) {
1687 dev_err(&pdev->dev, "FSIA stream probe failed\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001688 return ret;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001689 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001690
1691 /* FSI B setting */
1692 master->fsib.base = master->base + 0x40;
1693 master->fsib.master = master;
Kuninori Morimotofec691e2012-02-03 00:58:48 -08001694 master->fsib.info = &info->port_b;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001695 fsi_handler_init(&master->fsib);
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001696 ret = fsi_stream_probe(&master->fsib, &pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001697 if (ret < 0) {
1698 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1699 goto exit_fsia;
1700 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001701
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001702 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001704
Yong Zhang88e24c32011-09-22 16:59:20 +08001705 ret = request_irq(irq, &fsi_interrupt, 0,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001706 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001707 if (ret) {
1708 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001709 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001710 }
1711
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001712 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001713 if (ret < 0) {
1714 dev_err(&pdev->dev, "cannot snd soc register\n");
1715 goto exit_free_irq;
1716 }
1717
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001718 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1719 ARRAY_SIZE(fsi_soc_dai));
1720 if (ret < 0) {
1721 dev_err(&pdev->dev, "cannot snd dai register\n");
1722 goto exit_snd_soc;
1723 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001724
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001725 return ret;
1726
1727exit_snd_soc:
1728 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001729exit_free_irq:
1730 free_irq(irq, master);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001731exit_fsib:
Kuninori Morimotoc35e0052012-09-10 02:13:52 -07001732 pm_runtime_disable(&pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001733 fsi_stream_remove(&master->fsib);
1734exit_fsia:
1735 fsi_stream_remove(&master->fsia);
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001736
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001737 return ret;
1738}
1739
1740static int fsi_remove(struct platform_device *pdev)
1741{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001742 struct fsi_master *master;
1743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001744 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001745
Kuninori Morimotod985f272011-04-08 15:09:25 +09001746 free_irq(master->irq, master);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001747 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001748
Kuninori Morimotod985f272011-04-08 15:09:25 +09001749 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1750 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001751
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001752 fsi_stream_remove(&master->fsia);
1753 fsi_stream_remove(&master->fsib);
1754
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001755 return 0;
1756}
1757
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001758static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001759 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001760 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001761{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001762 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001763 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001764
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001765 fsi_stream_stop(fsi, io);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08001766 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001767}
1768
1769static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001770 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001771 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001772{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001773 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001774 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001775
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001776 fsi_hw_startup(fsi, io, dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001777
1778 if (fsi_is_clk_master(fsi) && fsi->rate)
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09001779 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001780
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001781 fsi_stream_start(fsi, io);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001782}
1783
1784static int fsi_suspend(struct device *dev)
1785{
1786 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001787 struct fsi_priv *fsia = &master->fsia;
1788 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001789
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001790 __fsi_suspend(fsia, &fsia->playback, dev);
1791 __fsi_suspend(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001792
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001793 __fsi_suspend(fsib, &fsib->playback, dev);
1794 __fsi_suspend(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001795
1796 return 0;
1797}
1798
1799static int fsi_resume(struct device *dev)
1800{
1801 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09001802 struct fsi_priv *fsia = &master->fsia;
1803 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001804
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001805 __fsi_resume(fsia, &fsia->playback, dev);
1806 __fsi_resume(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001807
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001808 __fsi_resume(fsib, &fsib->playback, dev);
1809 __fsi_resume(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001810
1811 return 0;
1812}
1813
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001814static struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09001815 .suspend = fsi_suspend,
1816 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001817};
1818
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001819static struct fsi_core fsi1_core = {
1820 .ver = 1,
1821
1822 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001823 .int_st = INT_ST,
1824 .iemsk = IEMSK,
1825 .imsk = IMSK,
1826};
1827
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001828static struct fsi_core fsi2_core = {
1829 .ver = 2,
1830
1831 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001832 .int_st = CPU_INT_ST,
1833 .iemsk = CPU_IEMSK,
1834 .imsk = CPU_IMSK,
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +09001835 .a_mclk = A_MST_CTLR,
1836 .b_mclk = B_MST_CTLR,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001837};
1838
1839static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001840 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1841 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001842 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001843};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001844MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001845
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001846static struct platform_driver fsi_driver = {
1847 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001848 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001849 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001850 },
1851 .probe = fsi_probe,
1852 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001853 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001854};
1855
Axel Lincb5e8732011-11-25 10:15:07 +08001856module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001857
1858MODULE_LICENSE("GPL");
1859MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1860MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02001861MODULE_ALIAS("platform:fsi-pcm-audio");