blob: ebf358808db118e78647571a4c4ba8be7fa919ab [file] [log] [blame]
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001/*
2 * Fifo-attached Serial Interface (FSI) support for SH7724
3 *
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on ssi.c
8 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/delay.h>
19#include <linux/list.h>
Kuninori Morimoto785d1c42009-11-30 20:24:48 +090020#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090021#include <linux/io.h>
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/initval.h>
25#include <sound/soc.h>
26#include <sound/pcm_params.h>
27#include <sound/sh_fsi.h>
28#include <asm/atomic.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090029
30#define DO_FMT 0x0000
31#define DOFF_CTL 0x0004
32#define DOFF_ST 0x0008
33#define DI_FMT 0x000C
34#define DIFF_CTL 0x0010
35#define DIFF_ST 0x0014
36#define CKG1 0x0018
37#define CKG2 0x001C
38#define DIDT 0x0020
39#define DODT 0x0024
40#define MUTE_ST 0x0028
41#define REG_END MUTE_ST
42
43#define INT_ST 0x0200
44#define IEMSK 0x0204
45#define IMSK 0x0208
46#define MUTE 0x020C
47#define CLK_RST 0x0210
48#define SOFT_RST 0x0214
49#define MREG_START INT_ST
50#define MREG_END SOFT_RST
51
52/* DO_FMT */
53/* DI_FMT */
54#define CR_FMT(param) ((param) << 4)
55# define CR_MONO 0x0
56# define CR_MONO_D 0x1
57# define CR_PCM 0x2
58# define CR_I2S 0x3
59# define CR_TDM 0x4
60# define CR_TDM_D 0x5
61
62/* DOFF_CTL */
63/* DIFF_CTL */
64#define IRQ_HALF 0x00100000
65#define FIFO_CLR 0x00000001
66
67/* DOFF_ST */
68#define ERR_OVER 0x00000010
69#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090070#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090071
72/* CLK_RST */
73#define B_CLK 0x00000010
74#define A_CLK 0x00000001
75
76/* INT_ST */
77#define INT_B_IN (1 << 12)
78#define INT_B_OUT (1 << 8)
79#define INT_A_IN (1 << 4)
80#define INT_A_OUT (1 << 0)
81
82#define FSI_RATES SNDRV_PCM_RATE_8000_96000
83
84#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
85
86/************************************************************************
87
88
89 struct
90
91
92************************************************************************/
93struct fsi_priv {
94 void __iomem *base;
95 struct snd_pcm_substream *substream;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +090096 struct fsi_master *master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090097
98 int fifo_max;
99 int chan;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900100
101 int byte_offset;
102 int period_len;
103 int buffer_len;
104 int periods;
105};
106
107struct fsi_master {
108 void __iomem *base;
109 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900110 struct fsi_priv fsia;
111 struct fsi_priv fsib;
112 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900113 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900114};
115
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900116/************************************************************************
117
118
119 basic read write function
120
121
122************************************************************************/
123static int __fsi_reg_write(u32 reg, u32 data)
124{
125 /* valid data area is 24bit */
126 data &= 0x00ffffff;
127
128 return ctrl_outl(data, reg);
129}
130
131static u32 __fsi_reg_read(u32 reg)
132{
133 return ctrl_inl(reg);
134}
135
136static int __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
137{
138 u32 val = __fsi_reg_read(reg);
139
140 val &= ~mask;
141 val |= data & mask;
142
143 return __fsi_reg_write(reg, val);
144}
145
146static int fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
147{
148 if (reg > REG_END)
149 return -1;
150
151 return __fsi_reg_write((u32)(fsi->base + reg), data);
152}
153
154static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
155{
156 if (reg > REG_END)
157 return 0;
158
159 return __fsi_reg_read((u32)(fsi->base + reg));
160}
161
162static int fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
163{
164 if (reg > REG_END)
165 return -1;
166
167 return __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
168}
169
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900170static int fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900171{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900172 int ret;
173 unsigned long flags;
174
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900175 if ((reg < MREG_START) ||
176 (reg > MREG_END))
177 return -1;
178
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900179 spin_lock_irqsave(&master->lock, flags);
180 ret = __fsi_reg_write((u32)(master->base + reg), data);
181 spin_unlock_irqrestore(&master->lock, flags);
182
183 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900184}
185
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900186static u32 fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900187{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900188 u32 ret;
189 unsigned long flags;
190
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900191 if ((reg < MREG_START) ||
192 (reg > MREG_END))
193 return 0;
194
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900195 spin_lock_irqsave(&master->lock, flags);
196 ret = __fsi_reg_read((u32)(master->base + reg));
197 spin_unlock_irqrestore(&master->lock, flags);
198
199 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900200}
201
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900202static int fsi_master_mask_set(struct fsi_master *master,
203 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900204{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900205 int ret;
206 unsigned long flags;
207
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900208 if ((reg < MREG_START) ||
209 (reg > MREG_END))
210 return -1;
211
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900212 spin_lock_irqsave(&master->lock, flags);
213 ret = __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
214 spin_unlock_irqrestore(&master->lock, flags);
215
216 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900217}
218
219/************************************************************************
220
221
222 basic function
223
224
225************************************************************************/
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900226static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900227{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900228 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900229}
230
231static int fsi_is_port_a(struct fsi_priv *fsi)
232{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900233 return fsi->master->base == fsi->base;
234}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900235
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900236static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900237{
238 struct snd_soc_pcm_runtime *rtd = substream->private_data;
239 struct snd_soc_dai_link *machine = rtd->dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900240
241 return machine->cpu_dai;
242}
243
244static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
245{
246 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900248 return dai->private_data;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900249}
250
251static u32 fsi_get_info_flags(struct fsi_priv *fsi)
252{
253 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900254 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900255
256 return is_porta ? master->info->porta_flags :
257 master->info->portb_flags;
258}
259
260static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
261{
262 u32 mode;
263 u32 flags = fsi_get_info_flags(fsi);
264
265 mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
266
267 /* return
268 * 1 : master mode
269 * 0 : slave mode
270 */
271
272 return (mode & flags) != mode;
273}
274
275static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play)
276{
277 int is_porta = fsi_is_port_a(fsi);
278 u32 data;
279
280 if (is_porta)
281 data = is_play ? (1 << 0) : (1 << 4);
282 else
283 data = is_play ? (1 << 8) : (1 << 12);
284
285 return data;
286}
287
288static void fsi_stream_push(struct fsi_priv *fsi,
289 struct snd_pcm_substream *substream,
290 u32 buffer_len,
291 u32 period_len)
292{
293 fsi->substream = substream;
294 fsi->buffer_len = buffer_len;
295 fsi->period_len = period_len;
296 fsi->byte_offset = 0;
297 fsi->periods = 0;
298}
299
300static void fsi_stream_pop(struct fsi_priv *fsi)
301{
302 fsi->substream = NULL;
303 fsi->buffer_len = 0;
304 fsi->period_len = 0;
305 fsi->byte_offset = 0;
306 fsi->periods = 0;
307}
308
309static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
310{
311 u32 status;
312 u32 reg = is_play ? DOFF_ST : DIFF_ST;
313 int residue;
314
315 status = fsi_reg_read(fsi, reg);
316 residue = 0x1ff & (status >> 8);
317 residue *= fsi->chan;
318
319 return residue;
320}
321
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900322/************************************************************************
323
324
325 ctrl function
326
327
328************************************************************************/
329static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
330{
331 u32 data = fsi_port_ab_io_bit(fsi, is_play);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900332 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900333
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900334 fsi_master_mask_set(master, IMSK, data, data);
335 fsi_master_mask_set(master, IEMSK, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900336}
337
338static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
339{
340 u32 data = fsi_port_ab_io_bit(fsi, is_play);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900341 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900342
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900343 fsi_master_mask_set(master, IMSK, data, 0);
344 fsi_master_mask_set(master, IEMSK, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900345}
346
347static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
348{
349 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900350 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900351
352 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900353 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900354 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900355 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900356}
357
358static void fsi_irq_init(struct fsi_priv *fsi, int is_play)
359{
360 u32 data;
361 u32 ctrl;
362
363 data = fsi_port_ab_io_bit(fsi, is_play);
364 ctrl = is_play ? DOFF_CTL : DIFF_CTL;
365
366 /* set IMSK */
367 fsi_irq_disable(fsi, is_play);
368
369 /* set interrupt generation factor */
370 fsi_reg_write(fsi, ctrl, IRQ_HALF);
371
372 /* clear FIFO */
373 fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
374
375 /* clear interrupt factor */
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900376 fsi_master_mask_set(fsi_get_master(fsi), INT_ST, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900377}
378
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900379static void fsi_soft_all_reset(struct fsi_master *master)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900380{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900381 u32 status = fsi_master_read(master, SOFT_RST);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900382
383 /* port AB reset */
384 status &= 0x000000ff;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900385 fsi_master_write(master, SOFT_RST, status);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900386 mdelay(10);
387
388 /* soft reset */
389 status &= 0x000000f0;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900390 fsi_master_write(master, SOFT_RST, status);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900391 status |= 0x00000001;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900392 fsi_master_write(master, SOFT_RST, status);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900393 mdelay(10);
394}
395
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900396/* playback interrupt */
397static int fsi_data_push(struct fsi_priv *fsi)
398{
399 struct snd_pcm_runtime *runtime;
400 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900401 u32 status;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900402 int send;
403 int fifo_free;
404 int width;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900405 u8 *start;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900406 int i, ret, over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900407
408 if (!fsi ||
409 !fsi->substream ||
410 !fsi->substream->runtime)
411 return -EINVAL;
412
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900413 over_period = 0;
414 substream = fsi->substream;
415 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900416
417 /* FSI FIFO has limit.
418 * So, this driver can not send periods data at a time
419 */
420 if (fsi->byte_offset >=
421 fsi->period_len * (fsi->periods + 1)) {
422
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900423 over_period = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900424 fsi->periods = (fsi->periods + 1) % runtime->periods;
425
426 if (0 == fsi->periods)
427 fsi->byte_offset = 0;
428 }
429
430 /* get 1 channel data width */
431 width = frames_to_bytes(runtime, 1) / fsi->chan;
432
433 /* get send size for alsa */
434 send = (fsi->buffer_len - fsi->byte_offset) / width;
435
436 /* get FIFO free size */
437 fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
438
439 /* size check */
440 if (fifo_free < send)
441 send = fifo_free;
442
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900443 start = runtime->dma_area;
444 start += fsi->byte_offset;
445
446 switch (width) {
447 case 2:
448 for (i = 0; i < send; i++)
449 fsi_reg_write(fsi, DODT,
450 ((u32)*((u16 *)start + i) << 8));
451 break;
452 case 4:
453 for (i = 0; i < send; i++)
454 fsi_reg_write(fsi, DODT, *((u32 *)start + i));
455 break;
456 default:
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900457 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900458 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900459
460 fsi->byte_offset += send * width;
461
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900462 ret = 0;
463 status = fsi_reg_read(fsi, DOFF_ST);
464 if (status & ERR_OVER) {
465 struct snd_soc_dai *dai = fsi_get_dai(substream);
466 dev_err(dai->dev, "over run error\n");
467 fsi_reg_write(fsi, DOFF_ST, status & ~ST_ERR);
468 ret = -EIO;
469 }
470
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900471 fsi_irq_enable(fsi, 1);
472
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900473 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900474 snd_pcm_period_elapsed(substream);
475
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900476 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900477}
478
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900479static int fsi_data_pop(struct fsi_priv *fsi)
480{
481 struct snd_pcm_runtime *runtime;
482 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900483 u32 status;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900484 int free;
485 int fifo_fill;
486 int width;
487 u8 *start;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900488 int i, ret, over_period;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900489
490 if (!fsi ||
491 !fsi->substream ||
492 !fsi->substream->runtime)
493 return -EINVAL;
494
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900495 over_period = 0;
496 substream = fsi->substream;
497 runtime = substream->runtime;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900498
499 /* FSI FIFO has limit.
500 * So, this driver can not send periods data at a time
501 */
502 if (fsi->byte_offset >=
503 fsi->period_len * (fsi->periods + 1)) {
504
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900505 over_period = 1;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900506 fsi->periods = (fsi->periods + 1) % runtime->periods;
507
508 if (0 == fsi->periods)
509 fsi->byte_offset = 0;
510 }
511
512 /* get 1 channel data width */
513 width = frames_to_bytes(runtime, 1) / fsi->chan;
514
515 /* get free space for alsa */
516 free = (fsi->buffer_len - fsi->byte_offset) / width;
517
518 /* get recv size */
519 fifo_fill = fsi_get_fifo_residue(fsi, 0);
520
521 if (free < fifo_fill)
522 fifo_fill = free;
523
524 start = runtime->dma_area;
525 start += fsi->byte_offset;
526
527 switch (width) {
528 case 2:
529 for (i = 0; i < fifo_fill; i++)
530 *((u16 *)start + i) =
531 (u16)(fsi_reg_read(fsi, DIDT) >> 8);
532 break;
533 case 4:
534 for (i = 0; i < fifo_fill; i++)
535 *((u32 *)start + i) = fsi_reg_read(fsi, DIDT);
536 break;
537 default:
538 return -EINVAL;
539 }
540
541 fsi->byte_offset += fifo_fill * width;
542
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900543 ret = 0;
544 status = fsi_reg_read(fsi, DIFF_ST);
545 if (status & ERR_UNDER) {
546 struct snd_soc_dai *dai = fsi_get_dai(substream);
547 dev_err(dai->dev, "under run error\n");
548 fsi_reg_write(fsi, DIFF_ST, status & ~ST_ERR);
549 ret = -EIO;
550 }
551
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900552 fsi_irq_enable(fsi, 0);
553
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900554 if (over_period)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900555 snd_pcm_period_elapsed(substream);
556
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900557 return ret;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900558}
559
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900560static irqreturn_t fsi_interrupt(int irq, void *data)
561{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900562 struct fsi_master *master = data;
563 u32 status = fsi_master_read(master, SOFT_RST) & ~0x00000010;
564 u32 int_st = fsi_master_read(master, INT_ST);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900565
566 /* clear irq status */
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900567 fsi_master_write(master, SOFT_RST, status);
568 fsi_master_write(master, SOFT_RST, status | 0x00000010);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900569
570 if (int_st & INT_A_OUT)
571 fsi_data_push(&master->fsia);
572 if (int_st & INT_B_OUT)
573 fsi_data_push(&master->fsib);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900574 if (int_st & INT_A_IN)
575 fsi_data_pop(&master->fsia);
576 if (int_st & INT_B_IN)
577 fsi_data_pop(&master->fsib);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900578
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900579 fsi_master_write(master, INT_ST, 0x0000000);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900580
581 return IRQ_HANDLED;
582}
583
584/************************************************************************
585
586
587 dai ops
588
589
590************************************************************************/
591static int fsi_dai_startup(struct snd_pcm_substream *substream,
592 struct snd_soc_dai *dai)
593{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900594 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900595 const char *msg;
596 u32 flags = fsi_get_info_flags(fsi);
597 u32 fmt;
598 u32 reg;
599 u32 data;
600 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
601 int is_master;
602 int ret = 0;
603
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900604 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900605
606 /* CKG1 */
607 data = is_play ? (1 << 0) : (1 << 4);
608 is_master = fsi_is_master_mode(fsi, is_play);
609 if (is_master)
610 fsi_reg_mask_set(fsi, CKG1, data, data);
611 else
612 fsi_reg_mask_set(fsi, CKG1, data, 0);
613
614 /* clock inversion (CKG2) */
615 data = 0;
616 switch (SH_FSI_INVERSION_MASK & flags) {
617 case SH_FSI_LRM_INV:
618 data = 1 << 12;
619 break;
620 case SH_FSI_BRM_INV:
621 data = 1 << 8;
622 break;
623 case SH_FSI_LRS_INV:
624 data = 1 << 4;
625 break;
626 case SH_FSI_BRS_INV:
627 data = 1 << 0;
628 break;
629 }
630 fsi_reg_write(fsi, CKG2, data);
631
632 /* do fmt, di fmt */
633 data = 0;
634 reg = is_play ? DO_FMT : DI_FMT;
635 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
636 switch (fmt) {
637 case SH_FSI_FMT_MONO:
638 msg = "MONO";
639 data = CR_FMT(CR_MONO);
640 fsi->chan = 1;
641 break;
642 case SH_FSI_FMT_MONO_DELAY:
643 msg = "MONO Delay";
644 data = CR_FMT(CR_MONO_D);
645 fsi->chan = 1;
646 break;
647 case SH_FSI_FMT_PCM:
648 msg = "PCM";
649 data = CR_FMT(CR_PCM);
650 fsi->chan = 2;
651 break;
652 case SH_FSI_FMT_I2S:
653 msg = "I2S";
654 data = CR_FMT(CR_I2S);
655 fsi->chan = 2;
656 break;
657 case SH_FSI_FMT_TDM:
658 msg = "TDM";
659 data = CR_FMT(CR_TDM) | (fsi->chan - 1);
660 fsi->chan = is_play ?
661 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
662 break;
663 case SH_FSI_FMT_TDM_DELAY:
664 msg = "TDM Delay";
665 data = CR_FMT(CR_TDM_D) | (fsi->chan - 1);
666 fsi->chan = is_play ?
667 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
668 break;
669 default:
670 dev_err(dai->dev, "unknown format.\n");
671 return -EINVAL;
672 }
673
674 switch (fsi->chan) {
675 case 1:
676 fsi->fifo_max = 256;
677 break;
678 case 2:
679 fsi->fifo_max = 128;
680 break;
681 case 3:
682 case 4:
683 fsi->fifo_max = 64;
684 break;
685 case 5:
686 case 6:
687 case 7:
688 case 8:
689 fsi->fifo_max = 32;
690 break;
691 default:
692 dev_err(dai->dev, "channel size error.\n");
693 return -EINVAL;
694 }
695
696 fsi_reg_write(fsi, reg, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900697
698 /*
699 * clear clk reset if master mode
700 */
701 if (is_master)
702 fsi_clk_ctrl(fsi, 1);
703
704 /* irq setting */
705 fsi_irq_init(fsi, is_play);
706
707 return ret;
708}
709
710static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
711 struct snd_soc_dai *dai)
712{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900713 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900714 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
715
716 fsi_irq_disable(fsi, is_play);
717 fsi_clk_ctrl(fsi, 0);
718
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900719 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900720}
721
722static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
723 struct snd_soc_dai *dai)
724{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900725 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900726 struct snd_pcm_runtime *runtime = substream->runtime;
727 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
728 int ret = 0;
729
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900730 switch (cmd) {
731 case SNDRV_PCM_TRIGGER_START:
732 fsi_stream_push(fsi, substream,
733 frames_to_bytes(runtime, runtime->buffer_size),
734 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900735 ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900736 break;
737 case SNDRV_PCM_TRIGGER_STOP:
738 fsi_irq_disable(fsi, is_play);
739 fsi_stream_pop(fsi);
740 break;
741 }
742
743 return ret;
744}
745
746static struct snd_soc_dai_ops fsi_dai_ops = {
747 .startup = fsi_dai_startup,
748 .shutdown = fsi_dai_shutdown,
749 .trigger = fsi_dai_trigger,
750};
751
752/************************************************************************
753
754
755 pcm ops
756
757
758************************************************************************/
759static struct snd_pcm_hardware fsi_pcm_hardware = {
760 .info = SNDRV_PCM_INFO_INTERLEAVED |
761 SNDRV_PCM_INFO_MMAP |
762 SNDRV_PCM_INFO_MMAP_VALID |
763 SNDRV_PCM_INFO_PAUSE,
764 .formats = FSI_FMTS,
765 .rates = FSI_RATES,
766 .rate_min = 8000,
767 .rate_max = 192000,
768 .channels_min = 1,
769 .channels_max = 2,
770 .buffer_bytes_max = 64 * 1024,
771 .period_bytes_min = 32,
772 .period_bytes_max = 8192,
773 .periods_min = 1,
774 .periods_max = 32,
775 .fifo_size = 256,
776};
777
778static int fsi_pcm_open(struct snd_pcm_substream *substream)
779{
780 struct snd_pcm_runtime *runtime = substream->runtime;
781 int ret = 0;
782
783 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
784
785 ret = snd_pcm_hw_constraint_integer(runtime,
786 SNDRV_PCM_HW_PARAM_PERIODS);
787
788 return ret;
789}
790
791static int fsi_hw_params(struct snd_pcm_substream *substream,
792 struct snd_pcm_hw_params *hw_params)
793{
794 return snd_pcm_lib_malloc_pages(substream,
795 params_buffer_bytes(hw_params));
796}
797
798static int fsi_hw_free(struct snd_pcm_substream *substream)
799{
800 return snd_pcm_lib_free_pages(substream);
801}
802
803static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
804{
805 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900806 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900807 long location;
808
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900809 location = (fsi->byte_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900810 if (location < 0)
811 location = 0;
812
813 return bytes_to_frames(runtime, location);
814}
815
816static struct snd_pcm_ops fsi_pcm_ops = {
817 .open = fsi_pcm_open,
818 .ioctl = snd_pcm_lib_ioctl,
819 .hw_params = fsi_hw_params,
820 .hw_free = fsi_hw_free,
821 .pointer = fsi_pointer,
822};
823
824/************************************************************************
825
826
827 snd_soc_platform
828
829
830************************************************************************/
831#define PREALLOC_BUFFER (32 * 1024)
832#define PREALLOC_BUFFER_MAX (32 * 1024)
833
834static void fsi_pcm_free(struct snd_pcm *pcm)
835{
836 snd_pcm_lib_preallocate_free_for_all(pcm);
837}
838
839static int fsi_pcm_new(struct snd_card *card,
840 struct snd_soc_dai *dai,
841 struct snd_pcm *pcm)
842{
843 /*
844 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
845 * in MMAP mode (i.e. aplay -M)
846 */
847 return snd_pcm_lib_preallocate_pages_for_all(
848 pcm,
849 SNDRV_DMA_TYPE_CONTINUOUS,
850 snd_dma_continuous_data(GFP_KERNEL),
851 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
852}
853
854/************************************************************************
855
856
857 alsa struct
858
859
860************************************************************************/
861struct snd_soc_dai fsi_soc_dai[] = {
862 {
863 .name = "FSIA",
864 .id = 0,
865 .playback = {
866 .rates = FSI_RATES,
867 .formats = FSI_FMTS,
868 .channels_min = 1,
869 .channels_max = 8,
870 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900871 .capture = {
872 .rates = FSI_RATES,
873 .formats = FSI_FMTS,
874 .channels_min = 1,
875 .channels_max = 8,
876 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900877 .ops = &fsi_dai_ops,
878 },
879 {
880 .name = "FSIB",
881 .id = 1,
882 .playback = {
883 .rates = FSI_RATES,
884 .formats = FSI_FMTS,
885 .channels_min = 1,
886 .channels_max = 8,
887 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900888 .capture = {
889 .rates = FSI_RATES,
890 .formats = FSI_FMTS,
891 .channels_min = 1,
892 .channels_max = 8,
893 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900894 .ops = &fsi_dai_ops,
895 },
896};
897EXPORT_SYMBOL_GPL(fsi_soc_dai);
898
899struct snd_soc_platform fsi_soc_platform = {
900 .name = "fsi-pcm",
901 .pcm_ops = &fsi_pcm_ops,
902 .pcm_new = fsi_pcm_new,
903 .pcm_free = fsi_pcm_free,
904};
905EXPORT_SYMBOL_GPL(fsi_soc_platform);
906
907/************************************************************************
908
909
910 platform function
911
912
913************************************************************************/
914static int fsi_probe(struct platform_device *pdev)
915{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900916 struct fsi_master *master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900917 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900918 unsigned int irq;
919 int ret;
920
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900921 if (0 != pdev->id) {
922 dev_err(&pdev->dev, "current fsi support id 0 only now\n");
923 return -ENODEV;
924 }
925
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900926 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
927 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +0100928 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900929 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
930 ret = -ENODEV;
931 goto exit;
932 }
933
934 master = kzalloc(sizeof(*master), GFP_KERNEL);
935 if (!master) {
936 dev_err(&pdev->dev, "Could not allocate master\n");
937 ret = -ENOMEM;
938 goto exit;
939 }
940
941 master->base = ioremap_nocache(res->start, resource_size(res));
942 if (!master->base) {
943 ret = -ENXIO;
944 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
945 goto exit_kfree;
946 }
947
948 master->irq = irq;
949 master->info = pdev->dev.platform_data;
950 master->fsia.base = master->base;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900951 master->fsia.master = master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900952 master->fsib.base = master->base + 0x40;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900953 master->fsib.master = master;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900954 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900955
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900956 pm_runtime_enable(&pdev->dev);
957 pm_runtime_resume(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900958
959 fsi_soc_dai[0].dev = &pdev->dev;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900960 fsi_soc_dai[0].private_data = &master->fsia;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900961 fsi_soc_dai[1].dev = &pdev->dev;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900962 fsi_soc_dai[1].private_data = &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900963
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900964 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900965
966 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, "fsi", master);
967 if (ret) {
968 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900969 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900970 }
971
972 ret = snd_soc_register_platform(&fsi_soc_platform);
973 if (ret < 0) {
974 dev_err(&pdev->dev, "cannot snd soc register\n");
975 goto exit_free_irq;
976 }
977
978 return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
979
980exit_free_irq:
981 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900982exit_iounmap:
983 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900984 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900985exit_kfree:
986 kfree(master);
987 master = NULL;
988exit:
989 return ret;
990}
991
992static int fsi_remove(struct platform_device *pdev)
993{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900994 struct fsi_master *master;
995
996 master = fsi_get_master(fsi_soc_dai[0].private_data);
997
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900998 snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
999 snd_soc_unregister_platform(&fsi_soc_platform);
1000
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001001 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001002
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001003 free_irq(master->irq, master);
1004
1005 iounmap(master->base);
1006 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001007
1008 fsi_soc_dai[0].dev = NULL;
1009 fsi_soc_dai[0].private_data = NULL;
1010 fsi_soc_dai[1].dev = NULL;
1011 fsi_soc_dai[1].private_data = NULL;
1012
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001013 return 0;
1014}
1015
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001016static int fsi_runtime_nop(struct device *dev)
1017{
1018 /* Runtime PM callback shared between ->runtime_suspend()
1019 * and ->runtime_resume(). Simply returns success.
1020 *
1021 * This driver re-initializes all registers after
1022 * pm_runtime_get_sync() anyway so there is no need
1023 * to save and restore registers here.
1024 */
1025 return 0;
1026}
1027
1028static struct dev_pm_ops fsi_pm_ops = {
1029 .runtime_suspend = fsi_runtime_nop,
1030 .runtime_resume = fsi_runtime_nop,
1031};
1032
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001033static struct platform_driver fsi_driver = {
1034 .driver = {
1035 .name = "sh_fsi",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001036 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001037 },
1038 .probe = fsi_probe,
1039 .remove = fsi_remove,
1040};
1041
1042static int __init fsi_mobile_init(void)
1043{
1044 return platform_driver_register(&fsi_driver);
1045}
1046
1047static void __exit fsi_mobile_exit(void)
1048{
1049 platform_driver_unregister(&fsi_driver);
1050}
1051module_init(fsi_mobile_init);
1052module_exit(fsi_mobile_exit);
1053
1054MODULE_LICENSE("GPL");
1055MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1056MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");