blob: 1c3c4fdc9befdfe071c33a7ad40d1c84dc20afa9 [file] [log] [blame]
Kuninori Morimoto134c8752018-06-12 05:56:51 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// Fifo-attached Serial Interface (FSI) support for SH7724
4//
5// Copyright (C) 2009 Renesas Solutions Corp.
6// Kuninori Morimoto <morimoto.kuninori@renesas.com>
7//
8// Based on ssi.c
9// Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090010
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090011#include <linux/delay.h>
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080012#include <linux/dma-mapping.h>
Kuninori Morimoto785d1c42009-11-30 20:24:48 +090013#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090014#include <linux/io.h>
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -080015#include <linux/of.h>
16#include <linux/of_device.h>
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080017#include <linux/scatterlist.h>
18#include <linux/sh_dma.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040020#include <linux/module.h>
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +020021#include <linux/workqueue.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090022#include <sound/soc.h>
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -080023#include <sound/pcm_params.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090024#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090025
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090026/* PortA/PortB register */
27#define REG_DO_FMT 0x0000
28#define REG_DOFF_CTL 0x0004
29#define REG_DOFF_ST 0x0008
30#define REG_DI_FMT 0x000C
31#define REG_DIFF_CTL 0x0010
32#define REG_DIFF_ST 0x0014
33#define REG_CKG1 0x0018
34#define REG_CKG2 0x001C
35#define REG_DIDT 0x0020
36#define REG_DODT 0x0024
37#define REG_MUTE_ST 0x0028
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080038#define REG_OUT_DMAC 0x002C
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +090039#define REG_OUT_SEL 0x0030
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -080040#define REG_IN_DMAC 0x0038
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090041
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +090042/* master register */
43#define MST_CLK_RST 0x0210
44#define MST_SOFT_RST 0x0214
45#define MST_FIFO_SZ 0x0218
46
47/* core register (depend on FSI version) */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090048#define A_MST_CTLR 0x0180
49#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090050#define CPU_INT_ST 0x01F4
51#define CPU_IEMSK 0x01F8
52#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090053#define INT_ST 0x0200
54#define IEMSK 0x0204
55#define IMSK 0x0208
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090056
57/* DO_FMT */
58/* DI_FMT */
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080059#define CR_BWS_MASK (0x3 << 20) /* FSI2 */
Kuninori Morimotof7d711e2010-12-03 17:36:24 +090060#define CR_BWS_24 (0x0 << 20) /* FSI2 */
61#define CR_BWS_16 (0x1 << 20) /* FSI2 */
62#define CR_BWS_20 (0x2 << 20) /* FSI2 */
63
64#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
65#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
66#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
67
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090068#define CR_MONO (0x0 << 4)
69#define CR_MONO_D (0x1 << 4)
70#define CR_PCM (0x2 << 4)
71#define CR_I2S (0x3 << 4)
72#define CR_TDM (0x4 << 4)
73#define CR_TDM_D (0x5 << 4)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090074
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -080075/* OUT_DMAC */
76/* IN_DMAC */
77#define VDMD_MASK (0x3 << 4)
78#define VDMD_FRONT (0x0 << 4) /* Package in front */
79#define VDMD_BACK (0x1 << 4) /* Package in back */
80#define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
81
82#define DMA_ON (0x1 << 0)
83
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090084/* DOFF_CTL */
85/* DIFF_CTL */
86#define IRQ_HALF 0x00100000
87#define FIFO_CLR 0x00000001
88
89/* DOFF_ST */
90#define ERR_OVER 0x00000010
91#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090092#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090093
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090094/* CKG1 */
95#define ACKMD_MASK 0x00007000
96#define BPFMD_MASK 0x00000700
Kuninori Morimoto4d805f72011-01-20 11:46:02 +090097#define DIMD (1 << 4)
98#define DOMD (1 << 0)
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090099
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900100/* A/B MST_CTLR */
101#define BP (1 << 4) /* Fix the signal of Biphase output */
102#define SE (1 << 0) /* Fix the master clock */
103
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900104/* CLK_RST */
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900105#define CRB (1 << 4)
106#define CRA (1 << 0)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900107
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900108/* IO SHIFT / MACRO */
109#define BI_SHIFT 12
110#define BO_SHIFT 8
111#define AI_SHIFT 4
112#define AO_SHIFT 0
113#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900114
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900115/* SOFT_RST */
116#define PBSR (1 << 12) /* Port B Software Reset */
117#define PASR (1 << 8) /* Port A Software Reset */
118#define IR (1 << 4) /* Interrupt Reset */
119#define FSISR (1 << 0) /* Software Reset */
120
Kuninori Morimotof7d711e2010-12-03 17:36:24 +0900121/* OUT_SEL (FSI2) */
122#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
123 /* 1: Biphase and serial */
124
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900125/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900126#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900127
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900128#define FSI_RATES SNDRV_PCM_RATE_8000_96000
129
130#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
131
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900132/*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700133 * bus options
134 *
135 * 0x000000BA
136 *
137 * A : sample widtht 16bit setting
138 * B : sample widtht 24bit setting
139 */
140
141#define SHIFT_16DATA 0
142#define SHIFT_24DATA 4
143
144#define PACKAGE_24BITBUS_BACK 0
145#define PACKAGE_24BITBUS_FRONT 1
146#define PACKAGE_16BITBUS_STREAM 2
147
148#define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
149#define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
150
151/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900152 * FSI driver use below type name for variable
153 *
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900154 * xxx_num : number of data
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900155 * xxx_pos : position of data
156 * xxx_capa : capacity of data
157 */
158
159/*
160 * period/frame/sample image
161 *
162 * ex) PCM (2ch)
163 *
164 * period pos period pos
165 * [n] [n + 1]
166 * |<-------------------- period--------------------->|
167 * ==|============================================ ... =|==
168 * | |
169 * ||<----- frame ----->|<------ frame ----->| ... |
170 * |+--------------------+--------------------+- ... |
171 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
172 * |+--------------------+--------------------+- ... |
173 * ==|============================================ ... =|==
174 */
175
176/*
177 * FSI FIFO image
178 *
179 * | |
180 * | |
181 * | [ sample ] |
182 * | [ sample ] |
183 * | [ sample ] |
184 * | [ sample ] |
185 * --> go to codecs
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900186 */
187
188/*
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800189 * FSI clock
190 *
191 * FSIxCLK [CPG] (ick) -------> |
192 * |-> FSI_DIV (div)-> FSI2
193 * FSIxCK [external] (xck) ---> |
194 */
195
196/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900197 * struct
198 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900199
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800200struct fsi_stream_handler;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900201struct fsi_stream {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900202
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800203 /*
204 * these are initialized by fsi_stream_init()
205 */
206 struct snd_pcm_substream *substream;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900207 int fifo_sample_capa; /* sample capacity of FSI FIFO */
208 int buff_sample_capa; /* sample capacity of ALSA buffer */
209 int buff_sample_pos; /* sample position of ALSA buffer */
210 int period_samples; /* sample number / 1 period */
211 int period_pos; /* current period position */
Kuninori Morimotoc1e6f102012-02-03 00:50:09 -0800212 int sample_width; /* sample width */
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900213 int uerr_num;
214 int oerr_num;
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800215
216 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700217 * bus options
218 */
219 u32 bus_option;
220
221 /*
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800222 * thse are initialized by fsi_handler_init()
223 */
224 struct fsi_stream_handler *handler;
225 struct fsi_priv *priv;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -0800226
227 /*
228 * these are for DMAEngine
229 */
230 struct dma_chan *chan;
Kuninori Morimotoa0732782013-12-10 20:46:59 -0800231 int dma_id;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900232};
233
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800234struct fsi_clk {
235 /* see [FSI clock] */
236 struct clk *own;
237 struct clk *xck;
238 struct clk *ick;
239 struct clk *div;
240 int (*set_rate)(struct device *dev,
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800241 struct fsi_priv *fsi);
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800242
243 unsigned long rate;
244 unsigned int count;
245};
246
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900247struct fsi_priv {
248 void __iomem *base;
Kuninori Morimoto7c6cc8f2015-02-17 01:48:19 +0000249 phys_addr_t phys;
Kuninori Morimoto93193c22010-10-12 19:19:28 +0900250 struct fsi_master *master;
251
252 struct fsi_stream playback;
253 struct fsi_stream capture;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900254
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800255 struct fsi_clk clock;
256
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -0700257 u32 fmt;
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900258
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900259 int chan_num:16;
Lars-Peter Clausen9f98cd62014-06-19 09:40:31 +0200260 unsigned int clk_master:1;
261 unsigned int clk_cpg:1;
262 unsigned int spdif:1;
263 unsigned int enable_stream:1;
264 unsigned int bit_clk_inv:1;
265 unsigned int lr_clk_inv:1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900266};
267
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800268struct fsi_stream_handler {
Kuninori Morimoto83344022012-02-03 00:59:02 -0800269 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
270 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700271 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800272 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
273 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
Kuninori Morimotoc375b2d2013-05-28 00:55:12 -0700274 int (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800275 int enable);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800276};
277#define fsi_stream_handler_call(io, func, args...) \
278 (!(io) ? -ENODEV : \
279 !((io)->handler->func) ? 0 : \
280 (io)->handler->func(args))
281
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900282struct fsi_core {
283 int ver;
284
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900285 u32 int_st;
286 u32 iemsk;
287 u32 imsk;
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900288 u32 a_mclk;
289 u32 b_mclk;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900290};
291
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900292struct fsi_master {
293 void __iomem *base;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900294 struct fsi_priv fsia;
295 struct fsi_priv fsib;
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -0800296 const struct fsi_core *core;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900297 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900298};
299
Geert Uytterhoeven3048e762017-05-16 19:20:00 +0200300static inline int fsi_stream_is_play(struct fsi_priv *fsi,
301 struct fsi_stream *io)
302{
303 return &fsi->playback == io;
304}
305
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800306
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900307/*
308 * basic read write function
309 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900310
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200311static void __fsi_reg_write(u32 __iomem *reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900312{
313 /* valid data area is 24bit */
314 data &= 0x00ffffff;
315
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100316 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900317}
318
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200319static u32 __fsi_reg_read(u32 __iomem *reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900320{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100321 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900322}
323
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200324static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900325{
326 u32 val = __fsi_reg_read(reg);
327
328 val &= ~mask;
329 val |= data & mask;
330
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100331 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900332}
333
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900334#define fsi_reg_write(p, r, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700335 __fsi_reg_write((p->base + REG_##r), d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900336
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900337#define fsi_reg_read(p, r)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700338 __fsi_reg_read((p->base + REG_##r))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900339
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900340#define fsi_reg_mask_set(p, r, m, d)\
Kuninori Morimoto8918b842011-10-31 22:11:53 -0700341 __fsi_reg_mask_set((p->base + REG_##r), m, d)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900342
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900343#define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
344#define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
345static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900346{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900347 u32 ret;
348 unsigned long flags;
349
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900350 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200351 ret = __fsi_reg_read(master->base + reg);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900352 spin_unlock_irqrestore(&master->lock, flags);
353
354 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900355}
356
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900357#define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
358#define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
359static void _fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900360 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900361{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900362 unsigned long flags;
363
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900364 spin_lock_irqsave(&master->lock, flags);
Arnd Bergmannca7acee2011-10-02 22:28:02 +0200365 __fsi_reg_mask_set(master->base + reg, mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900366 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900367}
368
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900369/*
370 * basic function
371 */
Kuninori Morimoto284c6f62012-05-17 17:34:16 -0700372static int fsi_version(struct fsi_master *master)
373{
374 return master->core->ver;
375}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900376
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900377static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900378{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900379 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900380}
381
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +0900382static int fsi_is_clk_master(struct fsi_priv *fsi)
383{
384 return fsi->clk_master;
385}
386
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900387static int fsi_is_port_a(struct fsi_priv *fsi)
388{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900389 return fsi->master->base == fsi->base;
390}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900391
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +0900392static int fsi_is_spdif(struct fsi_priv *fsi)
393{
394 return fsi->spdif;
395}
396
Kuninori Morimoto2522acd2012-11-16 01:17:30 -0800397static int fsi_is_enable_stream(struct fsi_priv *fsi)
398{
399 return fsi->enable_stream;
400}
401
Kuninori Morimotoa449e462012-02-03 00:54:02 -0800402static int fsi_is_play(struct snd_pcm_substream *substream)
403{
404 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
405}
406
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900407static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900408{
409 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900410
Kuninori Morimoto34a43782020-03-23 14:20:30 +0900411 return asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900412}
413
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900414static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900415{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000416 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900417
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 if (dai->id == 0)
419 return &master->fsia;
420 else
421 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900422}
423
Kuninori Morimoto0d032c12011-01-20 11:45:51 +0900424static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
425{
426 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
427}
428
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800429static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900430{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800431 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900432 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900433 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900434
435 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900436 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900437 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900438 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900439
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900440 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900441}
442
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900443static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
444{
445 return frames * fsi->chan_num;
446}
447
448static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
449{
450 return samples / fsi->chan_num;
451}
452
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800453static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
454 struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900455{
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -0800456 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900457 u32 status;
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900458 int frames;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900459
Kuninori Morimotoe8c8b632010-12-03 17:37:55 +0900460 status = is_play ?
461 fsi_reg_read(fsi, DOFF_ST) :
462 fsi_reg_read(fsi, DIFF_ST);
463
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900464 frames = 0x1ff & (status >> 8);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900465
Kuninori Morimoto2e651ba2011-05-23 20:46:03 +0900466 return fsi_frame2sample(fsi, frames);
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900467}
468
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +0900469static void fsi_count_fifo_err(struct fsi_priv *fsi)
470{
471 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
472 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
473
474 if (ostatus & ERR_OVER)
475 fsi->playback.oerr_num++;
476
477 if (ostatus & ERR_UNDER)
478 fsi->playback.uerr_num++;
479
480 if (istatus & ERR_OVER)
481 fsi->capture.oerr_num++;
482
483 if (istatus & ERR_UNDER)
484 fsi->capture.uerr_num++;
485
486 fsi_reg_write(fsi, DOFF_ST, 0);
487 fsi_reg_write(fsi, DIFF_ST, 0);
488}
489
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900490/*
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800491 * fsi_stream_xx() function
492 */
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800493static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800494 struct snd_pcm_substream *substream)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800495{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800496 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800497}
498
499static int fsi_stream_is_working(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800500 struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800501{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800502 struct fsi_master *master = fsi_get_master(fsi);
503 unsigned long flags;
504 int ret;
505
506 spin_lock_irqsave(&master->lock, flags);
Kuninori Morimoto97df8182012-02-03 00:57:40 -0800507 ret = !!(io->substream && io->substream->runtime);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800508 spin_unlock_irqrestore(&master->lock, flags);
509
510 return ret;
511}
512
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800513static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
514{
515 return io->priv;
516}
517
Kuninori Morimoto8c415292012-02-03 00:52:07 -0800518static void fsi_stream_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800519 struct fsi_stream *io,
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800520 struct snd_pcm_substream *substream)
521{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800522 struct snd_pcm_runtime *runtime = substream->runtime;
523 struct fsi_master *master = fsi_get_master(fsi);
524 unsigned long flags;
525
526 spin_lock_irqsave(&master->lock, flags);
527 io->substream = substream;
528 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
529 io->buff_sample_pos = 0;
530 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
531 io->period_pos = 0;
532 io->sample_width = samples_to_bytes(runtime, 1);
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700533 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800534 io->oerr_num = -1; /* ignore 1st err */
535 io->uerr_num = -1; /* ignore 1st err */
Kuninori Morimoto83344022012-02-03 00:59:02 -0800536 fsi_stream_handler_call(io, init, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800537 spin_unlock_irqrestore(&master->lock, flags);
538}
539
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800540static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800541{
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800542 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
543 struct fsi_master *master = fsi_get_master(fsi);
544 unsigned long flags;
545
546 spin_lock_irqsave(&master->lock, flags);
547
548 if (io->oerr_num > 0)
549 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
550
551 if (io->uerr_num > 0)
552 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
553
Kuninori Morimoto83344022012-02-03 00:59:02 -0800554 fsi_stream_handler_call(io, quit, fsi, io);
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800555 io->substream = NULL;
556 io->buff_sample_capa = 0;
557 io->buff_sample_pos = 0;
558 io->period_samples = 0;
559 io->period_pos = 0;
560 io->sample_width = 0;
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700561 io->bus_option = 0;
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800562 io->oerr_num = 0;
563 io->uerr_num = 0;
564 spin_unlock_irqrestore(&master->lock, flags);
565}
566
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800567static int fsi_stream_transfer(struct fsi_stream *io)
568{
569 struct fsi_priv *fsi = fsi_stream_to_priv(io);
570 if (!fsi)
571 return -EIO;
572
573 return fsi_stream_handler_call(io, transfer, fsi, io);
574}
575
Kuninori Morimoto180346e2012-02-03 00:57:25 -0800576#define fsi_stream_start(fsi, io)\
577 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
578
579#define fsi_stream_stop(fsi, io)\
580 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
581
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700582static int fsi_stream_probe(struct fsi_priv *fsi, struct device *dev)
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800583{
584 struct fsi_stream *io;
585 int ret1, ret2;
586
587 io = &fsi->playback;
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700588 ret1 = fsi_stream_handler_call(io, probe, fsi, io, dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800589
590 io = &fsi->capture;
Kuninori Morimotob1226dc2012-05-24 23:56:19 -0700591 ret2 = fsi_stream_handler_call(io, probe, fsi, io, dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -0800592
593 if (ret1 < 0)
594 return ret1;
595 if (ret2 < 0)
596 return ret2;
597
598 return 0;
599}
600
601static int fsi_stream_remove(struct fsi_priv *fsi)
602{
603 struct fsi_stream *io;
604 int ret1, ret2;
605
606 io = &fsi->playback;
607 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
608
609 io = &fsi->capture;
610 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
611
612 if (ret1 < 0)
613 return ret1;
614 if (ret2 < 0)
615 return ret2;
616
617 return 0;
618}
619
Kuninori Morimoto4e62d842012-02-03 00:50:35 -0800620/*
Kuninori Morimoto766812e2012-05-17 17:36:47 -0700621 * format/bus/dma setting
622 */
623static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
624 u32 bus, struct device *dev)
625{
626 struct fsi_master *master = fsi_get_master(fsi);
627 int is_play = fsi_stream_is_play(fsi, io);
628 u32 fmt = fsi->fmt;
629
630 if (fsi_version(master) >= 2) {
631 u32 dma = 0;
632
633 /*
634 * FSI2 needs DMA/Bus setting
635 */
636 switch (bus) {
637 case PACKAGE_24BITBUS_FRONT:
638 fmt |= CR_BWS_24;
639 dma |= VDMD_FRONT;
640 dev_dbg(dev, "24bit bus / package in front\n");
641 break;
642 case PACKAGE_16BITBUS_STREAM:
643 fmt |= CR_BWS_16;
644 dma |= VDMD_STREAM;
645 dev_dbg(dev, "16bit bus / stream mode\n");
646 break;
647 case PACKAGE_24BITBUS_BACK:
648 default:
649 fmt |= CR_BWS_24;
650 dma |= VDMD_BACK;
651 dev_dbg(dev, "24bit bus / package in back\n");
652 break;
653 }
654
655 if (is_play)
656 fsi_reg_write(fsi, OUT_DMAC, dma);
657 else
658 fsi_reg_write(fsi, IN_DMAC, dma);
659 }
660
661 if (is_play)
662 fsi_reg_write(fsi, DO_FMT, fmt);
663 else
664 fsi_reg_write(fsi, DI_FMT, fmt);
665}
666
667/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900668 * irq function
669 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900670
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800671static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900672{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800673 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900674 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900675
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900676 fsi_core_mask_set(master, imsk, data, data);
677 fsi_core_mask_set(master, iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900678}
679
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800680static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900681{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800682 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900683 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900684
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900685 fsi_core_mask_set(master, imsk, data, 0);
686 fsi_core_mask_set(master, iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900687}
688
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900689static u32 fsi_irq_get_status(struct fsi_master *master)
690{
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900691 return fsi_core_read(master, int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900692}
693
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900694static void fsi_irq_clear_status(struct fsi_priv *fsi)
695{
696 u32 data = 0;
697 struct fsi_master *master = fsi_get_master(fsi);
698
Kuninori Morimoto938e2a82012-02-03 00:56:57 -0800699 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
700 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900701
702 /* clear interrupt factor */
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900703 fsi_core_mask_set(master, int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900704}
705
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900706/*
707 * SPDIF master clock function
708 *
709 * These functions are used later FSI2
710 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900711static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
712{
713 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900714 u32 mask, val;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900715
Kuninori Morimoto2b0e7302010-12-03 17:37:44 +0900716 mask = BP | SE;
717 val = enable ? mask : 0;
718
719 fsi_is_port_a(fsi) ?
Kuninori Morimoto43fa95c2010-12-03 17:38:03 +0900720 fsi_core_mask_set(master, a_mclk, mask, val) :
721 fsi_core_mask_set(master, b_mclk, mask, val);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900722}
723
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900724/*
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +0900725 * clock function
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900726 */
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800727static int fsi_clk_init(struct device *dev,
728 struct fsi_priv *fsi,
729 int xck,
730 int ick,
731 int div,
732 int (*set_rate)(struct device *dev,
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800733 struct fsi_priv *fsi))
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800734{
735 struct fsi_clk *clock = &fsi->clock;
736 int is_porta = fsi_is_port_a(fsi);
737
738 clock->xck = NULL;
739 clock->ick = NULL;
740 clock->div = NULL;
741 clock->rate = 0;
742 clock->count = 0;
743 clock->set_rate = set_rate;
744
745 clock->own = devm_clk_get(dev, NULL);
746 if (IS_ERR(clock->own))
747 return -EINVAL;
748
749 /* external clock */
750 if (xck) {
751 clock->xck = devm_clk_get(dev, is_porta ? "xcka" : "xckb");
752 if (IS_ERR(clock->xck)) {
753 dev_err(dev, "can't get xck clock\n");
754 return -EINVAL;
755 }
756 if (clock->xck == clock->own) {
757 dev_err(dev, "cpu doesn't support xck clock\n");
758 return -EINVAL;
759 }
760 }
761
762 /* FSIACLK/FSIBCLK */
763 if (ick) {
764 clock->ick = devm_clk_get(dev, is_porta ? "icka" : "ickb");
765 if (IS_ERR(clock->ick)) {
766 dev_err(dev, "can't get ick clock\n");
767 return -EINVAL;
768 }
769 if (clock->ick == clock->own) {
770 dev_err(dev, "cpu doesn't support ick clock\n");
771 return -EINVAL;
772 }
773 }
774
775 /* FSI-DIV */
776 if (div) {
777 clock->div = devm_clk_get(dev, is_porta ? "diva" : "divb");
778 if (IS_ERR(clock->div)) {
779 dev_err(dev, "can't get div clock\n");
780 return -EINVAL;
781 }
782 if (clock->div == clock->own) {
Colin Ian Kingcb8cdb62019-02-17 22:48:30 +0000783 dev_err(dev, "cpu doesn't support div clock\n");
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800784 return -EINVAL;
785 }
786 }
787
788 return 0;
789}
790
791#define fsi_clk_invalid(fsi) fsi_clk_valid(fsi, 0)
792static void fsi_clk_valid(struct fsi_priv *fsi, unsigned long rate)
793{
794 fsi->clock.rate = rate;
795}
796
797static int fsi_clk_is_valid(struct fsi_priv *fsi)
798{
799 return fsi->clock.set_rate &&
800 fsi->clock.rate;
801}
802
803static int fsi_clk_enable(struct device *dev,
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800804 struct fsi_priv *fsi)
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800805{
806 struct fsi_clk *clock = &fsi->clock;
807 int ret = -EINVAL;
808
809 if (!fsi_clk_is_valid(fsi))
810 return ret;
811
812 if (0 == clock->count) {
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800813 ret = clock->set_rate(dev, fsi);
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800814 if (ret < 0) {
815 fsi_clk_invalid(fsi);
816 return ret;
817 }
818
Markus Elfring180cf792015-01-03 19:25:55 +0100819 clk_enable(clock->xck);
820 clk_enable(clock->ick);
821 clk_enable(clock->div);
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800822
823 clock->count++;
824 }
825
826 return ret;
827}
828
829static int fsi_clk_disable(struct device *dev,
830 struct fsi_priv *fsi)
831{
832 struct fsi_clk *clock = &fsi->clock;
833
834 if (!fsi_clk_is_valid(fsi))
835 return -EINVAL;
836
837 if (1 == clock->count--) {
Markus Elfringe98c89e2014-12-02 14:34:30 +0100838 clk_disable(clock->xck);
839 clk_disable(clock->ick);
840 clk_disable(clock->div);
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800841 }
842
843 return 0;
844}
845
846static int fsi_clk_set_ackbpf(struct device *dev,
847 struct fsi_priv *fsi,
848 int ackmd, int bpfmd)
849{
850 u32 data = 0;
851
852 /* check ackmd/bpfmd relationship */
853 if (bpfmd > ackmd) {
854 dev_err(dev, "unsupported rate (%d/%d)\n", ackmd, bpfmd);
855 return -EINVAL;
856 }
857
858 /* ACKMD */
859 switch (ackmd) {
860 case 512:
861 data |= (0x0 << 12);
862 break;
863 case 256:
864 data |= (0x1 << 12);
865 break;
866 case 128:
867 data |= (0x2 << 12);
868 break;
869 case 64:
870 data |= (0x3 << 12);
871 break;
872 case 32:
873 data |= (0x4 << 12);
874 break;
875 default:
876 dev_err(dev, "unsupported ackmd (%d)\n", ackmd);
877 return -EINVAL;
878 }
879
880 /* BPFMD */
881 switch (bpfmd) {
882 case 32:
883 data |= (0x0 << 8);
884 break;
885 case 64:
886 data |= (0x1 << 8);
887 break;
888 case 128:
889 data |= (0x2 << 8);
890 break;
891 case 256:
892 data |= (0x3 << 8);
893 break;
894 case 512:
895 data |= (0x4 << 8);
896 break;
897 case 16:
898 data |= (0x7 << 8);
899 break;
900 default:
901 dev_err(dev, "unsupported bpfmd (%d)\n", bpfmd);
902 return -EINVAL;
903 }
904
905 dev_dbg(dev, "ACKMD/BPFMD = %d/%d\n", ackmd, bpfmd);
906
907 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
908 udelay(10);
909
910 return 0;
911}
912
913static int fsi_clk_set_rate_external(struct device *dev,
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800914 struct fsi_priv *fsi)
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800915{
916 struct clk *xck = fsi->clock.xck;
917 struct clk *ick = fsi->clock.ick;
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800918 unsigned long rate = fsi->clock.rate;
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800919 unsigned long xrate;
920 int ackmd, bpfmd;
921 int ret = 0;
922
923 /* check clock rate */
924 xrate = clk_get_rate(xck);
925 if (xrate % rate) {
926 dev_err(dev, "unsupported clock rate\n");
927 return -EINVAL;
928 }
929
930 clk_set_parent(ick, xck);
931 clk_set_rate(ick, xrate);
932
933 bpfmd = fsi->chan_num * 32;
934 ackmd = xrate / rate;
935
936 dev_dbg(dev, "external/rate = %ld/%ld\n", xrate, rate);
937
938 ret = fsi_clk_set_ackbpf(dev, fsi, ackmd, bpfmd);
939 if (ret < 0)
940 dev_err(dev, "%s failed", __func__);
941
942 return ret;
943}
944
945static int fsi_clk_set_rate_cpg(struct device *dev,
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800946 struct fsi_priv *fsi)
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800947{
948 struct clk *ick = fsi->clock.ick;
949 struct clk *div = fsi->clock.div;
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -0800950 unsigned long rate = fsi->clock.rate;
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -0800951 unsigned long target = 0; /* 12288000 or 11289600 */
952 unsigned long actual, cout;
953 unsigned long diff, min;
954 unsigned long best_cout, best_act;
955 int adj;
956 int ackmd, bpfmd;
957 int ret = -EINVAL;
958
959 if (!(12288000 % rate))
960 target = 12288000;
961 if (!(11289600 % rate))
962 target = 11289600;
963 if (!target) {
964 dev_err(dev, "unsupported rate\n");
965 return ret;
966 }
967
968 bpfmd = fsi->chan_num * 32;
969 ackmd = target / rate;
970 ret = fsi_clk_set_ackbpf(dev, fsi, ackmd, bpfmd);
971 if (ret < 0) {
972 dev_err(dev, "%s failed", __func__);
973 return ret;
974 }
975
976 /*
977 * The clock flow is
978 *
979 * [CPG] = cout => [FSI_DIV] = audio => [FSI] => [codec]
980 *
981 * But, it needs to find best match of CPG and FSI_DIV
982 * combination, since it is difficult to generate correct
983 * frequency of audio clock from ick clock only.
984 * Because ick is created from its parent clock.
985 *
986 * target = rate x [512/256/128/64]fs
987 * cout = round(target x adjustment)
988 * actual = cout / adjustment (by FSI-DIV) ~= target
989 * audio = actual
990 */
991 min = ~0;
992 best_cout = 0;
993 best_act = 0;
994 for (adj = 1; adj < 0xffff; adj++) {
995
996 cout = target * adj;
997 if (cout > 100000000) /* max clock = 100MHz */
998 break;
999
1000 /* cout/actual audio clock */
1001 cout = clk_round_rate(ick, cout);
1002 actual = cout / adj;
1003
1004 /* find best frequency */
1005 diff = abs(actual - target);
1006 if (diff < min) {
1007 min = diff;
1008 best_cout = cout;
1009 best_act = actual;
1010 }
1011 }
1012
1013 ret = clk_set_rate(ick, best_cout);
1014 if (ret < 0) {
1015 dev_err(dev, "ick clock failed\n");
1016 return -EIO;
1017 }
1018
1019 ret = clk_set_rate(div, clk_round_rate(div, best_act));
1020 if (ret < 0) {
1021 dev_err(dev, "div clock failed\n");
1022 return -EIO;
1023 }
1024
1025 dev_dbg(dev, "ick/div = %ld/%ld\n",
1026 clk_get_rate(ick), clk_get_rate(div));
1027
1028 return ret;
1029}
1030
Kuninori Morimotod403e242014-06-22 17:55:41 -07001031static void fsi_pointer_update(struct fsi_stream *io, int size)
1032{
1033 io->buff_sample_pos += size;
1034
1035 if (io->buff_sample_pos >=
1036 io->period_samples * (io->period_pos + 1)) {
1037 struct snd_pcm_substream *substream = io->substream;
1038 struct snd_pcm_runtime *runtime = substream->runtime;
1039
1040 io->period_pos++;
1041
1042 if (io->period_pos >= runtime->periods) {
1043 io->buff_sample_pos = 0;
1044 io->period_pos = 0;
1045 }
1046
1047 snd_pcm_period_elapsed(substream);
1048 }
1049}
1050
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001051/*
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001052 * pio data transfer handler
Kuninori Morimoto1f5e2a32011-04-21 10:33:52 +09001053 */
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001054static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
1055{
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001056 int i;
1057
Kuninori Morimoto2522acd2012-11-16 01:17:30 -08001058 if (fsi_is_enable_stream(fsi)) {
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001059 /*
1060 * stream mode
1061 * see
1062 * fsi_pio_push_init()
1063 */
1064 u32 *buf = (u32 *)_buf;
1065
1066 for (i = 0; i < samples / 2; i++)
1067 fsi_reg_write(fsi, DODT, buf[i]);
1068 } else {
1069 /* normal mode */
1070 u16 *buf = (u16 *)_buf;
1071
1072 for (i = 0; i < samples; i++)
1073 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
1074 }
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001075}
1076
1077static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
1078{
1079 u16 *buf = (u16 *)_buf;
1080 int i;
1081
1082 for (i = 0; i < samples; i++)
1083 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
1084}
1085
1086static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
1087{
1088 u32 *buf = (u32 *)_buf;
1089 int i;
1090
1091 for (i = 0; i < samples; i++)
1092 fsi_reg_write(fsi, DODT, *(buf + i));
1093}
1094
1095static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
1096{
1097 u32 *buf = (u32 *)_buf;
1098 int i;
1099
1100 for (i = 0; i < samples; i++)
1101 *(buf + i) = fsi_reg_read(fsi, DIDT);
1102}
1103
1104static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
1105{
1106 struct snd_pcm_runtime *runtime = io->substream->runtime;
1107
1108 return runtime->dma_area +
1109 samples_to_bytes(runtime, io->buff_sample_pos);
1110}
1111
1112static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001113 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
1114 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
1115 int samples)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001116{
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001117 u8 *buf;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001118
Kuninori Morimoto97df8182012-02-03 00:57:40 -08001119 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001120 return -EINVAL;
1121
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001122 buf = fsi_pio_get_area(fsi, io);
1123
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001124 switch (io->sample_width) {
1125 case 2:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001126 run16(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001127 break;
1128 case 4:
Kuninori Morimoto95b0cf02012-02-03 00:52:38 -08001129 run32(fsi, buf, samples);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001130 break;
1131 default:
1132 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001133 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001134
Kuninori Morimotod403e242014-06-22 17:55:41 -07001135 fsi_pointer_update(io, samples);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001136
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +09001137 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001138}
1139
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001140static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001141{
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001142 int sample_residues; /* samples in FSI fifo */
1143 int sample_space; /* ALSA free samples space */
1144 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001145
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -08001146 sample_residues = fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001147 sample_space = io->buff_sample_capa - io->buff_sample_pos;
1148
1149 samples = min(sample_residues, sample_space);
1150
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001151 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -08001152 fsi_pio_pop16,
1153 fsi_pio_pop32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001154 samples);
Kuninori Morimotod8b33532010-09-17 13:49:05 +09001155}
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001156
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001157static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimotod8b33532010-09-17 13:49:05 +09001158{
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001159 int sample_residues; /* ALSA residue samples */
1160 int sample_space; /* FSI fifo free samples space */
1161 int samples;
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001162
1163 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
1164 sample_space = io->fifo_sample_capa -
Kuninori Morimoto7b1b3332012-02-03 00:55:26 -08001165 fsi_get_current_fifo_samples(fsi, io);
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001166
1167 samples = min(sample_residues, sample_space);
1168
Kuninori Morimoto1b0ca1a2012-02-03 00:56:27 -08001169 return fsi_pio_transfer(fsi, io,
Kuninori Morimotod78629e2012-02-03 00:51:14 -08001170 fsi_pio_push16,
1171 fsi_pio_push32,
Kuninori Morimoto376cf382012-02-03 00:50:59 -08001172 samples);
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001173}
1174
Kuninori Morimotoc375b2d2013-05-28 00:55:12 -07001175static int fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001176 int enable)
1177{
1178 struct fsi_master *master = fsi_get_master(fsi);
1179 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
1180
1181 if (enable)
1182 fsi_irq_enable(fsi, io);
1183 else
1184 fsi_irq_disable(fsi, io);
1185
1186 if (fsi_is_clk_master(fsi))
1187 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
Kuninori Morimotoc375b2d2013-05-28 00:55:12 -07001188
1189 return 0;
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001190}
1191
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001192static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
1193{
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001194 /*
1195 * we can use 16bit stream mode
1196 * when "playback" and "16bit data"
1197 * and platform allows "stream mode"
1198 * see
1199 * fsi_pio_push16()
1200 */
Kuninori Morimoto2522acd2012-11-16 01:17:30 -08001201 if (fsi_is_enable_stream(fsi))
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001202 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1203 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1204 else
1205 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1206 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
1207 return 0;
1208}
1209
1210static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
1211{
1212 /*
1213 * always 24bit bus, package back when "capture"
1214 */
1215 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1216 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
1217 return 0;
1218}
1219
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001220static struct fsi_stream_handler fsi_pio_push_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001221 .init = fsi_pio_push_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001222 .transfer = fsi_pio_push,
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001223 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001224};
1225
1226static struct fsi_stream_handler fsi_pio_pop_handler = {
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001227 .init = fsi_pio_pop_init,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001228 .transfer = fsi_pio_pop,
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001229 .start_stop = fsi_pio_start_stop,
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001230};
1231
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001232static irqreturn_t fsi_interrupt(int irq, void *data)
1233{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001234 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001235 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001236
1237 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +09001238 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1239 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001240
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001241 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001242 fsi_stream_transfer(&master->fsia.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001243 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001244 fsi_stream_transfer(&master->fsib.playback);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001245 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001246 fsi_stream_transfer(&master->fsia.capture);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +09001247 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001248 fsi_stream_transfer(&master->fsib.capture);
Kuninori Morimoto1ec9bc352010-12-17 12:55:22 +09001249
1250 fsi_count_fifo_err(&master->fsia);
1251 fsi_count_fifo_err(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001252
Kuninori Morimoto48d78e52010-12-03 17:37:31 +09001253 fsi_irq_clear_status(&master->fsia);
1254 fsi_irq_clear_status(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001255
1256 return IRQ_HANDLED;
1257}
1258
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001259/*
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001260 * dma data transfer handler
1261 */
1262static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1263{
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001264 /*
1265 * 24bit data : 24bit bus / package in back
1266 * 16bit data : 16bit bus / stream mode
1267 */
1268 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1269 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1270
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001271 return 0;
1272}
1273
1274static void fsi_dma_complete(void *data)
1275{
1276 struct fsi_stream *io = (struct fsi_stream *)data;
1277 struct fsi_priv *fsi = fsi_stream_to_priv(io);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001278
Kuninori Morimotod403e242014-06-22 17:55:41 -07001279 fsi_pointer_update(io, io->period_samples);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001280
1281 fsi_count_fifo_err(fsi);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001282}
1283
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001284static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001285{
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001286 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1287 struct snd_pcm_substream *substream = io->substream;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001288 struct dma_async_tx_descriptor *desc;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001289 int is_play = fsi_stream_is_play(fsi, io);
Lars-Peter Clausen6c7d1df2014-08-17 16:18:20 +02001290 enum dma_transfer_direction dir;
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001291 int ret = -EIO;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001292
Lars-Peter Clausen6c7d1df2014-08-17 16:18:20 +02001293 if (is_play)
1294 dir = DMA_MEM_TO_DEV;
1295 else
1296 dir = DMA_DEV_TO_MEM;
1297
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001298 desc = dmaengine_prep_dma_cyclic(io->chan,
1299 substream->runtime->dma_addr,
1300 snd_pcm_lib_buffer_bytes(substream),
1301 snd_pcm_lib_period_bytes(substream),
1302 dir,
1303 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1304 if (!desc) {
1305 dev_err(dai->dev, "dmaengine_prep_dma_cyclic() fail\n");
1306 goto fsi_dma_transfer_err;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001307 }
1308
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001309 desc->callback = fsi_dma_complete;
1310 desc->callback_param = io;
1311
1312 if (dmaengine_submit(desc) < 0) {
1313 dev_err(dai->dev, "tx_submit() fail\n");
1314 goto fsi_dma_transfer_err;
1315 }
1316
1317 dma_async_issue_pending(io->chan);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001318
1319 /*
1320 * FIXME
1321 *
1322 * In DMAEngine case, codec and FSI cannot be started simultaneously
Guennadi Liakhovetski57451e42012-10-03 14:33:50 +02001323 * since FSI is using the scheduler work queue.
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001324 * Therefore, in capture case, probably FSI FIFO will have got
1325 * overflow error in this point.
1326 * in that case, DMA cannot start transfer until error was cleared.
1327 */
1328 if (!is_play) {
1329 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1330 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1331 fsi_reg_write(fsi, DIFF_ST, 0);
1332 }
1333 }
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001334
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001335 ret = 0;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001336
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001337fsi_dma_transfer_err:
1338 return ret;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001339}
1340
Kuninori Morimotoc375b2d2013-05-28 00:55:12 -07001341static int fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001342 int start)
1343{
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001344 struct fsi_master *master = fsi_get_master(fsi);
1345 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001346 u32 enable = start ? DMA_ON : 0;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001347
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001348 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001349
Kuninori Morimotofbe42f62012-05-28 23:28:22 -07001350 dmaengine_terminate_all(io->chan);
1351
Kuninori Morimotoe42bb9b2012-05-24 23:55:11 -07001352 if (fsi_is_clk_master(fsi))
1353 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
Kuninori Morimotoc375b2d2013-05-28 00:55:12 -07001354
1355 return 0;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001356}
1357
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001358static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev)
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001359{
Kuninori Morimotoa0732782013-12-10 20:46:59 -08001360 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001361
Arnd Bergmanne6e969f2015-11-18 22:16:48 +01001362#ifdef CONFIG_SUPERH
1363 dma_cap_mask_t mask;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001364 dma_cap_zero(mask);
1365 dma_cap_set(DMA_SLAVE, mask);
1366
Arnd Bergmanne6e969f2015-11-18 22:16:48 +01001367 io->chan = dma_request_channel(mask, shdma_chan_filter,
1368 (void *)io->dma_id);
1369#else
1370 io->chan = dma_request_slave_channel(dev, is_play ? "tx" : "rx");
1371#endif
Kuninori Morimotoa0732782013-12-10 20:46:59 -08001372 if (io->chan) {
Kuninori Morimoto5b7cdc82015-02-17 01:48:08 +00001373 struct dma_slave_config cfg = {};
Kuninori Morimotoa0732782013-12-10 20:46:59 -08001374 int ret;
1375
Kuninori Morimoto7c6cc8f2015-02-17 01:48:19 +00001376 if (is_play) {
1377 cfg.dst_addr = fsi->phys + REG_DODT;
1378 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1379 cfg.direction = DMA_MEM_TO_DEV;
1380 } else {
1381 cfg.src_addr = fsi->phys + REG_DIDT;
1382 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1383 cfg.direction = DMA_DEV_TO_MEM;
1384 }
Kuninori Morimotoa0732782013-12-10 20:46:59 -08001385
1386 ret = dmaengine_slave_config(io->chan, &cfg);
1387 if (ret < 0) {
1388 dma_release_channel(io->chan);
1389 io->chan = NULL;
1390 }
1391 }
1392
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001393 if (!io->chan) {
1394
1395 /* switch to PIO handler */
Kuninori Morimotoa0732782013-12-10 20:46:59 -08001396 if (is_play)
Kuninori Morimotob1226dc2012-05-24 23:56:19 -07001397 fsi->playback.handler = &fsi_pio_push_handler;
1398 else
1399 fsi->capture.handler = &fsi_pio_pop_handler;
1400
1401 dev_info(dev, "switch handler (dma => pio)\n");
1402
1403 /* probe again */
1404 return fsi_stream_probe(fsi, dev);
1405 }
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001406
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001407 return 0;
1408}
1409
1410static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1411{
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001412 fsi_stream_stop(fsi, io);
1413
1414 if (io->chan)
1415 dma_release_channel(io->chan);
1416
1417 io->chan = NULL;
1418 return 0;
1419}
1420
1421static struct fsi_stream_handler fsi_dma_push_handler = {
1422 .init = fsi_dma_init,
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001423 .probe = fsi_dma_probe,
1424 .transfer = fsi_dma_transfer,
1425 .remove = fsi_dma_remove,
1426 .start_stop = fsi_dma_push_start_stop,
1427};
1428
1429/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001430 * dai ops
1431 */
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001432static void fsi_fifo_init(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001433 struct fsi_stream *io,
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001434 struct device *dev)
1435{
1436 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001437 int is_play = fsi_stream_is_play(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001438 u32 shift, i;
1439 int frame_capa;
1440
1441 /* get on-chip RAM capacity */
1442 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001443 shift >>= fsi_get_port_shift(fsi, io);
Kuninori Morimotob49e8022012-02-03 00:51:29 -08001444 shift &= FIFO_SZ_MASK;
1445 frame_capa = 256 << shift;
1446 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1447
1448 /*
1449 * The maximum number of sample data varies depending
1450 * on the number of channels selected for the format.
1451 *
1452 * FIFOs are used in 4-channel units in 3-channel mode
1453 * and in 8-channel units in 5- to 7-channel mode
1454 * meaning that more FIFOs than the required size of DPRAM
1455 * are used.
1456 *
1457 * ex) if 256 words of DP-RAM is connected
1458 * 1 channel: 256 (256 x 1 = 256)
1459 * 2 channels: 128 (128 x 2 = 256)
1460 * 3 channels: 64 ( 64 x 3 = 192)
1461 * 4 channels: 64 ( 64 x 4 = 256)
1462 * 5 channels: 32 ( 32 x 5 = 160)
1463 * 6 channels: 32 ( 32 x 6 = 192)
1464 * 7 channels: 32 ( 32 x 7 = 224)
1465 * 8 channels: 32 ( 32 x 8 = 256)
1466 */
1467 for (i = 1; i < fsi->chan_num; i <<= 1)
1468 frame_capa >>= 1;
1469 dev_dbg(dev, "%d channel %d store\n",
1470 fsi->chan_num, frame_capa);
1471
1472 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1473
1474 /*
1475 * set interrupt generation factor
1476 * clear FIFO
1477 */
1478 if (is_play) {
1479 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1480 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1481 } else {
1482 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1483 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1484 }
1485}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001486
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001487static int fsi_hw_startup(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001488 struct fsi_stream *io,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001489 struct device *dev)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001490{
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001491 u32 data = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001492
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001493 /* clock setting */
1494 if (fsi_is_clk_master(fsi))
1495 data = DIMD | DOMD;
1496
1497 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001498
1499 /* clock inversion (CKG2) */
1500 data = 0;
Kuninori Morimoto3449f5f2012-11-16 01:17:43 -08001501 if (fsi->bit_clk_inv)
1502 data |= (1 << 0);
1503 if (fsi->lr_clk_inv)
1504 data |= (1 << 4);
1505 if (fsi_is_clk_master(fsi))
1506 data <<= 8;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001507 fsi_reg_write(fsi, CKG2, data);
1508
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001509 /* spdif ? */
1510 if (fsi_is_spdif(fsi)) {
1511 fsi_spdif_clk_ctrl(fsi, 1);
1512 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1513 }
1514
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001515 /*
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001516 * get bus settings
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001517 */
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001518 data = 0;
1519 switch (io->sample_width) {
1520 case 2:
1521 data = BUSOP_GET(16, io->bus_option);
1522 break;
1523 case 4:
1524 data = BUSOP_GET(24, io->bus_option);
1525 break;
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001526 }
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001527 fsi_format_bus_setup(fsi, io, data, dev);
Kuninori Morimoto65ff03f2011-11-06 22:05:25 -08001528
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001529 /* irq clear */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001530 fsi_irq_disable(fsi, io);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +09001531 fsi_irq_clear_status(fsi);
1532
1533 /* fifo init */
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001534 fsi_fifo_init(fsi, io, dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001535
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001536 /* start master clock */
1537 if (fsi_is_clk_master(fsi))
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -08001538 return fsi_clk_enable(dev, fsi);
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001539
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +09001540 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001541}
1542
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001543static int fsi_hw_shutdown(struct fsi_priv *fsi,
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001544 struct device *dev)
1545{
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001546 /* stop master clock */
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001547 if (fsi_is_clk_master(fsi))
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -08001548 return fsi_clk_disable(dev, fsi);
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001549
1550 return 0;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001551}
1552
1553static int fsi_dai_startup(struct snd_pcm_substream *substream,
1554 struct snd_soc_dai *dai)
1555{
1556 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001557
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001558 fsi_clk_invalid(fsi);
Kuninori Morimotof33238e2012-05-17 17:35:34 -07001559
1560 return 0;
Kuninori Morimoto23ca8532011-05-23 20:46:26 +09001561}
1562
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001563static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1564 struct snd_soc_dai *dai)
1565{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001566 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001567
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001568 fsi_clk_invalid(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001569}
1570
1571static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1572 struct snd_soc_dai *dai)
1573{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001574 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001575 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001576 int ret = 0;
1577
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001578 switch (cmd) {
1579 case SNDRV_PCM_TRIGGER_START:
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001580 fsi_stream_init(fsi, io, substream);
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001581 if (!ret)
1582 ret = fsi_hw_startup(fsi, io, dai->dev);
1583 if (!ret)
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001584 ret = fsi_stream_start(fsi, io);
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001585 if (!ret)
Kuninori Morimoto8457e0e2014-06-22 17:55:59 -07001586 ret = fsi_stream_transfer(io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001587 break;
1588 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto80b4add2012-10-30 19:59:15 -07001589 if (!ret)
1590 ret = fsi_hw_shutdown(fsi, dai->dev);
Kuninori Morimoto180346e2012-02-03 00:57:25 -08001591 fsi_stream_stop(fsi, io);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001592 fsi_stream_quit(fsi, io);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001593 break;
1594 }
1595
1596 return ret;
1597}
1598
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001599static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1600{
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001601 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1602 case SND_SOC_DAIFMT_I2S:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001603 fsi->fmt = CR_I2S;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001604 fsi->chan_num = 2;
1605 break;
1606 case SND_SOC_DAIFMT_LEFT_J:
Kuninori Morimoto9c59dd32012-05-17 17:34:53 -07001607 fsi->fmt = CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001608 fsi->chan_num = 2;
1609 break;
1610 default:
1611 return -EINVAL;
1612 }
1613
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001614 return 0;
1615}
1616
1617static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1618{
1619 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001620
Kuninori Morimoto284c6f62012-05-17 17:34:16 -07001621 if (fsi_version(master) < 2)
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001622 return -EINVAL;
1623
Kuninori Morimoto766812e2012-05-17 17:36:47 -07001624 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001625 fsi->chan_num = 2;
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001626
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001627 return 0;
1628}
1629
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001630static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1631{
1632 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001633 int ret;
1634
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001635 /* set master/slave audio interface */
1636 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1637 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001638 break;
1639 case SND_SOC_DAIFMT_CBS_CFS:
Kuninori Morimotoc7a507e2014-03-13 17:56:25 -07001640 fsi->clk_master = 1; /* codec is slave, cpu is master */
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001641 break;
1642 default:
Kuninori Morimoto9478e0b2011-05-23 20:46:07 +09001643 return -EINVAL;
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001644 }
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001645
Kuninori Morimoto3449f5f2012-11-16 01:17:43 -08001646 /* set clock inversion */
1647 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1648 case SND_SOC_DAIFMT_NB_IF:
1649 fsi->bit_clk_inv = 0;
1650 fsi->lr_clk_inv = 1;
1651 break;
1652 case SND_SOC_DAIFMT_IB_NF:
1653 fsi->bit_clk_inv = 1;
1654 fsi->lr_clk_inv = 0;
1655 break;
1656 case SND_SOC_DAIFMT_IB_IF:
1657 fsi->bit_clk_inv = 1;
1658 fsi->lr_clk_inv = 1;
1659 break;
1660 case SND_SOC_DAIFMT_NB_NF:
1661 default:
1662 fsi->bit_clk_inv = 0;
1663 fsi->lr_clk_inv = 0;
1664 break;
1665 }
1666
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001667 if (fsi_is_clk_master(fsi)) {
Kuninori Morimotoab6340c2012-11-16 01:17:18 -08001668 if (fsi->clk_cpg)
Kuninori Morimotoab6f6d82012-11-05 18:30:38 -08001669 fsi_clk_init(dai->dev, fsi, 0, 1, 1,
1670 fsi_clk_set_rate_cpg);
Kuninori Morimotoab6340c2012-11-16 01:17:18 -08001671 else
1672 fsi_clk_init(dai->dev, fsi, 1, 1, 0,
1673 fsi_clk_set_rate_external);
Kuninori Morimoto6a9ebad2011-04-21 10:33:36 +09001674 }
1675
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001676 /* set format */
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001677 if (fsi_is_spdif(fsi))
Kuninori Morimotof17c13c2011-01-24 10:43:19 +09001678 ret = fsi_set_fmt_spdif(fsi);
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001679 else
1680 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001681
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001682 return ret;
1683}
1684
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001685static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1686 struct snd_pcm_hw_params *params,
1687 struct snd_soc_dai *dai)
1688{
1689 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001690
Kuninori Morimoto6cbdbff2012-12-16 22:12:21 -08001691 if (fsi_is_clk_master(fsi))
1692 fsi_clk_valid(fsi, params_rate(params));
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001693
Kuninori Morimotoddeb2d72012-10-29 00:37:22 -07001694 return 0;
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001695}
1696
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001697static const struct snd_soc_dai_ops fsi_dai_ops = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001698 .startup = fsi_dai_startup,
1699 .shutdown = fsi_dai_shutdown,
1700 .trigger = fsi_dai_trigger,
Kuninori Morimoto4d805f72011-01-20 11:46:02 +09001701 .set_fmt = fsi_dai_set_fmt,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +09001702 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001703};
1704
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001705/*
1706 * pcm ops
1707 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001708
Bhumika Goyal5c2e0352017-08-17 15:46:11 +05301709static const struct snd_pcm_hardware fsi_pcm_hardware = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001710 .info = SNDRV_PCM_INFO_INTERLEAVED |
1711 SNDRV_PCM_INFO_MMAP |
Kuninori Morimotoc1b9b9b2014-10-28 21:01:53 -07001712 SNDRV_PCM_INFO_MMAP_VALID,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001713 .buffer_bytes_max = 64 * 1024,
1714 .period_bytes_min = 32,
1715 .period_bytes_max = 8192,
1716 .periods_min = 1,
1717 .periods_max = 32,
1718 .fifo_size = 256,
1719};
1720
Kuninori Morimotobe513042019-10-02 14:33:06 +09001721static int fsi_pcm_open(struct snd_soc_component *component,
1722 struct snd_pcm_substream *substream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001723{
1724 struct snd_pcm_runtime *runtime = substream->runtime;
1725 int ret = 0;
1726
1727 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1728
1729 ret = snd_pcm_hw_constraint_integer(runtime,
1730 SNDRV_PCM_HW_PARAM_PERIODS);
1731
1732 return ret;
1733}
1734
Kuninori Morimotobe513042019-10-02 14:33:06 +09001735static snd_pcm_uframes_t fsi_pointer(struct snd_soc_component *component,
1736 struct snd_pcm_substream *substream)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001737{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001738 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08001739 struct fsi_stream *io = fsi_stream_get(fsi, substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001740
Kuninori Morimoto19878772012-02-08 16:57:29 -08001741 return fsi_sample2frame(fsi, io->buff_sample_pos);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001742}
1743
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001744/*
Kuninori Morimotof5a82fa2018-01-29 02:43:26 +00001745 * snd_soc_component
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001746 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001747
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001748#define PREALLOC_BUFFER (32 * 1024)
1749#define PREALLOC_BUFFER_MAX (32 * 1024)
1750
Kuninori Morimotobe513042019-10-02 14:33:06 +09001751static int fsi_pcm_new(struct snd_soc_component *component,
1752 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001753{
Takashi Iwai55df47f2019-12-10 15:25:58 +01001754 snd_pcm_set_managed_buffer_all(
Kuninori Morimotoffb83e82014-06-22 17:55:18 -07001755 rtd->pcm,
1756 SNDRV_DMA_TYPE_DEV,
1757 rtd->card->snd_card->dev,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001758 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
Takashi Iwaib76c7fe2019-02-04 16:39:54 +01001759 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001760}
1761
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001762/*
1763 * alsa struct
1764 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001765
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001767 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001769 .playback = {
1770 .rates = FSI_RATES,
1771 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001772 .channels_min = 2,
1773 .channels_max = 2,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001774 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001775 .capture = {
1776 .rates = FSI_RATES,
1777 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001778 .channels_min = 2,
1779 .channels_max = 2,
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001780 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001781 .ops = &fsi_dai_ops,
1782 },
1783 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001784 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001785 .playback = {
1786 .rates = FSI_RATES,
1787 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001788 .channels_min = 2,
1789 .channels_max = 2,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001790 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001791 .capture = {
1792 .rates = FSI_RATES,
1793 .formats = FSI_FMTS,
Kuninori Morimoto2a8c8a52012-10-02 19:08:53 -07001794 .channels_min = 2,
1795 .channels_max = 2,
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001796 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001797 .ops = &fsi_dai_ops,
1798 },
1799};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001800
Kuninori Morimotoda4f2f92013-03-14 00:19:20 -07001801static const struct snd_soc_component_driver fsi_soc_component = {
1802 .name = "fsi",
Kuninori Morimotobe513042019-10-02 14:33:06 +09001803 .open = fsi_pcm_open,
Kuninori Morimotobe513042019-10-02 14:33:06 +09001804 .pointer = fsi_pointer,
1805 .pcm_construct = fsi_pcm_new,
Kuninori Morimotoda4f2f92013-03-14 00:19:20 -07001806};
1807
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001808/*
1809 * platform function
1810 */
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08001811static void fsi_of_parse(char *name,
1812 struct device_node *np,
1813 struct sh_fsi_port_info *info,
1814 struct device *dev)
1815{
1816 int i;
1817 char prop[128];
1818 unsigned long flags = 0;
1819 struct {
1820 char *name;
1821 unsigned int val;
1822 } of_parse_property[] = {
1823 { "spdif-connection", SH_FSI_FMT_SPDIF },
1824 { "stream-mode-support", SH_FSI_ENABLE_STREAM_MODE },
1825 { "use-internal-clock", SH_FSI_CLK_CPG },
1826 };
1827
1828 for (i = 0; i < ARRAY_SIZE(of_parse_property); i++) {
1829 sprintf(prop, "%s,%s", name, of_parse_property[i].name);
1830 if (of_get_property(np, prop, NULL))
1831 flags |= of_parse_property[i].val;
1832 }
1833 info->flags = flags;
1834
1835 dev_dbg(dev, "%s flags : %lx\n", name, info->flags);
1836}
1837
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001838static void fsi_port_info_init(struct fsi_priv *fsi,
1839 struct sh_fsi_port_info *info)
1840{
1841 if (info->flags & SH_FSI_FMT_SPDIF)
1842 fsi->spdif = 1;
Kuninori Morimotoab6340c2012-11-16 01:17:18 -08001843
1844 if (info->flags & SH_FSI_CLK_CPG)
1845 fsi->clk_cpg = 1;
Kuninori Morimoto2522acd2012-11-16 01:17:30 -08001846
1847 if (info->flags & SH_FSI_ENABLE_STREAM_MODE)
1848 fsi->enable_stream = 1;
Kuninori Morimotoc2052de2012-11-16 01:17:06 -08001849}
1850
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08001851static void fsi_handler_init(struct fsi_priv *fsi,
1852 struct sh_fsi_port_info *info)
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001853{
1854 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1855 fsi->playback.priv = fsi;
1856 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1857 fsi->capture.priv = fsi;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001858
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08001859 if (info->tx_id) {
Kuninori Morimotoa0732782013-12-10 20:46:59 -08001860 fsi->playback.dma_id = info->tx_id;
Guennadi Liakhovetskib8373142012-05-09 17:09:20 +02001861 fsi->playback.handler = &fsi_dma_push_handler;
Kuninori Morimoto7da9ced2012-02-03 00:59:33 -08001862 }
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001863}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001864
Uwe Kleine-König9a42ab02015-03-31 16:48:57 +02001865static const struct fsi_core fsi1_core = {
Uwe Kleine-König3b6281c2015-03-31 16:48:56 +02001866 .ver = 1,
1867
1868 /* Interrupt */
1869 .int_st = INT_ST,
1870 .iemsk = IEMSK,
1871 .imsk = IMSK,
1872};
1873
Uwe Kleine-König9a42ab02015-03-31 16:48:57 +02001874static const struct fsi_core fsi2_core = {
Uwe Kleine-König3b6281c2015-03-31 16:48:56 +02001875 .ver = 2,
1876
1877 /* Interrupt */
1878 .int_st = CPU_INT_ST,
1879 .iemsk = CPU_IEMSK,
1880 .imsk = CPU_IMSK,
1881 .a_mclk = A_MST_CTLR,
1882 .b_mclk = B_MST_CTLR,
1883};
1884
1885static const struct of_device_id fsi_of_match[] = {
1886 { .compatible = "renesas,sh_fsi", .data = &fsi1_core},
1887 { .compatible = "renesas,sh_fsi2", .data = &fsi2_core},
1888 {},
1889};
1890MODULE_DEVICE_TABLE(of, fsi_of_match);
1891
Uwe Kleine-König9a42ab02015-03-31 16:48:57 +02001892static const struct platform_device_id fsi_id_table[] = {
Uwe Kleine-König3b6281c2015-03-31 16:48:56 +02001893 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
Uwe Kleine-König3b6281c2015-03-31 16:48:56 +02001894 {},
1895};
1896MODULE_DEVICE_TABLE(platform, fsi_id_table);
1897
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001898static int fsi_probe(struct platform_device *pdev)
1899{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001900 struct fsi_master *master;
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08001901 struct device_node *np = pdev->dev.of_node;
Kuninori Morimotofd974e52012-12-27 19:15:08 -08001902 struct sh_fsi_platform_info info;
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08001903 const struct fsi_core *core;
Kuninori Morimoto40f91182012-11-16 01:16:22 -08001904 struct fsi_priv *fsi;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001905 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001906 unsigned int irq;
1907 int ret;
1908
Kuninori Morimotofd974e52012-12-27 19:15:08 -08001909 memset(&info, 0, sizeof(info));
Kuninori Morimoto943fdad2012-11-16 01:16:52 -08001910
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08001911 core = NULL;
1912 if (np) {
Geert Uytterhoevenb48cc1d2017-10-04 14:28:30 +02001913 core = of_device_get_match_data(&pdev->dev);
1914 fsi_of_parse("fsia", np, &info.port_a, &pdev->dev);
1915 fsi_of_parse("fsib", np, &info.port_b, &pdev->dev);
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08001916 } else {
1917 const struct platform_device_id *id_entry = pdev->id_entry;
1918 if (id_entry)
1919 core = (struct fsi_core *)id_entry->driver_data;
1920
1921 if (pdev->dev.platform_data)
1922 memcpy(&info, pdev->dev.platform_data, sizeof(info));
1923 }
1924
1925 if (!core) {
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001926 dev_err(&pdev->dev, "unknown fsi device\n");
1927 return -ENODEV;
1928 }
1929
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001930 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1931 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001932 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001933 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001934 return -ENODEV;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001935 }
1936
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001937 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
Markus Elfringdd806272017-08-10 18:18:20 +02001938 if (!master)
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001939 return -ENOMEM;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001940
Geert Uytterhoeven82dabf52020-02-12 09:50:08 +01001941 master->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001942 if (!master->base) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001943 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001944 return -ENXIO;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001945 }
1946
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001947 /* master setting */
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08001948 master->core = core;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001949 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001950
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001951 /* FSI A setting */
Kuninori Morimoto40f91182012-11-16 01:16:22 -08001952 fsi = &master->fsia;
1953 fsi->base = master->base;
Kuninori Morimoto7c6cc8f2015-02-17 01:48:19 +00001954 fsi->phys = res->start;
Kuninori Morimoto40f91182012-11-16 01:16:22 -08001955 fsi->master = master;
Kuninori Morimotofd974e52012-12-27 19:15:08 -08001956 fsi_port_info_init(fsi, &info.port_a);
1957 fsi_handler_init(fsi, &info.port_a);
Kuninori Morimoto40f91182012-11-16 01:16:22 -08001958 ret = fsi_stream_probe(fsi, &pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001959 if (ret < 0) {
1960 dev_err(&pdev->dev, "FSIA stream probe failed\n");
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07001961 return ret;
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001962 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001963
1964 /* FSI B setting */
Kuninori Morimoto40f91182012-11-16 01:16:22 -08001965 fsi = &master->fsib;
1966 fsi->base = master->base + 0x40;
Kuninori Morimoto7c6cc8f2015-02-17 01:48:19 +00001967 fsi->phys = res->start + 0x40;
Kuninori Morimoto40f91182012-11-16 01:16:22 -08001968 fsi->master = master;
Kuninori Morimotofd974e52012-12-27 19:15:08 -08001969 fsi_port_info_init(fsi, &info.port_b);
1970 fsi_handler_init(fsi, &info.port_b);
Kuninori Morimoto40f91182012-11-16 01:16:22 -08001971 ret = fsi_stream_probe(fsi, &pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001972 if (ret < 0) {
1973 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1974 goto exit_fsia;
1975 }
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001976
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001977 pm_runtime_enable(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001978 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001979
Kuninori Morimoto1ddd8282012-10-02 23:22:57 -07001980 ret = devm_request_irq(&pdev->dev, irq, &fsi_interrupt, 0,
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08001981 dev_name(&pdev->dev), master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001982 if (ret) {
1983 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001984 goto exit_fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001985 }
1986
Kuninori Morimotof5a82fa2018-01-29 02:43:26 +00001987 ret = devm_snd_soc_register_component(&pdev->dev, &fsi_soc_component,
Kuninori Morimotoda4f2f92013-03-14 00:19:20 -07001988 fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001989 if (ret < 0) {
Kuninori Morimotoda4f2f92013-03-14 00:19:20 -07001990 dev_err(&pdev->dev, "cannot snd component register\n");
Kuninori Morimotof5a82fa2018-01-29 02:43:26 +00001991 goto exit_fsib;
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001992 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001993
Kuninori Morimoto0b5ec872011-04-08 15:09:02 +09001994 return ret;
1995
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001996exit_fsib:
Kuninori Morimotoc35e0052012-09-10 02:13:52 -07001997 pm_runtime_disable(&pdev->dev);
Kuninori Morimoto5e973132012-02-03 00:55:55 -08001998 fsi_stream_remove(&master->fsib);
1999exit_fsia:
2000 fsi_stream_remove(&master->fsia);
Kuninori Morimoto6ac42622012-09-10 02:14:31 -07002001
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002002 return ret;
2003}
2004
2005static int fsi_remove(struct platform_device *pdev)
2006{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09002007 struct fsi_master *master;
2008
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002009 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09002010
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002011 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002012
Kuninori Morimoto5e973132012-02-03 00:55:55 -08002013 fsi_stream_remove(&master->fsia);
2014 fsi_stream_remove(&master->fsib);
2015
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002016 return 0;
2017}
2018
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002019static void __fsi_suspend(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002020 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09002021 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002022{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002023 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002024 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002025
Kuninori Morimoto180346e2012-02-03 00:57:25 -08002026 fsi_stream_stop(fsi, io);
Kuninori Morimoto41bba152012-02-03 00:51:53 -08002027 fsi_hw_shutdown(fsi, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002028}
2029
2030static void __fsi_resume(struct fsi_priv *fsi,
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002031 struct fsi_stream *io,
Kuninori Morimoto4f56cde2011-05-23 20:46:18 +09002032 struct device *dev)
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002033{
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002034 if (!fsi_stream_is_working(fsi, io))
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002035 return;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002036
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002037 fsi_hw_startup(fsi, io, dev);
Kuninori Morimoto180346e2012-02-03 00:57:25 -08002038 fsi_stream_start(fsi, io);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002039}
2040
2041static int fsi_suspend(struct device *dev)
2042{
2043 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002044 struct fsi_priv *fsia = &master->fsia;
2045 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002046
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002047 __fsi_suspend(fsia, &fsia->playback, dev);
2048 __fsi_suspend(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002049
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002050 __fsi_suspend(fsib, &fsib->playback, dev);
2051 __fsi_suspend(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002052
2053 return 0;
2054}
2055
2056static int fsi_resume(struct device *dev)
2057{
2058 struct fsi_master *master = dev_get_drvdata(dev);
Kuninori Morimotocda828c2011-05-23 20:46:35 +09002059 struct fsi_priv *fsia = &master->fsia;
2060 struct fsi_priv *fsib = &master->fsib;
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002061
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002062 __fsi_resume(fsia, &fsia->playback, dev);
2063 __fsi_resume(fsia, &fsia->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002064
Kuninori Morimoto938e2a82012-02-03 00:56:57 -08002065 __fsi_resume(fsib, &fsib->playback, dev);
2066 __fsi_resume(fsib, &fsib->capture, dev);
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002067
2068 return 0;
2069}
2070
Gustavo A. R. Silva4b2983d2017-07-16 21:12:21 -05002071static const struct dev_pm_ops fsi_pm_ops = {
Kuninori Morimoto106c79e2011-04-21 10:33:47 +09002072 .suspend = fsi_suspend,
2073 .resume = fsi_resume,
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002074};
2075
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002076static struct platform_driver fsi_driver = {
2077 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002078 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09002079 .pm = &fsi_pm_ops,
Kuninori Morimoto9e7b6d62013-01-10 00:34:08 -08002080 .of_match_table = fsi_of_match,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002081 },
2082 .probe = fsi_probe,
2083 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09002084 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002085};
2086
Axel Lincb5e8732011-11-25 10:15:07 +08002087module_platform_driver(fsi_driver);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002088
Uwe Kleine-König1c6ae562015-03-29 21:47:16 +02002089MODULE_LICENSE("GPL v2");
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09002090MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
2091MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Guennadi Liakhovetskib3c27b52011-04-15 20:17:34 +02002092MODULE_ALIAS("platform:fsi-pcm-audio");