Merge remote-tracking branch 'asoc/fix/wm8978' into asoc-linus
[deliverable/linux.git] / sound / soc / sh / fsi.c
CommitLineData
a4d7d550
KM
1/*
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
a4d7d550 15#include <linux/delay.h>
7da9ced6 16#include <linux/dma-mapping.h>
785d1c45 17#include <linux/pm_runtime.h>
a4d7d550 18#include <linux/io.h>
9e7b6d60
KM
19#include <linux/of.h>
20#include <linux/of_device.h>
7da9ced6
KM
21#include <linux/scatterlist.h>
22#include <linux/sh_dma.h>
5a0e3ad6 23#include <linux/slab.h>
da155d5b 24#include <linux/module.h>
57451e43 25#include <linux/workqueue.h>
a4d7d550 26#include <sound/soc.h>
ab6f6d85 27#include <sound/pcm_params.h>
a4d7d550 28#include <sound/sh_fsi.h>
a4d7d550 29
e8c8b631
KM
30/* PortA/PortB register */
31#define REG_DO_FMT 0x0000
32#define REG_DOFF_CTL 0x0004
33#define REG_DOFF_ST 0x0008
34#define REG_DI_FMT 0x000C
35#define REG_DIFF_CTL 0x0010
36#define REG_DIFF_ST 0x0014
37#define REG_CKG1 0x0018
38#define REG_CKG2 0x001C
39#define REG_DIDT 0x0020
40#define REG_DODT 0x0024
41#define REG_MUTE_ST 0x0028
65ff03f4 42#define REG_OUT_DMAC 0x002C
e8c8b631 43#define REG_OUT_SEL 0x0030
65ff03f4 44#define REG_IN_DMAC 0x0038
cc780d38 45
43fa95ca
KM
46/* master register */
47#define MST_CLK_RST 0x0210
48#define MST_SOFT_RST 0x0214
49#define MST_FIFO_SZ 0x0218
50
51/* core register (depend on FSI version) */
3bc28070
KM
52#define A_MST_CTLR 0x0180
53#define B_MST_CTLR 0x01A0
cc780d38
KM
54#define CPU_INT_ST 0x01F4
55#define CPU_IEMSK 0x01F8
56#define CPU_IMSK 0x01FC
a4d7d550
KM
57#define INT_ST 0x0200
58#define IEMSK 0x0204
59#define IMSK 0x0208
a4d7d550
KM
60
61/* DO_FMT */
62/* DI_FMT */
7da9ced6 63#define CR_BWS_MASK (0x3 << 20) /* FSI2 */
f7d711e3
KM
64#define CR_BWS_24 (0x0 << 20) /* FSI2 */
65#define CR_BWS_16 (0x1 << 20) /* FSI2 */
66#define CR_BWS_20 (0x2 << 20) /* FSI2 */
67
68#define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
69#define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
70#define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
71
a7ffb52b
KM
72#define CR_MONO (0x0 << 4)
73#define CR_MONO_D (0x1 << 4)
74#define CR_PCM (0x2 << 4)
75#define CR_I2S (0x3 << 4)
76#define CR_TDM (0x4 << 4)
77#define CR_TDM_D (0x5 << 4)
a4d7d550 78
7da9ced6
KM
79/* OUT_DMAC */
80/* IN_DMAC */
81#define VDMD_MASK (0x3 << 4)
82#define VDMD_FRONT (0x0 << 4) /* Package in front */
83#define VDMD_BACK (0x1 << 4) /* Package in back */
84#define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
85
86#define DMA_ON (0x1 << 0)
87
a4d7d550
KM
88/* DOFF_CTL */
89/* DIFF_CTL */
90#define IRQ_HALF 0x00100000
91#define FIFO_CLR 0x00000001
92
93/* DOFF_ST */
94#define ERR_OVER 0x00000010
95#define ERR_UNDER 0x00000001
59c3b003 96#define ST_ERR (ERR_OVER | ERR_UNDER)
a4d7d550 97
ccad7b44
KM
98/* CKG1 */
99#define ACKMD_MASK 0x00007000
100#define BPFMD_MASK 0x00000700
4d805f7b
KM
101#define DIMD (1 << 4)
102#define DOMD (1 << 0)
ccad7b44 103
3bc28070
KM
104/* A/B MST_CTLR */
105#define BP (1 << 4) /* Fix the signal of Biphase output */
106#define SE (1 << 0) /* Fix the master clock */
107
a4d7d550 108/* CLK_RST */
1f5e2a31
KM
109#define CRB (1 << 4)
110#define CRA (1 << 0)
a4d7d550 111
cf6edd00
KM
112/* IO SHIFT / MACRO */
113#define BI_SHIFT 12
114#define BO_SHIFT 8
115#define AI_SHIFT 4
116#define AO_SHIFT 0
117#define AB_IO(param, shift) (param << shift)
a4d7d550 118
feb58cff
KM
119/* SOFT_RST */
120#define PBSR (1 << 12) /* Port B Software Reset */
121#define PASR (1 << 8) /* Port A Software Reset */
122#define IR (1 << 4) /* Interrupt Reset */
123#define FSISR (1 << 0) /* Software Reset */
124
f7d711e3
KM
125/* OUT_SEL (FSI2) */
126#define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
127 /* 1: Biphase and serial */
128
4a942b45 129/* FIFO_SZ */
cf6edd00 130#define FIFO_SZ_MASK 0x7
4a942b45 131
a4d7d550
KM
132#define FSI_RATES SNDRV_PCM_RATE_8000_96000
133
134#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
135
766812e6
KM
136/*
137 * bus options
138 *
139 * 0x000000BA
140 *
141 * A : sample widtht 16bit setting
142 * B : sample widtht 24bit setting
143 */
144
145#define SHIFT_16DATA 0
146#define SHIFT_24DATA 4
147
148#define PACKAGE_24BITBUS_BACK 0
149#define PACKAGE_24BITBUS_FRONT 1
150#define PACKAGE_16BITBUS_STREAM 2
151
152#define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
153#define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
154
5bfb9ad0
KM
155/*
156 * FSI driver use below type name for variable
157 *
5bfb9ad0 158 * xxx_num : number of data
2e651baf
KM
159 * xxx_pos : position of data
160 * xxx_capa : capacity of data
161 */
162
163/*
164 * period/frame/sample image
165 *
166 * ex) PCM (2ch)
167 *
168 * period pos period pos
169 * [n] [n + 1]
170 * |<-------------------- period--------------------->|
171 * ==|============================================ ... =|==
172 * | |
173 * ||<----- frame ----->|<------ frame ----->| ... |
174 * |+--------------------+--------------------+- ... |
175 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
176 * |+--------------------+--------------------+- ... |
177 * ==|============================================ ... =|==
178 */
179
180/*
181 * FSI FIFO image
182 *
183 * | |
184 * | |
185 * | [ sample ] |
186 * | [ sample ] |
187 * | [ sample ] |
188 * | [ sample ] |
189 * --> go to codecs
5bfb9ad0
KM
190 */
191
ab6f6d85
KM
192/*
193 * FSI clock
194 *
195 * FSIxCLK [CPG] (ick) -------> |
196 * |-> FSI_DIV (div)-> FSI2
197 * FSIxCK [external] (xck) ---> |
198 */
199
c8fe2574
KM
200/*
201 * struct
202 */
a4d7d550 203
5e97313a 204struct fsi_stream_handler;
93193c2b 205struct fsi_stream {
a4d7d550 206
5e97313a
KM
207 /*
208 * these are initialized by fsi_stream_init()
209 */
210 struct snd_pcm_substream *substream;
2e651baf
KM
211 int fifo_sample_capa; /* sample capacity of FSI FIFO */
212 int buff_sample_capa; /* sample capacity of ALSA buffer */
213 int buff_sample_pos; /* sample position of ALSA buffer */
214 int period_samples; /* sample number / 1 period */
215 int period_pos; /* current period position */
c1e6f10e 216 int sample_width; /* sample width */
1ec9bc35
KM
217 int uerr_num;
218 int oerr_num;
5e97313a 219
766812e6
KM
220 /*
221 * bus options
222 */
223 u32 bus_option;
224
5e97313a
KM
225 /*
226 * thse are initialized by fsi_handler_init()
227 */
228 struct fsi_stream_handler *handler;
229 struct fsi_priv *priv;
7da9ced6
KM
230
231 /*
232 * these are for DMAEngine
233 */
234 struct dma_chan *chan;
235 struct sh_dmae_slave slave; /* see fsi_handler_init() */
57451e43 236 struct work_struct work;
7da9ced6 237 dma_addr_t dma;
93193c2b
KM
238};
239
ab6f6d85
KM
240struct fsi_clk {
241 /* see [FSI clock] */
242 struct clk *own;
243 struct clk *xck;
244 struct clk *ick;
245 struct clk *div;
246 int (*set_rate)(struct device *dev,
6cbdbffb 247 struct fsi_priv *fsi);
ab6f6d85
KM
248
249 unsigned long rate;
250 unsigned int count;
251};
252
93193c2b
KM
253struct fsi_priv {
254 void __iomem *base;
255 struct fsi_master *master;
256
257 struct fsi_stream playback;
258 struct fsi_stream capture;
3bc28070 259
ab6f6d85
KM
260 struct fsi_clk clock;
261
9c59dd34 262 u32 fmt;
9478e0b6 263
6a9ebad8
KM
264 int chan_num:16;
265 int clk_master:1;
ab6340c4 266 int clk_cpg:1;
9478e0b6 267 int spdif:1;
2522acd2 268 int enable_stream:1;
3449f5fa
KM
269 int bit_clk_inv:1;
270 int lr_clk_inv:1;
a4d7d550
KM
271};
272
5e97313a 273struct fsi_stream_handler {
83344027
KM
274 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
275 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
b1226dc5 276 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev);
5e97313a
KM
277 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
278 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
c375b2d7 279 int (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
180346ed 280 int enable);
5e97313a
KM
281};
282#define fsi_stream_handler_call(io, func, args...) \
283 (!(io) ? -ENODEV : \
284 !((io)->handler->func) ? 0 : \
285 (io)->handler->func(args))
286
73b92c1f
KM
287struct fsi_core {
288 int ver;
289
cc780d38
KM
290 u32 int_st;
291 u32 iemsk;
292 u32 imsk;
2b0e7302
KM
293 u32 a_mclk;
294 u32 b_mclk;
cc780d38
KM
295};
296
a4d7d550
KM
297struct fsi_master {
298 void __iomem *base;
a4d7d550
KM
299 struct fsi_priv fsia;
300 struct fsi_priv fsib;
9e7b6d60 301 const struct fsi_core *core;
8fc176d5 302 spinlock_t lock;
a4d7d550
KM
303};
304
7b1b3331
KM
305static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
306
c8fe2574
KM
307/*
308 * basic read write function
309 */
a4d7d550 310
ca7aceef 311static void __fsi_reg_write(u32 __iomem *reg, u32 data)
a4d7d550
KM
312{
313 /* valid data area is 24bit */
314 data &= 0x00ffffff;
315
0f69d978 316 __raw_writel(data, reg);
a4d7d550
KM
317}
318
ca7aceef 319static u32 __fsi_reg_read(u32 __iomem *reg)
a4d7d550 320{
0f69d978 321 return __raw_readl(reg);
a4d7d550
KM
322}
323
ca7aceef 324static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
a4d7d550
KM
325{
326 u32 val = __fsi_reg_read(reg);
327
328 val &= ~mask;
329 val |= data & mask;
330
0f69d978 331 __fsi_reg_write(reg, val);
a4d7d550
KM
332}
333
e8c8b631 334#define fsi_reg_write(p, r, d)\
8918b843 335 __fsi_reg_write((p->base + REG_##r), d)
a4d7d550 336
e8c8b631 337#define fsi_reg_read(p, r)\
8918b843 338 __fsi_reg_read((p->base + REG_##r))
a4d7d550 339
e8c8b631 340#define fsi_reg_mask_set(p, r, m, d)\
8918b843 341 __fsi_reg_mask_set((p->base + REG_##r), m, d)
a4d7d550 342
43fa95ca
KM
343#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)
a4d7d550 346{
8fc176d5
KM
347 u32 ret;
348 unsigned long flags;
349
8fc176d5 350 spin_lock_irqsave(&master->lock, flags);
ca7aceef 351 ret = __fsi_reg_read(master->base + reg);
8fc176d5
KM
352 spin_unlock_irqrestore(&master->lock, flags);
353
354 return ret;
a4d7d550
KM
355}
356
43fa95ca
KM
357#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,
71f6e064 360 u32 reg, u32 mask, u32 data)
a4d7d550 361{
8fc176d5
KM
362 unsigned long flags;
363
8fc176d5 364 spin_lock_irqsave(&master->lock, flags);
ca7aceef 365 __fsi_reg_mask_set(master->base + reg, mask, data);
8fc176d5 366 spin_unlock_irqrestore(&master->lock, flags);
a4d7d550
KM
367}
368
c8fe2574
KM
369/*
370 * basic function
371 */
284c6f65
KM
372static int fsi_version(struct fsi_master *master)
373{
374 return master->core->ver;
375}
a4d7d550 376
71f6e064 377static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
a4d7d550 378{
71f6e064 379 return fsi->master;
a4d7d550
KM
380}
381
6a9ebad8
KM
382static int fsi_is_clk_master(struct fsi_priv *fsi)
383{
384 return fsi->clk_master;
385}
386
a4d7d550
KM
387static int fsi_is_port_a(struct fsi_priv *fsi)
388{
71f6e064
KM
389 return fsi->master->base == fsi->base;
390}
a4d7d550 391
9478e0b6
KM
392static int fsi_is_spdif(struct fsi_priv *fsi)
393{
394 return fsi->spdif;
395}
396
2522acd2
KM
397static int fsi_is_enable_stream(struct fsi_priv *fsi)
398{
399 return fsi->enable_stream;
400}
401
a449e467
KM
402static int fsi_is_play(struct snd_pcm_substream *substream)
403{
404 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
405}
406
142e8174 407static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
71f6e064
KM
408{
409 struct snd_soc_pcm_runtime *rtd = substream->private_data;
142e8174 410
f0fba2ad 411 return rtd->cpu_dai;
142e8174
KM
412}
413
0d032c19 414static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
142e8174 415{
f0fba2ad 416 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
a4d7d550 417
f0fba2ad
LG
418 if (dai->id == 0)
419 return &master->fsia;
420 else
421 return &master->fsib;
a4d7d550
KM
422}
423
0d032c19
KM
424static 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
938e2a8d 429static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
a4d7d550 430{
938e2a8d 431 int is_play = fsi_stream_is_play(fsi, io);
a4d7d550 432 int is_porta = fsi_is_port_a(fsi);
cf6edd00 433 u32 shift;
a4d7d550
KM
434
435 if (is_porta)
cf6edd00 436 shift = is_play ? AO_SHIFT : AI_SHIFT;
a4d7d550 437 else
cf6edd00 438 shift = is_play ? BO_SHIFT : BI_SHIFT;
a4d7d550 439
cf6edd00 440 return shift;
a4d7d550
KM
441}
442
2e651baf
KM
443static 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
7b1b3331
KM
453static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
454 struct fsi_stream *io)
4e62d84d 455{
7b1b3331 456 int is_play = fsi_stream_is_play(fsi, io);
4e62d84d
KM
457 u32 status;
458 int frames;
459
460 status = is_play ?
461 fsi_reg_read(fsi, DOFF_ST) :
462 fsi_reg_read(fsi, DIFF_ST);
463
464 frames = 0x1ff & (status >> 8);
465
466 return fsi_frame2sample(fsi, frames);
467}
468
469static 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
490/*
491 * fsi_stream_xx() function
492 */
a449e467
KM
493static inline int fsi_stream_is_play(struct fsi_priv *fsi,
494 struct fsi_stream *io)
4e62d84d 495{
a449e467 496 return &fsi->playback == io;
4e62d84d
KM
497}
498
499static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
938e2a8d 500 struct snd_pcm_substream *substream)
4e62d84d 501{
938e2a8d 502 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
4e62d84d
KM
503}
504
cda828ca 505static int fsi_stream_is_working(struct fsi_priv *fsi,
938e2a8d 506 struct fsi_stream *io)
cda828ca 507{
cda828ca
KM
508 struct fsi_master *master = fsi_get_master(fsi);
509 unsigned long flags;
510 int ret;
511
512 spin_lock_irqsave(&master->lock, flags);
97df8187 513 ret = !!(io->substream && io->substream->runtime);
cda828ca
KM
514 spin_unlock_irqrestore(&master->lock, flags);
515
516 return ret;
517}
518
5e97313a
KM
519static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
520{
521 return io->priv;
522}
523
8c415295 524static void fsi_stream_init(struct fsi_priv *fsi,
938e2a8d 525 struct fsi_stream *io,
0ffe296a 526 struct snd_pcm_substream *substream)
a4d7d550 527{
0ffe296a 528 struct snd_pcm_runtime *runtime = substream->runtime;
2da65892
KM
529 struct fsi_master *master = fsi_get_master(fsi);
530 unsigned long flags;
93193c2b 531
2da65892 532 spin_lock_irqsave(&master->lock, flags);
93193c2b 533 io->substream = substream;
2e651baf
KM
534 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
535 io->buff_sample_pos = 0;
536 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
537 io->period_pos = 0;
c1e6f10e 538 io->sample_width = samples_to_bytes(runtime, 1);
766812e6 539 io->bus_option = 0;
1ec9bc35
KM
540 io->oerr_num = -1; /* ignore 1st err */
541 io->uerr_num = -1; /* ignore 1st err */
83344027 542 fsi_stream_handler_call(io, init, fsi, io);
2da65892 543 spin_unlock_irqrestore(&master->lock, flags);
a4d7d550
KM
544}
545
938e2a8d 546static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
a4d7d550 547{
1ec9bc35 548 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
2da65892
KM
549 struct fsi_master *master = fsi_get_master(fsi);
550 unsigned long flags;
1ec9bc35 551
2da65892 552 spin_lock_irqsave(&master->lock, flags);
1ec9bc35
KM
553
554 if (io->oerr_num > 0)
555 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
556
557 if (io->uerr_num > 0)
558 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
93193c2b 559
83344027 560 fsi_stream_handler_call(io, quit, fsi, io);
93193c2b 561 io->substream = NULL;
2e651baf
KM
562 io->buff_sample_capa = 0;
563 io->buff_sample_pos = 0;
564 io->period_samples = 0;
565 io->period_pos = 0;
c1e6f10e 566 io->sample_width = 0;
766812e6 567 io->bus_option = 0;
1ec9bc35
KM
568 io->oerr_num = 0;
569 io->uerr_num = 0;
2da65892 570 spin_unlock_irqrestore(&master->lock, flags);
a4d7d550
KM
571}
572
5e97313a
KM
573static int fsi_stream_transfer(struct fsi_stream *io)
574{
575 struct fsi_priv *fsi = fsi_stream_to_priv(io);
576 if (!fsi)
577 return -EIO;
578
579 return fsi_stream_handler_call(io, transfer, fsi, io);
580}
581
180346ed
KM
582#define fsi_stream_start(fsi, io)\
583 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
584
585#define fsi_stream_stop(fsi, io)\
586 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
587
b1226dc5 588static int fsi_stream_probe(struct fsi_priv *fsi, struct device *dev)
5e97313a
KM
589{
590 struct fsi_stream *io;
591 int ret1, ret2;
592
593 io = &fsi->playback;
b1226dc5 594 ret1 = fsi_stream_handler_call(io, probe, fsi, io, dev);
5e97313a
KM
595
596 io = &fsi->capture;
b1226dc5 597 ret2 = fsi_stream_handler_call(io, probe, fsi, io, dev);
5e97313a
KM
598
599 if (ret1 < 0)
600 return ret1;
601 if (ret2 < 0)
602 return ret2;
603
604 return 0;
605}
606
607static int fsi_stream_remove(struct fsi_priv *fsi)
608{
609 struct fsi_stream *io;
610 int ret1, ret2;
611
612 io = &fsi->playback;
613 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
614
615 io = &fsi->capture;
616 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
617
618 if (ret1 < 0)
619 return ret1;
620 if (ret2 < 0)
621 return ret2;
622
623 return 0;
624}
625
766812e6
KM
626/*
627 * format/bus/dma setting
628 */
629static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
630 u32 bus, struct device *dev)
631{
632 struct fsi_master *master = fsi_get_master(fsi);
633 int is_play = fsi_stream_is_play(fsi, io);
634 u32 fmt = fsi->fmt;
635
636 if (fsi_version(master) >= 2) {
637 u32 dma = 0;
638
639 /*
640 * FSI2 needs DMA/Bus setting
641 */
642 switch (bus) {
643 case PACKAGE_24BITBUS_FRONT:
644 fmt |= CR_BWS_24;
645 dma |= VDMD_FRONT;
646 dev_dbg(dev, "24bit bus / package in front\n");
647 break;
648 case PACKAGE_16BITBUS_STREAM:
649 fmt |= CR_BWS_16;
650 dma |= VDMD_STREAM;
651 dev_dbg(dev, "16bit bus / stream mode\n");
652 break;
653 case PACKAGE_24BITBUS_BACK:
654 default:
655 fmt |= CR_BWS_24;
656 dma |= VDMD_BACK;
657 dev_dbg(dev, "24bit bus / package in back\n");
658 break;
659 }
660
661 if (is_play)
662 fsi_reg_write(fsi, OUT_DMAC, dma);
663 else
664 fsi_reg_write(fsi, IN_DMAC, dma);
665 }
666
667 if (is_play)
668 fsi_reg_write(fsi, DO_FMT, fmt);
669 else
670 fsi_reg_write(fsi, DI_FMT, fmt);
671}
672
c8fe2574
KM
673/*
674 * irq function
675 */
a4d7d550 676
938e2a8d 677static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
a4d7d550 678{
938e2a8d 679 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
71f6e064 680 struct fsi_master *master = fsi_get_master(fsi);
a4d7d550 681
43fa95ca
KM
682 fsi_core_mask_set(master, imsk, data, data);
683 fsi_core_mask_set(master, iemsk, data, data);
a4d7d550
KM
684}
685
938e2a8d 686static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
a4d7d550 687{
938e2a8d 688 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
71f6e064 689 struct fsi_master *master = fsi_get_master(fsi);
a4d7d550 690
43fa95ca
KM
691 fsi_core_mask_set(master, imsk, data, 0);
692 fsi_core_mask_set(master, iemsk, data, 0);
a4d7d550
KM
693}
694
10ea76cc
KM
695static u32 fsi_irq_get_status(struct fsi_master *master)
696{
43fa95ca 697 return fsi_core_read(master, int_st);
10ea76cc
KM
698}
699
10ea76cc
KM
700static void fsi_irq_clear_status(struct fsi_priv *fsi)
701{
702 u32 data = 0;
703 struct fsi_master *master = fsi_get_master(fsi);
704
938e2a8d
KM
705 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
706 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
10ea76cc
KM
707
708 /* clear interrupt factor */
43fa95ca 709 fsi_core_mask_set(master, int_st, data, 0);
10ea76cc
KM
710}
711
c8fe2574
KM
712/*
713 * SPDIF master clock function
714 *
715 * These functions are used later FSI2
716 */
3bc28070
KM
717static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
718{
719 struct fsi_master *master = fsi_get_master(fsi);
2b0e7302 720 u32 mask, val;
3bc28070 721
2b0e7302
KM
722 mask = BP | SE;
723 val = enable ? mask : 0;
724
725 fsi_is_port_a(fsi) ?
43fa95ca
KM
726 fsi_core_mask_set(master, a_mclk, mask, val) :
727 fsi_core_mask_set(master, b_mclk, mask, val);
3bc28070
KM
728}
729
c8fe2574 730/*
1f5e2a31 731 * clock function
c8fe2574 732 */
ab6f6d85
KM
733static int fsi_clk_init(struct device *dev,
734 struct fsi_priv *fsi,
735 int xck,
736 int ick,
737 int div,
738 int (*set_rate)(struct device *dev,
6cbdbffb 739 struct fsi_priv *fsi))
ab6f6d85
KM
740{
741 struct fsi_clk *clock = &fsi->clock;
742 int is_porta = fsi_is_port_a(fsi);
743
744 clock->xck = NULL;
745 clock->ick = NULL;
746 clock->div = NULL;
747 clock->rate = 0;
748 clock->count = 0;
749 clock->set_rate = set_rate;
750
751 clock->own = devm_clk_get(dev, NULL);
752 if (IS_ERR(clock->own))
753 return -EINVAL;
754
755 /* external clock */
756 if (xck) {
757 clock->xck = devm_clk_get(dev, is_porta ? "xcka" : "xckb");
758 if (IS_ERR(clock->xck)) {
759 dev_err(dev, "can't get xck clock\n");
760 return -EINVAL;
761 }
762 if (clock->xck == clock->own) {
763 dev_err(dev, "cpu doesn't support xck clock\n");
764 return -EINVAL;
765 }
766 }
767
768 /* FSIACLK/FSIBCLK */
769 if (ick) {
770 clock->ick = devm_clk_get(dev, is_porta ? "icka" : "ickb");
771 if (IS_ERR(clock->ick)) {
772 dev_err(dev, "can't get ick clock\n");
773 return -EINVAL;
774 }
775 if (clock->ick == clock->own) {
776 dev_err(dev, "cpu doesn't support ick clock\n");
777 return -EINVAL;
778 }
779 }
780
781 /* FSI-DIV */
782 if (div) {
783 clock->div = devm_clk_get(dev, is_porta ? "diva" : "divb");
784 if (IS_ERR(clock->div)) {
785 dev_err(dev, "can't get div clock\n");
786 return -EINVAL;
787 }
788 if (clock->div == clock->own) {
789 dev_err(dev, "cpu doens't support div clock\n");
790 return -EINVAL;
791 }
792 }
793
794 return 0;
795}
796
797#define fsi_clk_invalid(fsi) fsi_clk_valid(fsi, 0)
798static void fsi_clk_valid(struct fsi_priv *fsi, unsigned long rate)
799{
800 fsi->clock.rate = rate;
801}
802
803static int fsi_clk_is_valid(struct fsi_priv *fsi)
804{
805 return fsi->clock.set_rate &&
806 fsi->clock.rate;
807}
808
809static int fsi_clk_enable(struct device *dev,
6cbdbffb 810 struct fsi_priv *fsi)
ab6f6d85
KM
811{
812 struct fsi_clk *clock = &fsi->clock;
813 int ret = -EINVAL;
814
815 if (!fsi_clk_is_valid(fsi))
816 return ret;
817
818 if (0 == clock->count) {
6cbdbffb 819 ret = clock->set_rate(dev, fsi);
ab6f6d85
KM
820 if (ret < 0) {
821 fsi_clk_invalid(fsi);
822 return ret;
823 }
824
825 if (clock->xck)
826 clk_enable(clock->xck);
827 if (clock->ick)
828 clk_enable(clock->ick);
829 if (clock->div)
830 clk_enable(clock->div);
831
832 clock->count++;
833 }
834
835 return ret;
836}
837
838static int fsi_clk_disable(struct device *dev,
839 struct fsi_priv *fsi)
840{
841 struct fsi_clk *clock = &fsi->clock;
842
843 if (!fsi_clk_is_valid(fsi))
844 return -EINVAL;
845
846 if (1 == clock->count--) {
847 if (clock->xck)
848 clk_disable(clock->xck);
849 if (clock->ick)
850 clk_disable(clock->ick);
851 if (clock->div)
852 clk_disable(clock->div);
853 }
854
855 return 0;
856}
857
858static int fsi_clk_set_ackbpf(struct device *dev,
859 struct fsi_priv *fsi,
860 int ackmd, int bpfmd)
861{
862 u32 data = 0;
863
864 /* check ackmd/bpfmd relationship */
865 if (bpfmd > ackmd) {
866 dev_err(dev, "unsupported rate (%d/%d)\n", ackmd, bpfmd);
867 return -EINVAL;
868 }
869
870 /* ACKMD */
871 switch (ackmd) {
872 case 512:
873 data |= (0x0 << 12);
874 break;
875 case 256:
876 data |= (0x1 << 12);
877 break;
878 case 128:
879 data |= (0x2 << 12);
880 break;
881 case 64:
882 data |= (0x3 << 12);
883 break;
884 case 32:
885 data |= (0x4 << 12);
886 break;
887 default:
888 dev_err(dev, "unsupported ackmd (%d)\n", ackmd);
889 return -EINVAL;
890 }
891
892 /* BPFMD */
893 switch (bpfmd) {
894 case 32:
895 data |= (0x0 << 8);
896 break;
897 case 64:
898 data |= (0x1 << 8);
899 break;
900 case 128:
901 data |= (0x2 << 8);
902 break;
903 case 256:
904 data |= (0x3 << 8);
905 break;
906 case 512:
907 data |= (0x4 << 8);
908 break;
909 case 16:
910 data |= (0x7 << 8);
911 break;
912 default:
913 dev_err(dev, "unsupported bpfmd (%d)\n", bpfmd);
914 return -EINVAL;
915 }
916
917 dev_dbg(dev, "ACKMD/BPFMD = %d/%d\n", ackmd, bpfmd);
918
919 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
920 udelay(10);
921
922 return 0;
923}
924
925static int fsi_clk_set_rate_external(struct device *dev,
6cbdbffb 926 struct fsi_priv *fsi)
ab6f6d85
KM
927{
928 struct clk *xck = fsi->clock.xck;
929 struct clk *ick = fsi->clock.ick;
6cbdbffb 930 unsigned long rate = fsi->clock.rate;
ab6f6d85
KM
931 unsigned long xrate;
932 int ackmd, bpfmd;
933 int ret = 0;
934
935 /* check clock rate */
936 xrate = clk_get_rate(xck);
937 if (xrate % rate) {
938 dev_err(dev, "unsupported clock rate\n");
939 return -EINVAL;
940 }
941
942 clk_set_parent(ick, xck);
943 clk_set_rate(ick, xrate);
944
945 bpfmd = fsi->chan_num * 32;
946 ackmd = xrate / rate;
947
948 dev_dbg(dev, "external/rate = %ld/%ld\n", xrate, rate);
949
950 ret = fsi_clk_set_ackbpf(dev, fsi, ackmd, bpfmd);
951 if (ret < 0)
952 dev_err(dev, "%s failed", __func__);
953
954 return ret;
955}
956
957static int fsi_clk_set_rate_cpg(struct device *dev,
6cbdbffb 958 struct fsi_priv *fsi)
ab6f6d85
KM
959{
960 struct clk *ick = fsi->clock.ick;
961 struct clk *div = fsi->clock.div;
6cbdbffb 962 unsigned long rate = fsi->clock.rate;
ab6f6d85
KM
963 unsigned long target = 0; /* 12288000 or 11289600 */
964 unsigned long actual, cout;
965 unsigned long diff, min;
966 unsigned long best_cout, best_act;
967 int adj;
968 int ackmd, bpfmd;
969 int ret = -EINVAL;
970
971 if (!(12288000 % rate))
972 target = 12288000;
973 if (!(11289600 % rate))
974 target = 11289600;
975 if (!target) {
976 dev_err(dev, "unsupported rate\n");
977 return ret;
978 }
979
980 bpfmd = fsi->chan_num * 32;
981 ackmd = target / rate;
982 ret = fsi_clk_set_ackbpf(dev, fsi, ackmd, bpfmd);
983 if (ret < 0) {
984 dev_err(dev, "%s failed", __func__);
985 return ret;
986 }
987
988 /*
989 * The clock flow is
990 *
991 * [CPG] = cout => [FSI_DIV] = audio => [FSI] => [codec]
992 *
993 * But, it needs to find best match of CPG and FSI_DIV
994 * combination, since it is difficult to generate correct
995 * frequency of audio clock from ick clock only.
996 * Because ick is created from its parent clock.
997 *
998 * target = rate x [512/256/128/64]fs
999 * cout = round(target x adjustment)
1000 * actual = cout / adjustment (by FSI-DIV) ~= target
1001 * audio = actual
1002 */
1003 min = ~0;
1004 best_cout = 0;
1005 best_act = 0;
1006 for (adj = 1; adj < 0xffff; adj++) {
1007
1008 cout = target * adj;
1009 if (cout > 100000000) /* max clock = 100MHz */
1010 break;
1011
1012 /* cout/actual audio clock */
1013 cout = clk_round_rate(ick, cout);
1014 actual = cout / adj;
1015
1016 /* find best frequency */
1017 diff = abs(actual - target);
1018 if (diff < min) {
1019 min = diff;
1020 best_cout = cout;
1021 best_act = actual;
1022 }
1023 }
1024
1025 ret = clk_set_rate(ick, best_cout);
1026 if (ret < 0) {
1027 dev_err(dev, "ick clock failed\n");
1028 return -EIO;
1029 }
1030
1031 ret = clk_set_rate(div, clk_round_rate(div, best_act));
1032 if (ret < 0) {
1033 dev_err(dev, "div clock failed\n");
1034 return -EIO;
1035 }
1036
1037 dev_dbg(dev, "ick/div = %ld/%ld\n",
1038 clk_get_rate(ick), clk_get_rate(div));
1039
1040 return ret;
1041}
1042
1f5e2a31 1043/*
1b0ca1a0 1044 * pio data transfer handler
1f5e2a31 1045 */
1b0ca1a0
KM
1046static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
1047{
1b0ca1a0
KM
1048 int i;
1049
2522acd2 1050 if (fsi_is_enable_stream(fsi)) {
766812e6
KM
1051 /*
1052 * stream mode
1053 * see
1054 * fsi_pio_push_init()
1055 */
1056 u32 *buf = (u32 *)_buf;
1057
1058 for (i = 0; i < samples / 2; i++)
1059 fsi_reg_write(fsi, DODT, buf[i]);
1060 } else {
1061 /* normal mode */
1062 u16 *buf = (u16 *)_buf;
1063
1064 for (i = 0; i < samples; i++)
1065 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
1066 }
1b0ca1a0
KM
1067}
1068
1069static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
1070{
1071 u16 *buf = (u16 *)_buf;
1072 int i;
1073
1074 for (i = 0; i < samples; i++)
1075 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
1076}
1077
1078static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
1079{
1080 u32 *buf = (u32 *)_buf;
1081 int i;
1082
1083 for (i = 0; i < samples; i++)
1084 fsi_reg_write(fsi, DODT, *(buf + i));
1085}
1086
1087static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
1088{
1089 u32 *buf = (u32 *)_buf;
1090 int i;
1091
1092 for (i = 0; i < samples; i++)
1093 *(buf + i) = fsi_reg_read(fsi, DIDT);
1094}
1095
1096static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
1097{
1098 struct snd_pcm_runtime *runtime = io->substream->runtime;
1099
1100 return runtime->dma_area +
1101 samples_to_bytes(runtime, io->buff_sample_pos);
1102}
1103
1104static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
95b0cf05
KM
1105 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
1106 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
1107 int samples)
a4d7d550
KM
1108{
1109 struct snd_pcm_runtime *runtime;
376cf38a 1110 struct snd_pcm_substream *substream;
95b0cf05 1111 u8 *buf;
b9fde18c 1112 int over_period;
a4d7d550 1113
97df8187 1114 if (!fsi_stream_is_working(fsi, io))
a4d7d550
KM
1115 return -EINVAL;
1116
1c418d1f 1117 over_period = 0;
93193c2b 1118 substream = io->substream;
1c418d1f 1119 runtime = substream->runtime;
a4d7d550
KM
1120
1121 /* FSI FIFO has limit.
1122 * So, this driver can not send periods data at a time
1123 */
2e651baf
KM
1124 if (io->buff_sample_pos >=
1125 io->period_samples * (io->period_pos + 1)) {
a4d7d550 1126
1c418d1f 1127 over_period = 1;
2e651baf 1128 io->period_pos = (io->period_pos + 1) % runtime->periods;
a4d7d550 1129
2e651baf
KM
1130 if (0 == io->period_pos)
1131 io->buff_sample_pos = 0;
a4d7d550
KM
1132 }
1133
95b0cf05
KM
1134 buf = fsi_pio_get_area(fsi, io);
1135
376cf38a
KM
1136 switch (io->sample_width) {
1137 case 2:
95b0cf05 1138 run16(fsi, buf, samples);
376cf38a
KM
1139 break;
1140 case 4:
95b0cf05 1141 run32(fsi, buf, samples);
376cf38a
KM
1142 break;
1143 default:
1144 return -EINVAL;
d8b33534 1145 }
a4d7d550 1146
2e651baf
KM
1147 /* update buff_sample_pos */
1148 io->buff_sample_pos += samples;
a4d7d550 1149
1c418d1f 1150 if (over_period)
a4d7d550
KM
1151 snd_pcm_period_elapsed(substream);
1152
47fc9a0a 1153 return 0;
a4d7d550
KM
1154}
1155
5e97313a 1156static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
07102f3c 1157{
376cf38a
KM
1158 int sample_residues; /* samples in FSI fifo */
1159 int sample_space; /* ALSA free samples space */
1160 int samples;
376cf38a 1161
7b1b3331 1162 sample_residues = fsi_get_current_fifo_samples(fsi, io);
376cf38a
KM
1163 sample_space = io->buff_sample_capa - io->buff_sample_pos;
1164
1165 samples = min(sample_residues, sample_space);
1166
1b0ca1a0 1167 return fsi_pio_transfer(fsi, io,
d78629e2
KM
1168 fsi_pio_pop16,
1169 fsi_pio_pop32,
376cf38a 1170 samples);
d8b33534 1171}
07102f3c 1172
5e97313a 1173static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
d8b33534 1174{
376cf38a
KM
1175 int sample_residues; /* ALSA residue samples */
1176 int sample_space; /* FSI fifo free samples space */
1177 int samples;
376cf38a
KM
1178
1179 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
1180 sample_space = io->fifo_sample_capa -
7b1b3331 1181 fsi_get_current_fifo_samples(fsi, io);
376cf38a
KM
1182
1183 samples = min(sample_residues, sample_space);
1184
1b0ca1a0 1185 return fsi_pio_transfer(fsi, io,
d78629e2
KM
1186 fsi_pio_push16,
1187 fsi_pio_push32,
376cf38a 1188 samples);
07102f3c
KM
1189}
1190
c375b2d7 1191static int fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
180346ed
KM
1192 int enable)
1193{
1194 struct fsi_master *master = fsi_get_master(fsi);
1195 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
1196
1197 if (enable)
1198 fsi_irq_enable(fsi, io);
1199 else
1200 fsi_irq_disable(fsi, io);
1201
1202 if (fsi_is_clk_master(fsi))
1203 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
c375b2d7
KM
1204
1205 return 0;
180346ed
KM
1206}
1207
766812e6
KM
1208static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
1209{
766812e6
KM
1210 /*
1211 * we can use 16bit stream mode
1212 * when "playback" and "16bit data"
1213 * and platform allows "stream mode"
1214 * see
1215 * fsi_pio_push16()
1216 */
2522acd2 1217 if (fsi_is_enable_stream(fsi))
766812e6
KM
1218 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1219 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1220 else
1221 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1222 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
1223 return 0;
1224}
1225
1226static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
1227{
1228 /*
1229 * always 24bit bus, package back when "capture"
1230 */
1231 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1232 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
1233 return 0;
1234}
1235
5e97313a 1236static struct fsi_stream_handler fsi_pio_push_handler = {
766812e6 1237 .init = fsi_pio_push_init,
5e97313a 1238 .transfer = fsi_pio_push,
180346ed 1239 .start_stop = fsi_pio_start_stop,
5e97313a
KM
1240};
1241
1242static struct fsi_stream_handler fsi_pio_pop_handler = {
766812e6 1243 .init = fsi_pio_pop_init,
5e97313a 1244 .transfer = fsi_pio_pop,
180346ed 1245 .start_stop = fsi_pio_start_stop,
5e97313a
KM
1246};
1247
a4d7d550
KM
1248static irqreturn_t fsi_interrupt(int irq, void *data)
1249{
71f6e064 1250 struct fsi_master *master = data;
10ea76cc 1251 u32 int_st = fsi_irq_get_status(master);
a4d7d550
KM
1252
1253 /* clear irq status */
feb58cff
KM
1254 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1255 fsi_master_mask_set(master, SOFT_RST, IR, IR);
a4d7d550 1256
cf6edd00 1257 if (int_st & AB_IO(1, AO_SHIFT))
5e97313a 1258 fsi_stream_transfer(&master->fsia.playback);
cf6edd00 1259 if (int_st & AB_IO(1, BO_SHIFT))
5e97313a 1260 fsi_stream_transfer(&master->fsib.playback);
cf6edd00 1261 if (int_st & AB_IO(1, AI_SHIFT))
5e97313a 1262 fsi_stream_transfer(&master->fsia.capture);
cf6edd00 1263 if (int_st & AB_IO(1, BI_SHIFT))
5e97313a 1264 fsi_stream_transfer(&master->fsib.capture);
1ec9bc35
KM
1265
1266 fsi_count_fifo_err(&master->fsia);
1267 fsi_count_fifo_err(&master->fsib);
a4d7d550 1268
48d78e58
KM
1269 fsi_irq_clear_status(&master->fsia);
1270 fsi_irq_clear_status(&master->fsib);
a4d7d550
KM
1271
1272 return IRQ_HANDLED;
1273}
1274
7da9ced6
KM
1275/*
1276 * dma data transfer handler
1277 */
1278static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1279{
1280 struct snd_pcm_runtime *runtime = io->substream->runtime;
1281 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1282 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1283 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1284
766812e6
KM
1285 /*
1286 * 24bit data : 24bit bus / package in back
1287 * 16bit data : 16bit bus / stream mode
1288 */
1289 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1290 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1291
7da9ced6
KM
1292 io->dma = dma_map_single(dai->dev, runtime->dma_area,
1293 snd_pcm_lib_buffer_bytes(io->substream), dir);
1294 return 0;
1295}
1296
1297static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
1298{
1299 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1300 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1301 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1302
1303 dma_unmap_single(dai->dev, io->dma,
1304 snd_pcm_lib_buffer_bytes(io->substream), dir);
1305 return 0;
1306}
1307
4a1b09b7
KM
1308static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
1309{
1310 struct snd_pcm_runtime *runtime = io->substream->runtime;
1311
1312 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
1313}
1314
7da9ced6
KM
1315static void fsi_dma_complete(void *data)
1316{
1317 struct fsi_stream *io = (struct fsi_stream *)data;
1318 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1319 struct snd_pcm_runtime *runtime = io->substream->runtime;
1320 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1321 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1322 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1323
4a1b09b7 1324 dma_sync_single_for_cpu(dai->dev, fsi_dma_get_area(io),
7da9ced6
KM
1325 samples_to_bytes(runtime, io->period_samples), dir);
1326
1327 io->buff_sample_pos += io->period_samples;
1328 io->period_pos++;
1329
1330 if (io->period_pos >= runtime->periods) {
1331 io->period_pos = 0;
1332 io->buff_sample_pos = 0;
1333 }
1334
1335 fsi_count_fifo_err(fsi);
1336 fsi_stream_transfer(io);
1337
1338 snd_pcm_period_elapsed(io->substream);
1339}
1340
57451e43 1341static void fsi_dma_do_work(struct work_struct *work)
7da9ced6 1342{
57451e43 1343 struct fsi_stream *io = container_of(work, struct fsi_stream, work);
7da9ced6 1344 struct fsi_priv *fsi = fsi_stream_to_priv(io);
7da9ced6
KM
1345 struct snd_soc_dai *dai;
1346 struct dma_async_tx_descriptor *desc;
7da9ced6
KM
1347 struct snd_pcm_runtime *runtime;
1348 enum dma_data_direction dir;
7da9ced6
KM
1349 int is_play = fsi_stream_is_play(fsi, io);
1350 int len;
1351 dma_addr_t buf;
1352
1353 if (!fsi_stream_is_working(fsi, io))
1354 return;
1355
1356 dai = fsi_get_dai(io->substream);
7da9ced6
KM
1357 runtime = io->substream->runtime;
1358 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1359 len = samples_to_bytes(runtime, io->period_samples);
1360 buf = fsi_dma_get_area(io);
1361
4a1b09b7 1362 dma_sync_single_for_device(dai->dev, buf, len, dir);
7da9ced6 1363
5514efdf
KM
1364 desc = dmaengine_prep_slave_single(io->chan, buf, len, dir,
1365 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
7da9ced6 1366 if (!desc) {
cdf27f37 1367 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
7da9ced6
KM
1368 return;
1369 }
1370
1371 desc->callback = fsi_dma_complete;
1372 desc->callback_param = io;
1373
5514efdf 1374 if (dmaengine_submit(desc) < 0) {
7da9ced6
KM
1375 dev_err(dai->dev, "tx_submit() fail\n");
1376 return;
1377 }
1378
5514efdf 1379 dma_async_issue_pending(io->chan);
7da9ced6
KM
1380
1381 /*
1382 * FIXME
1383 *
1384 * In DMAEngine case, codec and FSI cannot be started simultaneously
57451e43 1385 * since FSI is using the scheduler work queue.
7da9ced6
KM
1386 * Therefore, in capture case, probably FSI FIFO will have got
1387 * overflow error in this point.
1388 * in that case, DMA cannot start transfer until error was cleared.
1389 */
1390 if (!is_play) {
1391 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1392 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1393 fsi_reg_write(fsi, DIFF_ST, 0);
1394 }
1395 }
1396}
1397
1398static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1399{
1400 struct sh_dmae_slave *slave = param;
1401
1402 chan->private = slave;
1403
1404 return true;
1405}
1406
1407static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1408{
57451e43 1409 schedule_work(&io->work);
7da9ced6
KM
1410
1411 return 0;
1412}
1413
c375b2d7 1414static int fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
7da9ced6
KM
1415 int start)
1416{
e42bb9bf
KM
1417 struct fsi_master *master = fsi_get_master(fsi);
1418 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
766812e6 1419 u32 enable = start ? DMA_ON : 0;
7da9ced6 1420
766812e6 1421 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
e42bb9bf 1422
fbe42f66
KM
1423 dmaengine_terminate_all(io->chan);
1424
e42bb9bf
KM
1425 if (fsi_is_clk_master(fsi))
1426 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
c375b2d7
KM
1427
1428 return 0;
7da9ced6
KM
1429}
1430
b1226dc5 1431static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev)
7da9ced6
KM
1432{
1433 dma_cap_mask_t mask;
1434
1435 dma_cap_zero(mask);
1436 dma_cap_set(DMA_SLAVE, mask);
1437
1438 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
b1226dc5
KM
1439 if (!io->chan) {
1440
1441 /* switch to PIO handler */
1442 if (fsi_stream_is_play(fsi, io))
1443 fsi->playback.handler = &fsi_pio_push_handler;
1444 else
1445 fsi->capture.handler = &fsi_pio_pop_handler;
1446
1447 dev_info(dev, "switch handler (dma => pio)\n");
1448
1449 /* probe again */
1450 return fsi_stream_probe(fsi, dev);
1451 }
7da9ced6 1452
57451e43 1453 INIT_WORK(&io->work, fsi_dma_do_work);
7da9ced6
KM
1454
1455 return 0;
1456}
1457
1458static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1459{
57451e43 1460 cancel_work_sync(&io->work);
7da9ced6
KM
1461
1462 fsi_stream_stop(fsi, io);
1463
1464 if (io->chan)
1465 dma_release_channel(io->chan);
1466
1467 io->chan = NULL;
1468 return 0;
1469}
1470
1471static struct fsi_stream_handler fsi_dma_push_handler = {
1472 .init = fsi_dma_init,
1473 .quit = fsi_dma_quit,
1474 .probe = fsi_dma_probe,
1475 .transfer = fsi_dma_transfer,
1476 .remove = fsi_dma_remove,
1477 .start_stop = fsi_dma_push_start_stop,
1478};
1479
c8fe2574
KM
1480/*
1481 * dai ops
1482 */
b49e8027 1483static void fsi_fifo_init(struct fsi_priv *fsi,
938e2a8d 1484 struct fsi_stream *io,
b49e8027
KM
1485 struct device *dev)
1486{
1487 struct fsi_master *master = fsi_get_master(fsi);
938e2a8d 1488 int is_play = fsi_stream_is_play(fsi, io);
b49e8027
KM
1489 u32 shift, i;
1490 int frame_capa;
1491
1492 /* get on-chip RAM capacity */
1493 shift = fsi_master_read(master, FIFO_SZ);
938e2a8d 1494 shift >>= fsi_get_port_shift(fsi, io);
b49e8027
KM
1495 shift &= FIFO_SZ_MASK;
1496 frame_capa = 256 << shift;
1497 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1498
1499 /*
1500 * The maximum number of sample data varies depending
1501 * on the number of channels selected for the format.
1502 *
1503 * FIFOs are used in 4-channel units in 3-channel mode
1504 * and in 8-channel units in 5- to 7-channel mode
1505 * meaning that more FIFOs than the required size of DPRAM
1506 * are used.
1507 *
1508 * ex) if 256 words of DP-RAM is connected
1509 * 1 channel: 256 (256 x 1 = 256)
1510 * 2 channels: 128 (128 x 2 = 256)
1511 * 3 channels: 64 ( 64 x 3 = 192)
1512 * 4 channels: 64 ( 64 x 4 = 256)
1513 * 5 channels: 32 ( 32 x 5 = 160)
1514 * 6 channels: 32 ( 32 x 6 = 192)
1515 * 7 channels: 32 ( 32 x 7 = 224)
1516 * 8 channels: 32 ( 32 x 8 = 256)
1517 */
1518 for (i = 1; i < fsi->chan_num; i <<= 1)
1519 frame_capa >>= 1;
1520 dev_dbg(dev, "%d channel %d store\n",
1521 fsi->chan_num, frame_capa);
1522
1523 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1524
1525 /*
1526 * set interrupt generation factor
1527 * clear FIFO
1528 */
1529 if (is_play) {
1530 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1531 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1532 } else {
1533 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1534 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1535 }
1536}
a4d7d550 1537
23ca8533 1538static int fsi_hw_startup(struct fsi_priv *fsi,
938e2a8d 1539 struct fsi_stream *io,
23ca8533 1540 struct device *dev)
a4d7d550 1541{
9478e0b6 1542 u32 data = 0;
a4d7d550 1543
9478e0b6
KM
1544 /* clock setting */
1545 if (fsi_is_clk_master(fsi))
1546 data = DIMD | DOMD;
1547
1548 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
a4d7d550
KM
1549
1550 /* clock inversion (CKG2) */
1551 data = 0;
3449f5fa
KM
1552 if (fsi->bit_clk_inv)
1553 data |= (1 << 0);
1554 if (fsi->lr_clk_inv)
1555 data |= (1 << 4);
1556 if (fsi_is_clk_master(fsi))
1557 data <<= 8;
a4d7d550
KM
1558 fsi_reg_write(fsi, CKG2, data);
1559
9478e0b6
KM
1560 /* spdif ? */
1561 if (fsi_is_spdif(fsi)) {
1562 fsi_spdif_clk_ctrl(fsi, 1);
1563 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1564 }
1565
65ff03f4 1566 /*
766812e6 1567 * get bus settings
65ff03f4 1568 */
766812e6
KM
1569 data = 0;
1570 switch (io->sample_width) {
1571 case 2:
1572 data = BUSOP_GET(16, io->bus_option);
1573 break;
1574 case 4:
1575 data = BUSOP_GET(24, io->bus_option);
1576 break;
65ff03f4 1577 }
766812e6 1578 fsi_format_bus_setup(fsi, io, data, dev);
65ff03f4 1579
10ea76cc 1580 /* irq clear */
938e2a8d 1581 fsi_irq_disable(fsi, io);
10ea76cc
KM
1582 fsi_irq_clear_status(fsi);
1583
1584 /* fifo init */
938e2a8d 1585 fsi_fifo_init(fsi, io, dev);
a4d7d550 1586
ddeb2d70
KM
1587 /* start master clock */
1588 if (fsi_is_clk_master(fsi))
6cbdbffb 1589 return fsi_clk_enable(dev, fsi);
ddeb2d70 1590
a68a3b4e 1591 return 0;
a4d7d550
KM
1592}
1593
80b4addc 1594static int fsi_hw_shutdown(struct fsi_priv *fsi,
23ca8533
KM
1595 struct device *dev)
1596{
ddeb2d70 1597 /* stop master clock */
23ca8533 1598 if (fsi_is_clk_master(fsi))
6cbdbffb 1599 return fsi_clk_disable(dev, fsi);
80b4addc
KM
1600
1601 return 0;
23ca8533
KM
1602}
1603
1604static int fsi_dai_startup(struct snd_pcm_substream *substream,
1605 struct snd_soc_dai *dai)
1606{
1607 struct fsi_priv *fsi = fsi_get_priv(substream);
23ca8533 1608
ab6f6d85 1609 fsi_clk_invalid(fsi);
f33238e9
KM
1610
1611 return 0;
23ca8533
KM
1612}
1613
a4d7d550
KM
1614static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1615 struct snd_soc_dai *dai)
1616{
71f6e064 1617 struct fsi_priv *fsi = fsi_get_priv(substream);
a4d7d550 1618
ab6f6d85 1619 fsi_clk_invalid(fsi);
a4d7d550
KM
1620}
1621
1622static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1623 struct snd_soc_dai *dai)
1624{
71f6e064 1625 struct fsi_priv *fsi = fsi_get_priv(substream);
938e2a8d 1626 struct fsi_stream *io = fsi_stream_get(fsi, substream);
a4d7d550
KM
1627 int ret = 0;
1628
a4d7d550
KM
1629 switch (cmd) {
1630 case SNDRV_PCM_TRIGGER_START:
938e2a8d 1631 fsi_stream_init(fsi, io, substream);
80b4addc
KM
1632 if (!ret)
1633 ret = fsi_hw_startup(fsi, io, dai->dev);
1634 if (!ret)
1635 ret = fsi_stream_transfer(io);
1636 if (!ret)
180346ed 1637 fsi_stream_start(fsi, io);
a4d7d550
KM
1638 break;
1639 case SNDRV_PCM_TRIGGER_STOP:
80b4addc
KM
1640 if (!ret)
1641 ret = fsi_hw_shutdown(fsi, dai->dev);
180346ed 1642 fsi_stream_stop(fsi, io);
938e2a8d 1643 fsi_stream_quit(fsi, io);
a4d7d550
KM
1644 break;
1645 }
1646
1647 return ret;
1648}
1649
f17c13ca
KM
1650static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1651{
f17c13ca
KM
1652 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1653 case SND_SOC_DAIFMT_I2S:
9c59dd34 1654 fsi->fmt = CR_I2S;
f17c13ca
KM
1655 fsi->chan_num = 2;
1656 break;
1657 case SND_SOC_DAIFMT_LEFT_J:
9c59dd34 1658 fsi->fmt = CR_PCM;
f17c13ca
KM
1659 fsi->chan_num = 2;
1660 break;
1661 default:
1662 return -EINVAL;
1663 }
1664
f17c13ca
KM
1665 return 0;
1666}
1667
1668static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1669{
1670 struct fsi_master *master = fsi_get_master(fsi);
f17c13ca 1671
284c6f65 1672 if (fsi_version(master) < 2)
f17c13ca
KM
1673 return -EINVAL;
1674
766812e6 1675 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
f17c13ca 1676 fsi->chan_num = 2;
f17c13ca 1677
f17c13ca
KM
1678 return 0;
1679}
1680
4d805f7b
KM
1681static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1682{
1683 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
4d805f7b
KM
1684 int ret;
1685
4d805f7b
KM
1686 /* set master/slave audio interface */
1687 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1688 case SND_SOC_DAIFMT_CBM_CFM:
6a9ebad8 1689 fsi->clk_master = 1;
4d805f7b
KM
1690 break;
1691 case SND_SOC_DAIFMT_CBS_CFS:
1692 break;
1693 default:
9478e0b6 1694 return -EINVAL;
4d805f7b 1695 }
6a9ebad8 1696
3449f5fa
KM
1697 /* set clock inversion */
1698 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1699 case SND_SOC_DAIFMT_NB_IF:
1700 fsi->bit_clk_inv = 0;
1701 fsi->lr_clk_inv = 1;
f17c13ca 1702 break;
3449f5fa
KM
1703 case SND_SOC_DAIFMT_IB_NF:
1704 fsi->bit_clk_inv = 1;
1705 fsi->lr_clk_inv = 0;
f17c13ca 1706 break;
3449f5fa
KM
1707 case SND_SOC_DAIFMT_IB_IF:
1708 fsi->bit_clk_inv = 1;
1709 fsi->lr_clk_inv = 1;
1710 break;
1711 case SND_SOC_DAIFMT_NB_NF:
f17c13ca 1712 default:
3449f5fa
KM
1713 fsi->bit_clk_inv = 0;
1714 fsi->lr_clk_inv = 0;
1715 break;
1716 }
1717
ab6f6d85 1718 if (fsi_is_clk_master(fsi)) {
ab6340c4 1719 if (fsi->clk_cpg)
ab6f6d85
KM
1720 fsi_clk_init(dai->dev, fsi, 0, 1, 1,
1721 fsi_clk_set_rate_cpg);
ab6340c4
KM
1722 else
1723 fsi_clk_init(dai->dev, fsi, 1, 1, 0,
1724 fsi_clk_set_rate_external);
f17c13ca 1725 }
4d805f7b 1726
f17c13ca 1727 /* set format */
c2052def 1728 if (fsi_is_spdif(fsi))
f17c13ca 1729 ret = fsi_set_fmt_spdif(fsi);
c2052def
KM
1730 else
1731 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
4d805f7b 1732
4d805f7b
KM
1733 return ret;
1734}
1735
ccad7b44
KM
1736static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1737 struct snd_pcm_hw_params *params,
1738 struct snd_soc_dai *dai)
1739{
1740 struct fsi_priv *fsi = fsi_get_priv(substream);
ccad7b44 1741
6cbdbffb
KM
1742 if (fsi_is_clk_master(fsi))
1743 fsi_clk_valid(fsi, params_rate(params));
ccad7b44 1744
ddeb2d70 1745 return 0;
ccad7b44
KM
1746}
1747
85e7652d 1748static const struct snd_soc_dai_ops fsi_dai_ops = {
a4d7d550
KM
1749 .startup = fsi_dai_startup,
1750 .shutdown = fsi_dai_shutdown,
1751 .trigger = fsi_dai_trigger,
4d805f7b 1752 .set_fmt = fsi_dai_set_fmt,
ccad7b44 1753 .hw_params = fsi_dai_hw_params,
a4d7d550
KM
1754};
1755
c8fe2574
KM
1756/*
1757 * pcm ops
1758 */
a4d7d550 1759
a4d7d550
KM
1760static struct snd_pcm_hardware fsi_pcm_hardware = {
1761 .info = SNDRV_PCM_INFO_INTERLEAVED |
1762 SNDRV_PCM_INFO_MMAP |
1763 SNDRV_PCM_INFO_MMAP_VALID |
1764 SNDRV_PCM_INFO_PAUSE,
1765 .formats = FSI_FMTS,
1766 .rates = FSI_RATES,
1767 .rate_min = 8000,
1768 .rate_max = 192000,
2a8c8a56 1769 .channels_min = 2,
a4d7d550
KM
1770 .channels_max = 2,
1771 .buffer_bytes_max = 64 * 1024,
1772 .period_bytes_min = 32,
1773 .period_bytes_max = 8192,
1774 .periods_min = 1,
1775 .periods_max = 32,
1776 .fifo_size = 256,
1777};
1778
1779static int fsi_pcm_open(struct snd_pcm_substream *substream)
1780{
1781 struct snd_pcm_runtime *runtime = substream->runtime;
1782 int ret = 0;
1783
1784 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1785
1786 ret = snd_pcm_hw_constraint_integer(runtime,
1787 SNDRV_PCM_HW_PARAM_PERIODS);
1788
1789 return ret;
1790}
1791
1792static int fsi_hw_params(struct snd_pcm_substream *substream,
1793 struct snd_pcm_hw_params *hw_params)
1794{
1795 return snd_pcm_lib_malloc_pages(substream,
1796 params_buffer_bytes(hw_params));
1797}
1798
1799static int fsi_hw_free(struct snd_pcm_substream *substream)
1800{
1801 return snd_pcm_lib_free_pages(substream);
1802}
1803
1804static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1805{
71f6e064 1806 struct fsi_priv *fsi = fsi_get_priv(substream);
938e2a8d 1807 struct fsi_stream *io = fsi_stream_get(fsi, substream);
a4d7d550 1808
1987877d 1809 return fsi_sample2frame(fsi, io->buff_sample_pos);
a4d7d550
KM
1810}
1811
1812static struct snd_pcm_ops fsi_pcm_ops = {
1813 .open = fsi_pcm_open,
1814 .ioctl = snd_pcm_lib_ioctl,
1815 .hw_params = fsi_hw_params,
1816 .hw_free = fsi_hw_free,
1817 .pointer = fsi_pointer,
1818};
1819
c8fe2574
KM
1820/*
1821 * snd_soc_platform
1822 */
a4d7d550 1823
a4d7d550
KM
1824#define PREALLOC_BUFFER (32 * 1024)
1825#define PREALLOC_BUFFER_MAX (32 * 1024)
1826
1827static void fsi_pcm_free(struct snd_pcm *pcm)
1828{
1829 snd_pcm_lib_preallocate_free_for_all(pcm);
1830}
1831
552d1ef6 1832static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
a4d7d550 1833{
552d1ef6
LG
1834 struct snd_pcm *pcm = rtd->pcm;
1835
a4d7d550
KM
1836 /*
1837 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1838 * in MMAP mode (i.e. aplay -M)
1839 */
1840 return snd_pcm_lib_preallocate_pages_for_all(
1841 pcm,
1842 SNDRV_DMA_TYPE_CONTINUOUS,
1843 snd_dma_continuous_data(GFP_KERNEL),
1844 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1845}
1846
c8fe2574
KM
1847/*
1848 * alsa struct
1849 */
a4d7d550 1850
f0fba2ad 1851static struct snd_soc_dai_driver fsi_soc_dai[] = {
a4d7d550 1852 {
f0fba2ad 1853 .name = "fsia-dai",
a4d7d550
KM
1854 .playback = {
1855 .rates = FSI_RATES,
1856 .formats = FSI_FMTS,
2a8c8a56
KM
1857 .channels_min = 2,
1858 .channels_max = 2,
a4d7d550 1859 },
07102f3c
KM
1860 .capture = {
1861 .rates = FSI_RATES,
1862 .formats = FSI_FMTS,
2a8c8a56
KM
1863 .channels_min = 2,
1864 .channels_max = 2,
07102f3c 1865 },
a4d7d550
KM
1866 .ops = &fsi_dai_ops,
1867 },
1868 {
f0fba2ad 1869 .name = "fsib-dai",
a4d7d550
KM
1870 .playback = {
1871 .rates = FSI_RATES,
1872 .formats = FSI_FMTS,
2a8c8a56
KM
1873 .channels_min = 2,
1874 .channels_max = 2,
a4d7d550 1875 },
07102f3c
KM
1876 .capture = {
1877 .rates = FSI_RATES,
1878 .formats = FSI_FMTS,
2a8c8a56
KM
1879 .channels_min = 2,
1880 .channels_max = 2,
07102f3c 1881 },
a4d7d550
KM
1882 .ops = &fsi_dai_ops,
1883 },
1884};
a4d7d550 1885
f0fba2ad
LG
1886static struct snd_soc_platform_driver fsi_soc_platform = {
1887 .ops = &fsi_pcm_ops,
a4d7d550
KM
1888 .pcm_new = fsi_pcm_new,
1889 .pcm_free = fsi_pcm_free,
1890};
a4d7d550 1891
da4f2f9e
KM
1892static const struct snd_soc_component_driver fsi_soc_component = {
1893 .name = "fsi",
1894};
1895
c8fe2574
KM
1896/*
1897 * platform function
1898 */
9e7b6d60
KM
1899static void fsi_of_parse(char *name,
1900 struct device_node *np,
1901 struct sh_fsi_port_info *info,
1902 struct device *dev)
1903{
1904 int i;
1905 char prop[128];
1906 unsigned long flags = 0;
1907 struct {
1908 char *name;
1909 unsigned int val;
1910 } of_parse_property[] = {
1911 { "spdif-connection", SH_FSI_FMT_SPDIF },
1912 { "stream-mode-support", SH_FSI_ENABLE_STREAM_MODE },
1913 { "use-internal-clock", SH_FSI_CLK_CPG },
1914 };
1915
1916 for (i = 0; i < ARRAY_SIZE(of_parse_property); i++) {
1917 sprintf(prop, "%s,%s", name, of_parse_property[i].name);
1918 if (of_get_property(np, prop, NULL))
1919 flags |= of_parse_property[i].val;
1920 }
1921 info->flags = flags;
1922
1923 dev_dbg(dev, "%s flags : %lx\n", name, info->flags);
1924}
1925
c2052def
KM
1926static void fsi_port_info_init(struct fsi_priv *fsi,
1927 struct sh_fsi_port_info *info)
1928{
1929 if (info->flags & SH_FSI_FMT_SPDIF)
1930 fsi->spdif = 1;
ab6340c4
KM
1931
1932 if (info->flags & SH_FSI_CLK_CPG)
1933 fsi->clk_cpg = 1;
2522acd2
KM
1934
1935 if (info->flags & SH_FSI_ENABLE_STREAM_MODE)
1936 fsi->enable_stream = 1;
c2052def
KM
1937}
1938
943fdadc
KM
1939static void fsi_handler_init(struct fsi_priv *fsi,
1940 struct sh_fsi_port_info *info)
5e97313a
KM
1941{
1942 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1943 fsi->playback.priv = fsi;
1944 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1945 fsi->capture.priv = fsi;
7da9ced6 1946
943fdadc
KM
1947 if (info->tx_id) {
1948 fsi->playback.slave.shdma_slave.slave_id = info->tx_id;
b8373147 1949 fsi->playback.handler = &fsi_dma_push_handler;
7da9ced6 1950 }
5e97313a 1951}
a4d7d550 1952
9e7b6d60 1953static struct of_device_id fsi_of_match[];
a4d7d550
KM
1954static int fsi_probe(struct platform_device *pdev)
1955{
71f6e064 1956 struct fsi_master *master;
9e7b6d60 1957 struct device_node *np = pdev->dev.of_node;
fd974e52 1958 struct sh_fsi_platform_info info;
9e7b6d60 1959 const struct fsi_core *core;
40f9118b 1960 struct fsi_priv *fsi;
a4d7d550 1961 struct resource *res;
a4d7d550
KM
1962 unsigned int irq;
1963 int ret;
1964
fd974e52 1965 memset(&info, 0, sizeof(info));
943fdadc 1966
9e7b6d60
KM
1967 core = NULL;
1968 if (np) {
1969 const struct of_device_id *of_id;
1970
1971 of_id = of_match_device(fsi_of_match, &pdev->dev);
1972 if (of_id) {
1973 core = of_id->data;
1974 fsi_of_parse("fsia", np, &info.port_a, &pdev->dev);
1975 fsi_of_parse("fsib", np, &info.port_b, &pdev->dev);
1976 }
1977 } else {
1978 const struct platform_device_id *id_entry = pdev->id_entry;
1979 if (id_entry)
1980 core = (struct fsi_core *)id_entry->driver_data;
1981
1982 if (pdev->dev.platform_data)
1983 memcpy(&info, pdev->dev.platform_data, sizeof(info));
1984 }
1985
1986 if (!core) {
cc780d38
KM
1987 dev_err(&pdev->dev, "unknown fsi device\n");
1988 return -ENODEV;
1989 }
1990
a4d7d550
KM
1991 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1992 irq = platform_get_irq(pdev, 0);
b6aa1793 1993 if (!res || (int)irq <= 0) {
a4d7d550 1994 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
6ac4262f 1995 return -ENODEV;
a4d7d550
KM
1996 }
1997
6ac4262f 1998 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
a4d7d550
KM
1999 if (!master) {
2000 dev_err(&pdev->dev, "Could not allocate master\n");
6ac4262f 2001 return -ENOMEM;
a4d7d550
KM
2002 }
2003
6ac4262f
KM
2004 master->base = devm_ioremap_nocache(&pdev->dev,
2005 res->start, resource_size(res));
a4d7d550 2006 if (!master->base) {
a4d7d550 2007 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
6ac4262f 2008 return -ENXIO;
a4d7d550
KM
2009 }
2010
3bc28070 2011 /* master setting */
9e7b6d60 2012 master->core = core;
3bc28070
KM
2013 spin_lock_init(&master->lock);
2014
2015 /* FSI A setting */
40f9118b
KM
2016 fsi = &master->fsia;
2017 fsi->base = master->base;
2018 fsi->master = master;
fd974e52
KM
2019 fsi_port_info_init(fsi, &info.port_a);
2020 fsi_handler_init(fsi, &info.port_a);
40f9118b 2021 ret = fsi_stream_probe(fsi, &pdev->dev);
5e97313a
KM
2022 if (ret < 0) {
2023 dev_err(&pdev->dev, "FSIA stream probe failed\n");
6ac4262f 2024 return ret;
5e97313a 2025 }
3bc28070
KM
2026
2027 /* FSI B setting */
40f9118b
KM
2028 fsi = &master->fsib;
2029 fsi->base = master->base + 0x40;
2030 fsi->master = master;
fd974e52
KM
2031 fsi_port_info_init(fsi, &info.port_b);
2032 fsi_handler_init(fsi, &info.port_b);
40f9118b 2033 ret = fsi_stream_probe(fsi, &pdev->dev);
5e97313a
KM
2034 if (ret < 0) {
2035 dev_err(&pdev->dev, "FSIB stream probe failed\n");
2036 goto exit_fsia;
2037 }
a4d7d550 2038
785d1c45 2039 pm_runtime_enable(&pdev->dev);
f0fba2ad 2040 dev_set_drvdata(&pdev->dev, master);
a4d7d550 2041
1ddd8286 2042 ret = devm_request_irq(&pdev->dev, irq, &fsi_interrupt, 0,
9e7b6d60 2043 dev_name(&pdev->dev), master);
a4d7d550
KM
2044 if (ret) {
2045 dev_err(&pdev->dev, "irq request err\n");
5e97313a 2046 goto exit_fsib;
a4d7d550
KM
2047 }
2048
f0fba2ad 2049 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
a4d7d550
KM
2050 if (ret < 0) {
2051 dev_err(&pdev->dev, "cannot snd soc register\n");
1ddd8286 2052 goto exit_fsib;
a4d7d550
KM
2053 }
2054
da4f2f9e
KM
2055 ret = snd_soc_register_component(&pdev->dev, &fsi_soc_component,
2056 fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
0b5ec87d 2057 if (ret < 0) {
da4f2f9e 2058 dev_err(&pdev->dev, "cannot snd component register\n");
0b5ec87d
KM
2059 goto exit_snd_soc;
2060 }
a4d7d550 2061
0b5ec87d
KM
2062 return ret;
2063
2064exit_snd_soc:
2065 snd_soc_unregister_platform(&pdev->dev);
5e97313a 2066exit_fsib:
c35e005f 2067 pm_runtime_disable(&pdev->dev);
5e97313a
KM
2068 fsi_stream_remove(&master->fsib);
2069exit_fsia:
2070 fsi_stream_remove(&master->fsia);
6ac4262f 2071
a4d7d550
KM
2072 return ret;
2073}
2074
2075static int fsi_remove(struct platform_device *pdev)
2076{
71f6e064
KM
2077 struct fsi_master *master;
2078
f0fba2ad 2079 master = dev_get_drvdata(&pdev->dev);
71f6e064 2080
785d1c45 2081 pm_runtime_disable(&pdev->dev);
a4d7d550 2082
da4f2f9e 2083 snd_soc_unregister_component(&pdev->dev);
d985f27e 2084 snd_soc_unregister_platform(&pdev->dev);
a4d7d550 2085
5e97313a
KM
2086 fsi_stream_remove(&master->fsia);
2087 fsi_stream_remove(&master->fsib);
2088
a4d7d550
KM
2089 return 0;
2090}
2091
106c79ec 2092static void __fsi_suspend(struct fsi_priv *fsi,
938e2a8d 2093 struct fsi_stream *io,
4f56cde1 2094 struct device *dev)
106c79ec 2095{
938e2a8d 2096 if (!fsi_stream_is_working(fsi, io))
cda828ca 2097 return;
106c79ec 2098
180346ed 2099 fsi_stream_stop(fsi, io);
41bba151 2100 fsi_hw_shutdown(fsi, dev);
106c79ec
KM
2101}
2102
2103static void __fsi_resume(struct fsi_priv *fsi,
938e2a8d 2104 struct fsi_stream *io,
4f56cde1 2105 struct device *dev)
106c79ec 2106{
938e2a8d 2107 if (!fsi_stream_is_working(fsi, io))
cda828ca 2108 return;
106c79ec 2109
938e2a8d 2110 fsi_hw_startup(fsi, io, dev);
180346ed 2111 fsi_stream_start(fsi, io);
106c79ec
KM
2112}
2113
2114static int fsi_suspend(struct device *dev)
2115{
2116 struct fsi_master *master = dev_get_drvdata(dev);
cda828ca
KM
2117 struct fsi_priv *fsia = &master->fsia;
2118 struct fsi_priv *fsib = &master->fsib;
106c79ec 2119
938e2a8d
KM
2120 __fsi_suspend(fsia, &fsia->playback, dev);
2121 __fsi_suspend(fsia, &fsia->capture, dev);
106c79ec 2122
938e2a8d
KM
2123 __fsi_suspend(fsib, &fsib->playback, dev);
2124 __fsi_suspend(fsib, &fsib->capture, dev);
106c79ec
KM
2125
2126 return 0;
2127}
2128
2129static int fsi_resume(struct device *dev)
2130{
2131 struct fsi_master *master = dev_get_drvdata(dev);
cda828ca
KM
2132 struct fsi_priv *fsia = &master->fsia;
2133 struct fsi_priv *fsib = &master->fsib;
106c79ec 2134
938e2a8d
KM
2135 __fsi_resume(fsia, &fsia->playback, dev);
2136 __fsi_resume(fsia, &fsia->capture, dev);
106c79ec 2137
938e2a8d
KM
2138 __fsi_resume(fsib, &fsib->playback, dev);
2139 __fsi_resume(fsib, &fsib->capture, dev);
106c79ec
KM
2140
2141 return 0;
2142}
2143
785d1c45 2144static struct dev_pm_ops fsi_pm_ops = {
106c79ec
KM
2145 .suspend = fsi_suspend,
2146 .resume = fsi_resume,
785d1c45
KM
2147};
2148
73b92c1f
KM
2149static struct fsi_core fsi1_core = {
2150 .ver = 1,
2151
2152 /* Interrupt */
cc780d38
KM
2153 .int_st = INT_ST,
2154 .iemsk = IEMSK,
2155 .imsk = IMSK,
2156};
2157
73b92c1f
KM
2158static struct fsi_core fsi2_core = {
2159 .ver = 2,
2160
2161 /* Interrupt */
cc780d38
KM
2162 .int_st = CPU_INT_ST,
2163 .iemsk = CPU_IEMSK,
2164 .imsk = CPU_IMSK,
2b0e7302
KM
2165 .a_mclk = A_MST_CTLR,
2166 .b_mclk = B_MST_CTLR,
cc780d38
KM
2167};
2168
e43fc6af 2169static struct of_device_id fsi_of_match[] = {
9e7b6d60
KM
2170 { .compatible = "renesas,sh_fsi", .data = &fsi1_core},
2171 { .compatible = "renesas,sh_fsi2", .data = &fsi2_core},
2172 {},
2173};
2174MODULE_DEVICE_TABLE(of, fsi_of_match);
2175
cc780d38 2176static struct platform_device_id fsi_id_table[] = {
73b92c1f
KM
2177 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
2178 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
05c69450 2179 {},
cc780d38 2180};
d85a6d7b 2181MODULE_DEVICE_TABLE(platform, fsi_id_table);
cc780d38 2182
a4d7d550
KM
2183static struct platform_driver fsi_driver = {
2184 .driver = {
f0fba2ad 2185 .name = "fsi-pcm-audio",
785d1c45 2186 .pm = &fsi_pm_ops,
9e7b6d60 2187 .of_match_table = fsi_of_match,
a4d7d550
KM
2188 },
2189 .probe = fsi_probe,
2190 .remove = fsi_remove,
cc780d38 2191 .id_table = fsi_id_table,
a4d7d550
KM
2192};
2193
cb5e8738 2194module_platform_driver(fsi_driver);
a4d7d550
KM
2195
2196MODULE_LICENSE("GPL");
2197MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
2198MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
b3c27b51 2199MODULE_ALIAS("platform:fsi-pcm-audio");
This page took 0.32934 seconds and 5 git commands to generate.