ASoC: fsi: modify noisy comment out
[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>
785d1c45 16#include <linux/pm_runtime.h>
a4d7d550 17#include <linux/io.h>
5a0e3ad6 18#include <linux/slab.h>
a4d7d550 19#include <sound/soc.h>
a4d7d550 20#include <sound/sh_fsi.h>
a4d7d550
KM
21
22#define DO_FMT 0x0000
23#define DOFF_CTL 0x0004
24#define DOFF_ST 0x0008
25#define DI_FMT 0x000C
26#define DIFF_CTL 0x0010
27#define DIFF_ST 0x0014
28#define CKG1 0x0018
29#define CKG2 0x001C
30#define DIDT 0x0020
31#define DODT 0x0024
32#define MUTE_ST 0x0028
3bc28070
KM
33#define OUT_SEL 0x0030
34#define REG_END OUT_SEL
cc780d38 35
3bc28070
KM
36#define A_MST_CTLR 0x0180
37#define B_MST_CTLR 0x01A0
cc780d38
KM
38#define CPU_INT_ST 0x01F4
39#define CPU_IEMSK 0x01F8
40#define CPU_IMSK 0x01FC
a4d7d550
KM
41#define INT_ST 0x0200
42#define IEMSK 0x0204
43#define IMSK 0x0208
44#define MUTE 0x020C
45#define CLK_RST 0x0210
46#define SOFT_RST 0x0214
4a942b45 47#define FIFO_SZ 0x0218
3bc28070 48#define MREG_START A_MST_CTLR
4a942b45 49#define MREG_END FIFO_SZ
a4d7d550
KM
50
51/* DO_FMT */
52/* DI_FMT */
a7ffb52b
KM
53#define CR_MONO (0x0 << 4)
54#define CR_MONO_D (0x1 << 4)
55#define CR_PCM (0x2 << 4)
56#define CR_I2S (0x3 << 4)
57#define CR_TDM (0x4 << 4)
58#define CR_TDM_D (0x5 << 4)
3bc28070 59#define CR_SPDIF 0x00100120
a4d7d550
KM
60
61/* DOFF_CTL */
62/* DIFF_CTL */
63#define IRQ_HALF 0x00100000
64#define FIFO_CLR 0x00000001
65
66/* DOFF_ST */
67#define ERR_OVER 0x00000010
68#define ERR_UNDER 0x00000001
59c3b003 69#define ST_ERR (ERR_OVER | ERR_UNDER)
a4d7d550 70
ccad7b44
KM
71/* CKG1 */
72#define ACKMD_MASK 0x00007000
73#define BPFMD_MASK 0x00000700
74
3bc28070
KM
75/* A/B MST_CTLR */
76#define BP (1 << 4) /* Fix the signal of Biphase output */
77#define SE (1 << 0) /* Fix the master clock */
78
a4d7d550
KM
79/* CLK_RST */
80#define B_CLK 0x00000010
81#define A_CLK 0x00000001
82
83/* INT_ST */
84#define INT_B_IN (1 << 12)
85#define INT_B_OUT (1 << 8)
86#define INT_A_IN (1 << 4)
87#define INT_A_OUT (1 << 0)
88
feb58cff
KM
89/* SOFT_RST */
90#define PBSR (1 << 12) /* Port B Software Reset */
91#define PASR (1 << 8) /* Port A Software Reset */
92#define IR (1 << 4) /* Interrupt Reset */
93#define FSISR (1 << 0) /* Software Reset */
94
4a942b45
KM
95/* FIFO_SZ */
96#define OUT_SZ_MASK 0x7
97#define BO_SZ_SHIFT 8
98#define AO_SZ_SHIFT 0
99
a4d7d550
KM
100#define FSI_RATES SNDRV_PCM_RATE_8000_96000
101
102#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
103
c8fe2574
KM
104/*
105 * struct
106 */
a4d7d550 107
a4d7d550
KM
108struct fsi_priv {
109 void __iomem *base;
110 struct snd_pcm_substream *substream;
71f6e064 111 struct fsi_master *master;
a4d7d550
KM
112
113 int fifo_max;
114 int chan;
a4d7d550
KM
115
116 int byte_offset;
117 int period_len;
118 int buffer_len;
119 int periods;
3bc28070
KM
120
121 u32 mst_ctrl;
a4d7d550
KM
122};
123
73b92c1f
KM
124struct fsi_core {
125 int ver;
126
cc780d38
KM
127 u32 int_st;
128 u32 iemsk;
129 u32 imsk;
130};
131
a4d7d550
KM
132struct fsi_master {
133 void __iomem *base;
134 int irq;
a4d7d550
KM
135 struct fsi_priv fsia;
136 struct fsi_priv fsib;
73b92c1f 137 struct fsi_core *core;
a4d7d550 138 struct sh_fsi_platform_info *info;
8fc176d5 139 spinlock_t lock;
a4d7d550
KM
140};
141
c8fe2574
KM
142/*
143 * basic read write function
144 */
a4d7d550 145
0f69d978 146static void __fsi_reg_write(u32 reg, u32 data)
a4d7d550
KM
147{
148 /* valid data area is 24bit */
149 data &= 0x00ffffff;
150
0f69d978 151 __raw_writel(data, reg);
a4d7d550
KM
152}
153
154static u32 __fsi_reg_read(u32 reg)
155{
0f69d978 156 return __raw_readl(reg);
a4d7d550
KM
157}
158
0f69d978 159static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
a4d7d550
KM
160{
161 u32 val = __fsi_reg_read(reg);
162
163 val &= ~mask;
164 val |= data & mask;
165
0f69d978 166 __fsi_reg_write(reg, val);
a4d7d550
KM
167}
168
0f69d978 169static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
a4d7d550 170{
d7854147
KM
171 if (reg > REG_END) {
172 pr_err("fsi: register access err (%s)\n", __func__);
0f69d978 173 return;
d7854147 174 }
a4d7d550 175
0f69d978 176 __fsi_reg_write((u32)(fsi->base + reg), data);
a4d7d550
KM
177}
178
179static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
180{
d7854147
KM
181 if (reg > REG_END) {
182 pr_err("fsi: register access err (%s)\n", __func__);
a4d7d550 183 return 0;
d7854147 184 }
a4d7d550
KM
185
186 return __fsi_reg_read((u32)(fsi->base + reg));
187}
188
0f69d978 189static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
a4d7d550 190{
d7854147
KM
191 if (reg > REG_END) {
192 pr_err("fsi: register access err (%s)\n", __func__);
0f69d978 193 return;
d7854147 194 }
a4d7d550 195
0f69d978 196 __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
a4d7d550
KM
197}
198
0f69d978 199static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
a4d7d550 200{
8fc176d5
KM
201 unsigned long flags;
202
a4d7d550 203 if ((reg < MREG_START) ||
d7854147
KM
204 (reg > MREG_END)) {
205 pr_err("fsi: register access err (%s)\n", __func__);
0f69d978 206 return;
d7854147 207 }
a4d7d550 208
8fc176d5 209 spin_lock_irqsave(&master->lock, flags);
0f69d978 210 __fsi_reg_write((u32)(master->base + reg), data);
8fc176d5 211 spin_unlock_irqrestore(&master->lock, flags);
a4d7d550
KM
212}
213
71f6e064 214static u32 fsi_master_read(struct fsi_master *master, u32 reg)
a4d7d550 215{
8fc176d5
KM
216 u32 ret;
217 unsigned long flags;
218
a4d7d550 219 if ((reg < MREG_START) ||
d7854147
KM
220 (reg > MREG_END)) {
221 pr_err("fsi: register access err (%s)\n", __func__);
a4d7d550 222 return 0;
d7854147 223 }
a4d7d550 224
8fc176d5
KM
225 spin_lock_irqsave(&master->lock, flags);
226 ret = __fsi_reg_read((u32)(master->base + reg));
227 spin_unlock_irqrestore(&master->lock, flags);
228
229 return ret;
a4d7d550
KM
230}
231
0f69d978 232static void fsi_master_mask_set(struct fsi_master *master,
71f6e064 233 u32 reg, u32 mask, u32 data)
a4d7d550 234{
8fc176d5
KM
235 unsigned long flags;
236
a4d7d550 237 if ((reg < MREG_START) ||
d7854147
KM
238 (reg > MREG_END)) {
239 pr_err("fsi: register access err (%s)\n", __func__);
0f69d978 240 return;
d7854147 241 }
a4d7d550 242
8fc176d5 243 spin_lock_irqsave(&master->lock, flags);
0f69d978 244 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
8fc176d5 245 spin_unlock_irqrestore(&master->lock, flags);
a4d7d550
KM
246}
247
c8fe2574
KM
248/*
249 * basic function
250 */
a4d7d550 251
71f6e064 252static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
a4d7d550 253{
71f6e064 254 return fsi->master;
a4d7d550
KM
255}
256
257static int fsi_is_port_a(struct fsi_priv *fsi)
258{
71f6e064
KM
259 return fsi->master->base == fsi->base;
260}
a4d7d550 261
142e8174 262static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
71f6e064
KM
263{
264 struct snd_soc_pcm_runtime *rtd = substream->private_data;
142e8174 265
f0fba2ad 266 return rtd->cpu_dai;
142e8174
KM
267}
268
269static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
270{
271 struct snd_soc_dai *dai = fsi_get_dai(substream);
f0fba2ad 272 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
a4d7d550 273
f0fba2ad
LG
274 if (dai->id == 0)
275 return &master->fsia;
276 else
277 return &master->fsib;
a4d7d550
KM
278}
279
280static u32 fsi_get_info_flags(struct fsi_priv *fsi)
281{
282 int is_porta = fsi_is_port_a(fsi);
71f6e064 283 struct fsi_master *master = fsi_get_master(fsi);
a4d7d550
KM
284
285 return is_porta ? master->info->porta_flags :
286 master->info->portb_flags;
287}
288
289static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
290{
291 u32 mode;
292 u32 flags = fsi_get_info_flags(fsi);
293
294 mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
295
296 /* return
297 * 1 : master mode
298 * 0 : slave mode
299 */
300
301 return (mode & flags) != mode;
302}
303
304static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play)
305{
306 int is_porta = fsi_is_port_a(fsi);
307 u32 data;
308
309 if (is_porta)
310 data = is_play ? (1 << 0) : (1 << 4);
311 else
312 data = is_play ? (1 << 8) : (1 << 12);
313
314 return data;
315}
316
317static void fsi_stream_push(struct fsi_priv *fsi,
318 struct snd_pcm_substream *substream,
319 u32 buffer_len,
320 u32 period_len)
321{
322 fsi->substream = substream;
323 fsi->buffer_len = buffer_len;
324 fsi->period_len = period_len;
325 fsi->byte_offset = 0;
326 fsi->periods = 0;
327}
328
329static void fsi_stream_pop(struct fsi_priv *fsi)
330{
331 fsi->substream = NULL;
332 fsi->buffer_len = 0;
333 fsi->period_len = 0;
334 fsi->byte_offset = 0;
335 fsi->periods = 0;
336}
337
338static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
339{
340 u32 status;
341 u32 reg = is_play ? DOFF_ST : DIFF_ST;
342 int residue;
343
344 status = fsi_reg_read(fsi, reg);
345 residue = 0x1ff & (status >> 8);
346 residue *= fsi->chan;
347
348 return residue;
349}
350
c79eab3e
KM
351static u8 *fsi_dma_get_area(struct fsi_priv *fsi)
352{
353 return fsi->substream->runtime->dma_area + fsi->byte_offset;
354}
355
c8fe2574
KM
356/*
357 * irq function
358 */
a4d7d550 359
a4d7d550
KM
360static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
361{
362 u32 data = fsi_port_ab_io_bit(fsi, is_play);
71f6e064 363 struct fsi_master *master = fsi_get_master(fsi);
a4d7d550 364
73b92c1f
KM
365 fsi_master_mask_set(master, master->core->imsk, data, data);
366 fsi_master_mask_set(master, master->core->iemsk, data, data);
a4d7d550
KM
367}
368
369static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
370{
371 u32 data = fsi_port_ab_io_bit(fsi, is_play);
71f6e064 372 struct fsi_master *master = fsi_get_master(fsi);
a4d7d550 373
73b92c1f
KM
374 fsi_master_mask_set(master, master->core->imsk, data, 0);
375 fsi_master_mask_set(master, master->core->iemsk, data, 0);
a4d7d550
KM
376}
377
10ea76cc
KM
378static u32 fsi_irq_get_status(struct fsi_master *master)
379{
73b92c1f 380 return fsi_master_read(master, master->core->int_st);
10ea76cc
KM
381}
382
383static void fsi_irq_clear_all_status(struct fsi_master *master)
384{
73b92c1f 385 fsi_master_write(master, master->core->int_st, 0);
a4d7d550
KM
386}
387
10ea76cc
KM
388static void fsi_irq_clear_status(struct fsi_priv *fsi)
389{
390 u32 data = 0;
391 struct fsi_master *master = fsi_get_master(fsi);
392
393 data |= fsi_port_ab_io_bit(fsi, 0);
394 data |= fsi_port_ab_io_bit(fsi, 1);
395
396 /* clear interrupt factor */
73b92c1f 397 fsi_master_mask_set(master, master->core->int_st, data, 0);
10ea76cc
KM
398}
399
c8fe2574
KM
400/*
401 * SPDIF master clock function
402 *
403 * These functions are used later FSI2
404 */
3bc28070
KM
405static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
406{
407 struct fsi_master *master = fsi_get_master(fsi);
408 u32 val = BP | SE;
409
410 if (master->core->ver < 2) {
411 pr_err("fsi: register access err (%s)\n", __func__);
412 return;
413 }
414
415 if (enable)
416 fsi_master_mask_set(master, fsi->mst_ctrl, val, val);
417 else
418 fsi_master_mask_set(master, fsi->mst_ctrl, val, 0);
419}
420
c8fe2574
KM
421/*
422 * ctrl function
423 */
10ea76cc 424
a4d7d550
KM
425static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
426{
427 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
71f6e064 428 struct fsi_master *master = fsi_get_master(fsi);
a4d7d550
KM
429
430 if (enable)
71f6e064 431 fsi_master_mask_set(master, CLK_RST, val, val);
a4d7d550 432 else
71f6e064 433 fsi_master_mask_set(master, CLK_RST, val, 0);
a4d7d550
KM
434}
435
4a942b45
KM
436static void fsi_fifo_init(struct fsi_priv *fsi,
437 int is_play,
438 struct snd_soc_dai *dai)
a4d7d550 439{
4a942b45
KM
440 struct fsi_master *master = fsi_get_master(fsi);
441 u32 ctrl, shift, i;
a4d7d550 442
4a942b45
KM
443 /* get on-chip RAM capacity */
444 shift = fsi_master_read(master, FIFO_SZ);
445 shift >>= fsi_is_port_a(fsi) ? AO_SZ_SHIFT : BO_SZ_SHIFT;
446 shift &= OUT_SZ_MASK;
447 fsi->fifo_max = 256 << shift;
448 dev_dbg(dai->dev, "fifo = %d words\n", fsi->fifo_max);
a4d7d550 449
4a942b45
KM
450 /*
451 * The maximum number of sample data varies depending
452 * on the number of channels selected for the format.
453 *
454 * FIFOs are used in 4-channel units in 3-channel mode
455 * and in 8-channel units in 5- to 7-channel mode
456 * meaning that more FIFOs than the required size of DPRAM
457 * are used.
458 *
459 * ex) if 256 words of DP-RAM is connected
460 * 1 channel: 256 (256 x 1 = 256)
461 * 2 channels: 128 (128 x 2 = 256)
462 * 3 channels: 64 ( 64 x 3 = 192)
463 * 4 channels: 64 ( 64 x 4 = 256)
464 * 5 channels: 32 ( 32 x 5 = 160)
465 * 6 channels: 32 ( 32 x 6 = 192)
466 * 7 channels: 32 ( 32 x 7 = 224)
467 * 8 channels: 32 ( 32 x 8 = 256)
468 */
469 for (i = 1; i < fsi->chan; i <<= 1)
470 fsi->fifo_max >>= 1;
471 dev_dbg(dai->dev, "%d channel %d store\n", fsi->chan, fsi->fifo_max);
a4d7d550 472
a4d7d550 473 ctrl = is_play ? DOFF_CTL : DIFF_CTL;
a4d7d550
KM
474
475 /* set interrupt generation factor */
476 fsi_reg_write(fsi, ctrl, IRQ_HALF);
477
478 /* clear FIFO */
479 fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
a4d7d550
KM
480}
481
71f6e064 482static void fsi_soft_all_reset(struct fsi_master *master)
a4d7d550 483{
a4d7d550 484 /* port AB reset */
feb58cff 485 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
a4d7d550
KM
486 mdelay(10);
487
488 /* soft reset */
feb58cff
KM
489 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
490 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
a4d7d550
KM
491 mdelay(10);
492}
493
a4d7d550 494/* playback interrupt */
47fc9a0a 495static int fsi_data_push(struct fsi_priv *fsi, int startup)
a4d7d550
KM
496{
497 struct snd_pcm_runtime *runtime;
498 struct snd_pcm_substream *substream = NULL;
59c3b003 499 u32 status;
a4d7d550
KM
500 int send;
501 int fifo_free;
502 int width;
9ddc9aa9 503 u8 *start;
47fc9a0a 504 int i, over_period;
a4d7d550
KM
505
506 if (!fsi ||
507 !fsi->substream ||
508 !fsi->substream->runtime)
509 return -EINVAL;
510
1c418d1f
KM
511 over_period = 0;
512 substream = fsi->substream;
513 runtime = substream->runtime;
a4d7d550
KM
514
515 /* FSI FIFO has limit.
516 * So, this driver can not send periods data at a time
517 */
518 if (fsi->byte_offset >=
519 fsi->period_len * (fsi->periods + 1)) {
520
1c418d1f 521 over_period = 1;
a4d7d550
KM
522 fsi->periods = (fsi->periods + 1) % runtime->periods;
523
524 if (0 == fsi->periods)
525 fsi->byte_offset = 0;
526 }
527
528 /* get 1 channel data width */
529 width = frames_to_bytes(runtime, 1) / fsi->chan;
530
531 /* get send size for alsa */
532 send = (fsi->buffer_len - fsi->byte_offset) / width;
533
534 /* get FIFO free size */
535 fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
536
537 /* size check */
538 if (fifo_free < send)
539 send = fifo_free;
540
c79eab3e 541 start = fsi_dma_get_area(fsi);
9ddc9aa9
KM
542
543 switch (width) {
544 case 2:
545 for (i = 0; i < send; i++)
546 fsi_reg_write(fsi, DODT,
547 ((u32)*((u16 *)start + i) << 8));
548 break;
549 case 4:
550 for (i = 0; i < send; i++)
551 fsi_reg_write(fsi, DODT, *((u32 *)start + i));
552 break;
553 default:
a4d7d550 554 return -EINVAL;
9ddc9aa9 555 }
a4d7d550
KM
556
557 fsi->byte_offset += send * width;
558
59c3b003 559 status = fsi_reg_read(fsi, DOFF_ST);
47fc9a0a 560 if (!startup) {
59c3b003 561 struct snd_soc_dai *dai = fsi_get_dai(substream);
47fc9a0a
KM
562
563 if (status & ERR_OVER)
564 dev_err(dai->dev, "over run\n");
565 if (status & ERR_UNDER)
566 dev_err(dai->dev, "under run\n");
59c3b003 567 }
47fc9a0a 568 fsi_reg_write(fsi, DOFF_ST, 0);
59c3b003 569
a4d7d550
KM
570 fsi_irq_enable(fsi, 1);
571
1c418d1f 572 if (over_period)
a4d7d550
KM
573 snd_pcm_period_elapsed(substream);
574
47fc9a0a 575 return 0;
a4d7d550
KM
576}
577
47fc9a0a 578static int fsi_data_pop(struct fsi_priv *fsi, int startup)
07102f3c
KM
579{
580 struct snd_pcm_runtime *runtime;
581 struct snd_pcm_substream *substream = NULL;
59c3b003 582 u32 status;
07102f3c
KM
583 int free;
584 int fifo_fill;
585 int width;
586 u8 *start;
47fc9a0a 587 int i, over_period;
07102f3c
KM
588
589 if (!fsi ||
590 !fsi->substream ||
591 !fsi->substream->runtime)
592 return -EINVAL;
593
1c418d1f
KM
594 over_period = 0;
595 substream = fsi->substream;
596 runtime = substream->runtime;
07102f3c
KM
597
598 /* FSI FIFO has limit.
599 * So, this driver can not send periods data at a time
600 */
601 if (fsi->byte_offset >=
602 fsi->period_len * (fsi->periods + 1)) {
603
1c418d1f 604 over_period = 1;
07102f3c
KM
605 fsi->periods = (fsi->periods + 1) % runtime->periods;
606
607 if (0 == fsi->periods)
608 fsi->byte_offset = 0;
609 }
610
611 /* get 1 channel data width */
612 width = frames_to_bytes(runtime, 1) / fsi->chan;
613
614 /* get free space for alsa */
615 free = (fsi->buffer_len - fsi->byte_offset) / width;
616
617 /* get recv size */
618 fifo_fill = fsi_get_fifo_residue(fsi, 0);
619
620 if (free < fifo_fill)
621 fifo_fill = free;
622
c79eab3e 623 start = fsi_dma_get_area(fsi);
07102f3c
KM
624
625 switch (width) {
626 case 2:
627 for (i = 0; i < fifo_fill; i++)
628 *((u16 *)start + i) =
629 (u16)(fsi_reg_read(fsi, DIDT) >> 8);
630 break;
631 case 4:
632 for (i = 0; i < fifo_fill; i++)
633 *((u32 *)start + i) = fsi_reg_read(fsi, DIDT);
634 break;
635 default:
636 return -EINVAL;
637 }
638
639 fsi->byte_offset += fifo_fill * width;
640
59c3b003 641 status = fsi_reg_read(fsi, DIFF_ST);
47fc9a0a 642 if (!startup) {
59c3b003 643 struct snd_soc_dai *dai = fsi_get_dai(substream);
47fc9a0a
KM
644
645 if (status & ERR_OVER)
646 dev_err(dai->dev, "over run\n");
647 if (status & ERR_UNDER)
648 dev_err(dai->dev, "under run\n");
59c3b003 649 }
47fc9a0a 650 fsi_reg_write(fsi, DIFF_ST, 0);
59c3b003 651
07102f3c
KM
652 fsi_irq_enable(fsi, 0);
653
1c418d1f 654 if (over_period)
07102f3c
KM
655 snd_pcm_period_elapsed(substream);
656
47fc9a0a 657 return 0;
07102f3c
KM
658}
659
a4d7d550
KM
660static irqreturn_t fsi_interrupt(int irq, void *data)
661{
71f6e064 662 struct fsi_master *master = data;
10ea76cc 663 u32 int_st = fsi_irq_get_status(master);
a4d7d550
KM
664
665 /* clear irq status */
feb58cff
KM
666 fsi_master_mask_set(master, SOFT_RST, IR, 0);
667 fsi_master_mask_set(master, SOFT_RST, IR, IR);
a4d7d550
KM
668
669 if (int_st & INT_A_OUT)
47fc9a0a 670 fsi_data_push(&master->fsia, 0);
a4d7d550 671 if (int_st & INT_B_OUT)
47fc9a0a 672 fsi_data_push(&master->fsib, 0);
07102f3c 673 if (int_st & INT_A_IN)
47fc9a0a 674 fsi_data_pop(&master->fsia, 0);
07102f3c 675 if (int_st & INT_B_IN)
47fc9a0a 676 fsi_data_pop(&master->fsib, 0);
a4d7d550 677
10ea76cc 678 fsi_irq_clear_all_status(master);
a4d7d550
KM
679
680 return IRQ_HANDLED;
681}
682
c8fe2574
KM
683/*
684 * dai ops
685 */
a4d7d550 686
a4d7d550
KM
687static int fsi_dai_startup(struct snd_pcm_substream *substream,
688 struct snd_soc_dai *dai)
689{
71f6e064 690 struct fsi_priv *fsi = fsi_get_priv(substream);
a4d7d550 691 u32 flags = fsi_get_info_flags(fsi);
3bc28070 692 struct fsi_master *master = fsi_get_master(fsi);
a4d7d550
KM
693 u32 fmt;
694 u32 reg;
695 u32 data;
696 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
697 int is_master;
698 int ret = 0;
699
785d1c45 700 pm_runtime_get_sync(dai->dev);
a4d7d550
KM
701
702 /* CKG1 */
703 data = is_play ? (1 << 0) : (1 << 4);
704 is_master = fsi_is_master_mode(fsi, is_play);
705 if (is_master)
706 fsi_reg_mask_set(fsi, CKG1, data, data);
707 else
708 fsi_reg_mask_set(fsi, CKG1, data, 0);
709
710 /* clock inversion (CKG2) */
711 data = 0;
b427b44c
KM
712 if (SH_FSI_LRM_INV & flags)
713 data |= 1 << 12;
714 if (SH_FSI_BRM_INV & flags)
715 data |= 1 << 8;
716 if (SH_FSI_LRS_INV & flags)
717 data |= 1 << 4;
718 if (SH_FSI_BRS_INV & flags)
719 data |= 1 << 0;
720
a4d7d550
KM
721 fsi_reg_write(fsi, CKG2, data);
722
723 /* do fmt, di fmt */
724 data = 0;
725 reg = is_play ? DO_FMT : DI_FMT;
726 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
727 switch (fmt) {
728 case SH_FSI_FMT_MONO:
a7ffb52b 729 data = CR_MONO;
a4d7d550
KM
730 fsi->chan = 1;
731 break;
732 case SH_FSI_FMT_MONO_DELAY:
a7ffb52b 733 data = CR_MONO_D;
a4d7d550
KM
734 fsi->chan = 1;
735 break;
736 case SH_FSI_FMT_PCM:
a7ffb52b 737 data = CR_PCM;
a4d7d550
KM
738 fsi->chan = 2;
739 break;
740 case SH_FSI_FMT_I2S:
a7ffb52b 741 data = CR_I2S;
a4d7d550
KM
742 fsi->chan = 2;
743 break;
744 case SH_FSI_FMT_TDM:
a4d7d550
KM
745 fsi->chan = is_play ?
746 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
a7ffb52b 747 data = CR_TDM | (fsi->chan - 1);
a4d7d550
KM
748 break;
749 case SH_FSI_FMT_TDM_DELAY:
a4d7d550
KM
750 fsi->chan = is_play ?
751 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
a7ffb52b 752 data = CR_TDM_D | (fsi->chan - 1);
a4d7d550 753 break;
3bc28070
KM
754 case SH_FSI_FMT_SPDIF:
755 if (master->core->ver < 2) {
756 dev_err(dai->dev, "This FSI can not use SPDIF\n");
757 return -EINVAL;
758 }
759 data = CR_SPDIF;
760 fsi->chan = 2;
761 fsi_spdif_clk_ctrl(fsi, 1);
762 fsi_reg_mask_set(fsi, OUT_SEL, 0x0010, 0x0010);
763 break;
a4d7d550
KM
764 default:
765 dev_err(dai->dev, "unknown format.\n");
766 return -EINVAL;
767 }
a4d7d550 768 fsi_reg_write(fsi, reg, data);
a4d7d550 769
10ea76cc
KM
770 /* irq clear */
771 fsi_irq_disable(fsi, is_play);
772 fsi_irq_clear_status(fsi);
773
774 /* fifo init */
4a942b45 775 fsi_fifo_init(fsi, is_play, dai);
a4d7d550
KM
776
777 return ret;
778}
779
780static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
781 struct snd_soc_dai *dai)
782{
71f6e064 783 struct fsi_priv *fsi = fsi_get_priv(substream);
a4d7d550
KM
784 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
785
786 fsi_irq_disable(fsi, is_play);
787 fsi_clk_ctrl(fsi, 0);
788
785d1c45 789 pm_runtime_put_sync(dai->dev);
a4d7d550
KM
790}
791
792static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
793 struct snd_soc_dai *dai)
794{
71f6e064 795 struct fsi_priv *fsi = fsi_get_priv(substream);
a4d7d550
KM
796 struct snd_pcm_runtime *runtime = substream->runtime;
797 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
798 int ret = 0;
799
a4d7d550
KM
800 switch (cmd) {
801 case SNDRV_PCM_TRIGGER_START:
802 fsi_stream_push(fsi, substream,
803 frames_to_bytes(runtime, runtime->buffer_size),
804 frames_to_bytes(runtime, runtime->period_size));
47fc9a0a 805 ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
a4d7d550
KM
806 break;
807 case SNDRV_PCM_TRIGGER_STOP:
808 fsi_irq_disable(fsi, is_play);
809 fsi_stream_pop(fsi);
810 break;
811 }
812
813 return ret;
814}
815
ccad7b44
KM
816static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
817 struct snd_pcm_hw_params *params,
818 struct snd_soc_dai *dai)
819{
820 struct fsi_priv *fsi = fsi_get_priv(substream);
821 struct fsi_master *master = fsi_get_master(fsi);
822 int (*set_rate)(int is_porta, int rate) = master->info->set_rate;
823 int fsi_ver = master->core->ver;
824 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
825 int ret;
826
827 /* if slave mode, set_rate is not needed */
828 if (!fsi_is_master_mode(fsi, is_play))
829 return 0;
830
831 /* it is error if no set_rate */
832 if (!set_rate)
833 return -EIO;
834
ccad7b44
KM
835 ret = set_rate(fsi_is_port_a(fsi), params_rate(params));
836 if (ret > 0) {
837 u32 data = 0;
838
839 switch (ret & SH_FSI_ACKMD_MASK) {
840 default:
841 /* FALL THROUGH */
842 case SH_FSI_ACKMD_512:
843 data |= (0x0 << 12);
844 break;
845 case SH_FSI_ACKMD_256:
846 data |= (0x1 << 12);
847 break;
848 case SH_FSI_ACKMD_128:
849 data |= (0x2 << 12);
850 break;
851 case SH_FSI_ACKMD_64:
852 data |= (0x3 << 12);
853 break;
854 case SH_FSI_ACKMD_32:
855 if (fsi_ver < 2)
856 dev_err(dai->dev, "unsupported ACKMD\n");
857 else
858 data |= (0x4 << 12);
859 break;
860 }
861
862 switch (ret & SH_FSI_BPFMD_MASK) {
863 default:
864 /* FALL THROUGH */
865 case SH_FSI_BPFMD_32:
866 data |= (0x0 << 8);
867 break;
868 case SH_FSI_BPFMD_64:
869 data |= (0x1 << 8);
870 break;
871 case SH_FSI_BPFMD_128:
872 data |= (0x2 << 8);
873 break;
874 case SH_FSI_BPFMD_256:
875 data |= (0x3 << 8);
876 break;
877 case SH_FSI_BPFMD_512:
878 data |= (0x4 << 8);
879 break;
880 case SH_FSI_BPFMD_16:
881 if (fsi_ver < 2)
882 dev_err(dai->dev, "unsupported ACKMD\n");
883 else
884 data |= (0x7 << 8);
885 break;
886 }
887
888 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
889 udelay(10);
890 fsi_clk_ctrl(fsi, 1);
891 ret = 0;
892 }
ccad7b44
KM
893
894 return ret;
895
896}
897
a4d7d550
KM
898static struct snd_soc_dai_ops fsi_dai_ops = {
899 .startup = fsi_dai_startup,
900 .shutdown = fsi_dai_shutdown,
901 .trigger = fsi_dai_trigger,
ccad7b44 902 .hw_params = fsi_dai_hw_params,
a4d7d550
KM
903};
904
c8fe2574
KM
905/*
906 * pcm ops
907 */
a4d7d550 908
a4d7d550
KM
909static struct snd_pcm_hardware fsi_pcm_hardware = {
910 .info = SNDRV_PCM_INFO_INTERLEAVED |
911 SNDRV_PCM_INFO_MMAP |
912 SNDRV_PCM_INFO_MMAP_VALID |
913 SNDRV_PCM_INFO_PAUSE,
914 .formats = FSI_FMTS,
915 .rates = FSI_RATES,
916 .rate_min = 8000,
917 .rate_max = 192000,
918 .channels_min = 1,
919 .channels_max = 2,
920 .buffer_bytes_max = 64 * 1024,
921 .period_bytes_min = 32,
922 .period_bytes_max = 8192,
923 .periods_min = 1,
924 .periods_max = 32,
925 .fifo_size = 256,
926};
927
928static int fsi_pcm_open(struct snd_pcm_substream *substream)
929{
930 struct snd_pcm_runtime *runtime = substream->runtime;
931 int ret = 0;
932
933 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
934
935 ret = snd_pcm_hw_constraint_integer(runtime,
936 SNDRV_PCM_HW_PARAM_PERIODS);
937
938 return ret;
939}
940
941static int fsi_hw_params(struct snd_pcm_substream *substream,
942 struct snd_pcm_hw_params *hw_params)
943{
944 return snd_pcm_lib_malloc_pages(substream,
945 params_buffer_bytes(hw_params));
946}
947
948static int fsi_hw_free(struct snd_pcm_substream *substream)
949{
950 return snd_pcm_lib_free_pages(substream);
951}
952
953static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
954{
955 struct snd_pcm_runtime *runtime = substream->runtime;
71f6e064 956 struct fsi_priv *fsi = fsi_get_priv(substream);
a4d7d550
KM
957 long location;
958
9ddc9aa9 959 location = (fsi->byte_offset - 1);
a4d7d550
KM
960 if (location < 0)
961 location = 0;
962
963 return bytes_to_frames(runtime, location);
964}
965
966static struct snd_pcm_ops fsi_pcm_ops = {
967 .open = fsi_pcm_open,
968 .ioctl = snd_pcm_lib_ioctl,
969 .hw_params = fsi_hw_params,
970 .hw_free = fsi_hw_free,
971 .pointer = fsi_pointer,
972};
973
c8fe2574
KM
974/*
975 * snd_soc_platform
976 */
a4d7d550 977
a4d7d550
KM
978#define PREALLOC_BUFFER (32 * 1024)
979#define PREALLOC_BUFFER_MAX (32 * 1024)
980
981static void fsi_pcm_free(struct snd_pcm *pcm)
982{
983 snd_pcm_lib_preallocate_free_for_all(pcm);
984}
985
986static int fsi_pcm_new(struct snd_card *card,
987 struct snd_soc_dai *dai,
988 struct snd_pcm *pcm)
989{
990 /*
991 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
992 * in MMAP mode (i.e. aplay -M)
993 */
994 return snd_pcm_lib_preallocate_pages_for_all(
995 pcm,
996 SNDRV_DMA_TYPE_CONTINUOUS,
997 snd_dma_continuous_data(GFP_KERNEL),
998 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
999}
1000
c8fe2574
KM
1001/*
1002 * alsa struct
1003 */
a4d7d550 1004
f0fba2ad 1005static struct snd_soc_dai_driver fsi_soc_dai[] = {
a4d7d550 1006 {
f0fba2ad 1007 .name = "fsia-dai",
a4d7d550
KM
1008 .playback = {
1009 .rates = FSI_RATES,
1010 .formats = FSI_FMTS,
1011 .channels_min = 1,
1012 .channels_max = 8,
1013 },
07102f3c
KM
1014 .capture = {
1015 .rates = FSI_RATES,
1016 .formats = FSI_FMTS,
1017 .channels_min = 1,
1018 .channels_max = 8,
1019 },
a4d7d550
KM
1020 .ops = &fsi_dai_ops,
1021 },
1022 {
f0fba2ad 1023 .name = "fsib-dai",
a4d7d550
KM
1024 .playback = {
1025 .rates = FSI_RATES,
1026 .formats = FSI_FMTS,
1027 .channels_min = 1,
1028 .channels_max = 8,
1029 },
07102f3c
KM
1030 .capture = {
1031 .rates = FSI_RATES,
1032 .formats = FSI_FMTS,
1033 .channels_min = 1,
1034 .channels_max = 8,
1035 },
a4d7d550
KM
1036 .ops = &fsi_dai_ops,
1037 },
1038};
a4d7d550 1039
f0fba2ad
LG
1040static struct snd_soc_platform_driver fsi_soc_platform = {
1041 .ops = &fsi_pcm_ops,
a4d7d550
KM
1042 .pcm_new = fsi_pcm_new,
1043 .pcm_free = fsi_pcm_free,
1044};
a4d7d550 1045
c8fe2574
KM
1046/*
1047 * platform function
1048 */
a4d7d550 1049
a4d7d550
KM
1050static int fsi_probe(struct platform_device *pdev)
1051{
71f6e064 1052 struct fsi_master *master;
cc780d38 1053 const struct platform_device_id *id_entry;
a4d7d550 1054 struct resource *res;
a4d7d550
KM
1055 unsigned int irq;
1056 int ret;
1057
cc780d38
KM
1058 id_entry = pdev->id_entry;
1059 if (!id_entry) {
1060 dev_err(&pdev->dev, "unknown fsi device\n");
1061 return -ENODEV;
1062 }
1063
a4d7d550
KM
1064 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1065 irq = platform_get_irq(pdev, 0);
b6aa1793 1066 if (!res || (int)irq <= 0) {
a4d7d550
KM
1067 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1068 ret = -ENODEV;
1069 goto exit;
1070 }
1071
1072 master = kzalloc(sizeof(*master), GFP_KERNEL);
1073 if (!master) {
1074 dev_err(&pdev->dev, "Could not allocate master\n");
1075 ret = -ENOMEM;
1076 goto exit;
1077 }
1078
1079 master->base = ioremap_nocache(res->start, resource_size(res));
1080 if (!master->base) {
1081 ret = -ENXIO;
1082 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1083 goto exit_kfree;
1084 }
1085
3bc28070 1086 /* master setting */
a4d7d550
KM
1087 master->irq = irq;
1088 master->info = pdev->dev.platform_data;
3bc28070
KM
1089 master->core = (struct fsi_core *)id_entry->driver_data;
1090 spin_lock_init(&master->lock);
1091
1092 /* FSI A setting */
a4d7d550 1093 master->fsia.base = master->base;
71f6e064 1094 master->fsia.master = master;
3bc28070
KM
1095 master->fsia.mst_ctrl = A_MST_CTLR;
1096
1097 /* FSI B setting */
a4d7d550 1098 master->fsib.base = master->base + 0x40;
71f6e064 1099 master->fsib.master = master;
3bc28070 1100 master->fsib.mst_ctrl = B_MST_CTLR;
a4d7d550 1101
785d1c45
KM
1102 pm_runtime_enable(&pdev->dev);
1103 pm_runtime_resume(&pdev->dev);
f0fba2ad 1104 dev_set_drvdata(&pdev->dev, master);
a4d7d550 1105
71f6e064 1106 fsi_soft_all_reset(master);
a4d7d550 1107
cc780d38
KM
1108 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1109 id_entry->name, master);
a4d7d550
KM
1110 if (ret) {
1111 dev_err(&pdev->dev, "irq request err\n");
9ddc9aa9 1112 goto exit_iounmap;
a4d7d550
KM
1113 }
1114
f0fba2ad 1115 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
a4d7d550
KM
1116 if (ret < 0) {
1117 dev_err(&pdev->dev, "cannot snd soc register\n");
1118 goto exit_free_irq;
1119 }
1120
f0fba2ad 1121 return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
a4d7d550
KM
1122
1123exit_free_irq:
1124 free_irq(irq, master);
a4d7d550
KM
1125exit_iounmap:
1126 iounmap(master->base);
785d1c45 1127 pm_runtime_disable(&pdev->dev);
a4d7d550
KM
1128exit_kfree:
1129 kfree(master);
1130 master = NULL;
1131exit:
1132 return ret;
1133}
1134
1135static int fsi_remove(struct platform_device *pdev)
1136{
71f6e064
KM
1137 struct fsi_master *master;
1138
f0fba2ad 1139 master = dev_get_drvdata(&pdev->dev);
71f6e064 1140
f0fba2ad
LG
1141 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1142 snd_soc_unregister_platform(&pdev->dev);
a4d7d550 1143
785d1c45 1144 pm_runtime_disable(&pdev->dev);
a4d7d550 1145
a4d7d550
KM
1146 free_irq(master->irq, master);
1147
1148 iounmap(master->base);
1149 kfree(master);
71f6e064 1150
a4d7d550
KM
1151 return 0;
1152}
1153
785d1c45
KM
1154static int fsi_runtime_nop(struct device *dev)
1155{
1156 /* Runtime PM callback shared between ->runtime_suspend()
1157 * and ->runtime_resume(). Simply returns success.
1158 *
1159 * This driver re-initializes all registers after
1160 * pm_runtime_get_sync() anyway so there is no need
1161 * to save and restore registers here.
1162 */
1163 return 0;
1164}
1165
1166static struct dev_pm_ops fsi_pm_ops = {
1167 .runtime_suspend = fsi_runtime_nop,
1168 .runtime_resume = fsi_runtime_nop,
1169};
1170
73b92c1f
KM
1171static struct fsi_core fsi1_core = {
1172 .ver = 1,
1173
1174 /* Interrupt */
cc780d38
KM
1175 .int_st = INT_ST,
1176 .iemsk = IEMSK,
1177 .imsk = IMSK,
1178};
1179
73b92c1f
KM
1180static struct fsi_core fsi2_core = {
1181 .ver = 2,
1182
1183 /* Interrupt */
cc780d38
KM
1184 .int_st = CPU_INT_ST,
1185 .iemsk = CPU_IEMSK,
1186 .imsk = CPU_IMSK,
1187};
1188
1189static struct platform_device_id fsi_id_table[] = {
73b92c1f
KM
1190 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1191 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
cc780d38 1192};
d85a6d7b 1193MODULE_DEVICE_TABLE(platform, fsi_id_table);
cc780d38 1194
a4d7d550
KM
1195static struct platform_driver fsi_driver = {
1196 .driver = {
f0fba2ad 1197 .name = "fsi-pcm-audio",
785d1c45 1198 .pm = &fsi_pm_ops,
a4d7d550
KM
1199 },
1200 .probe = fsi_probe,
1201 .remove = fsi_remove,
cc780d38 1202 .id_table = fsi_id_table,
a4d7d550
KM
1203};
1204
1205static int __init fsi_mobile_init(void)
1206{
1207 return platform_driver_register(&fsi_driver);
1208}
1209
1210static void __exit fsi_mobile_exit(void)
1211{
1212 platform_driver_unregister(&fsi_driver);
1213}
d85a6d7b 1214
a4d7d550
KM
1215module_init(fsi_mobile_init);
1216module_exit(fsi_mobile_exit);
1217
1218MODULE_LICENSE("GPL");
1219MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1220MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
This page took 0.158883 seconds and 5 git commands to generate.