blob: db91349b680643112278ffabdbbb7cf0739e0060 [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
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +090082/* SOFT_RST */
83#define PBSR (1 << 12) /* Port B Software Reset */
84#define PASR (1 << 8) /* Port A Software Reset */
85#define IR (1 << 4) /* Interrupt Reset */
86#define FSISR (1 << 0) /* Software Reset */
87
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090088#define FSI_RATES SNDRV_PCM_RATE_8000_96000
89
90#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
91
92/************************************************************************
93
94
95 struct
96
97
98************************************************************************/
99struct fsi_priv {
100 void __iomem *base;
101 struct snd_pcm_substream *substream;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900102 struct fsi_master *master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900103
104 int fifo_max;
105 int chan;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900106
107 int byte_offset;
108 int period_len;
109 int buffer_len;
110 int periods;
111};
112
113struct fsi_master {
114 void __iomem *base;
115 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900116 struct fsi_priv fsia;
117 struct fsi_priv fsib;
118 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900119 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900120};
121
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900122/************************************************************************
123
124
125 basic read write function
126
127
128************************************************************************/
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100129static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900130{
131 /* valid data area is 24bit */
132 data &= 0x00ffffff;
133
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100134 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900135}
136
137static u32 __fsi_reg_read(u32 reg)
138{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100139 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900140}
141
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100142static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900143{
144 u32 val = __fsi_reg_read(reg);
145
146 val &= ~mask;
147 val |= data & mask;
148
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100149 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900150}
151
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100152static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900153{
154 if (reg > REG_END)
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100155 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900156
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100157 __fsi_reg_write((u32)(fsi->base + reg), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900158}
159
160static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
161{
162 if (reg > REG_END)
163 return 0;
164
165 return __fsi_reg_read((u32)(fsi->base + reg));
166}
167
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100168static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900169{
170 if (reg > REG_END)
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100171 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900172
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100173 __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900174}
175
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100176static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900177{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900178 unsigned long flags;
179
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900180 if ((reg < MREG_START) ||
181 (reg > MREG_END))
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100182 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900183
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900184 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100185 __fsi_reg_write((u32)(master->base + reg), data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900186 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900187}
188
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900189static u32 fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900190{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900191 u32 ret;
192 unsigned long flags;
193
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900194 if ((reg < MREG_START) ||
195 (reg > MREG_END))
196 return 0;
197
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900198 spin_lock_irqsave(&master->lock, flags);
199 ret = __fsi_reg_read((u32)(master->base + reg));
200 spin_unlock_irqrestore(&master->lock, flags);
201
202 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900203}
204
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100205static void fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900206 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900207{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900208 unsigned long flags;
209
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900210 if ((reg < MREG_START) ||
211 (reg > MREG_END))
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100212 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900213
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900214 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100215 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900216 spin_unlock_irqrestore(&master->lock, flags);
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 Morimotoa4d7d552009-08-20 21:01:05 +0900381 /* port AB reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900382 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900383 mdelay(10);
384
385 /* soft reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900386 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
387 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900388 mdelay(10);
389}
390
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900391/* playback interrupt */
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900392static int fsi_data_push(struct fsi_priv *fsi, int startup)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900393{
394 struct snd_pcm_runtime *runtime;
395 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900396 u32 status;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900397 int send;
398 int fifo_free;
399 int width;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900400 u8 *start;
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900401 int i, over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900402
403 if (!fsi ||
404 !fsi->substream ||
405 !fsi->substream->runtime)
406 return -EINVAL;
407
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900408 over_period = 0;
409 substream = fsi->substream;
410 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900411
412 /* FSI FIFO has limit.
413 * So, this driver can not send periods data at a time
414 */
415 if (fsi->byte_offset >=
416 fsi->period_len * (fsi->periods + 1)) {
417
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900418 over_period = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900419 fsi->periods = (fsi->periods + 1) % runtime->periods;
420
421 if (0 == fsi->periods)
422 fsi->byte_offset = 0;
423 }
424
425 /* get 1 channel data width */
426 width = frames_to_bytes(runtime, 1) / fsi->chan;
427
428 /* get send size for alsa */
429 send = (fsi->buffer_len - fsi->byte_offset) / width;
430
431 /* get FIFO free size */
432 fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
433
434 /* size check */
435 if (fifo_free < send)
436 send = fifo_free;
437
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900438 start = runtime->dma_area;
439 start += fsi->byte_offset;
440
441 switch (width) {
442 case 2:
443 for (i = 0; i < send; i++)
444 fsi_reg_write(fsi, DODT,
445 ((u32)*((u16 *)start + i) << 8));
446 break;
447 case 4:
448 for (i = 0; i < send; i++)
449 fsi_reg_write(fsi, DODT, *((u32 *)start + i));
450 break;
451 default:
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900452 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900453 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900454
455 fsi->byte_offset += send * width;
456
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900457 status = fsi_reg_read(fsi, DOFF_ST);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900458 if (!startup) {
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900459 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900460
461 if (status & ERR_OVER)
462 dev_err(dai->dev, "over run\n");
463 if (status & ERR_UNDER)
464 dev_err(dai->dev, "under run\n");
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900465 }
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900466 fsi_reg_write(fsi, DOFF_ST, 0);
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900467
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900468 fsi_irq_enable(fsi, 1);
469
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900470 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900471 snd_pcm_period_elapsed(substream);
472
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900473 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900474}
475
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900476static int fsi_data_pop(struct fsi_priv *fsi, int startup)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900477{
478 struct snd_pcm_runtime *runtime;
479 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900480 u32 status;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900481 int free;
482 int fifo_fill;
483 int width;
484 u8 *start;
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900485 int i, over_period;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900486
487 if (!fsi ||
488 !fsi->substream ||
489 !fsi->substream->runtime)
490 return -EINVAL;
491
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900492 over_period = 0;
493 substream = fsi->substream;
494 runtime = substream->runtime;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900495
496 /* FSI FIFO has limit.
497 * So, this driver can not send periods data at a time
498 */
499 if (fsi->byte_offset >=
500 fsi->period_len * (fsi->periods + 1)) {
501
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900502 over_period = 1;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900503 fsi->periods = (fsi->periods + 1) % runtime->periods;
504
505 if (0 == fsi->periods)
506 fsi->byte_offset = 0;
507 }
508
509 /* get 1 channel data width */
510 width = frames_to_bytes(runtime, 1) / fsi->chan;
511
512 /* get free space for alsa */
513 free = (fsi->buffer_len - fsi->byte_offset) / width;
514
515 /* get recv size */
516 fifo_fill = fsi_get_fifo_residue(fsi, 0);
517
518 if (free < fifo_fill)
519 fifo_fill = free;
520
521 start = runtime->dma_area;
522 start += fsi->byte_offset;
523
524 switch (width) {
525 case 2:
526 for (i = 0; i < fifo_fill; i++)
527 *((u16 *)start + i) =
528 (u16)(fsi_reg_read(fsi, DIDT) >> 8);
529 break;
530 case 4:
531 for (i = 0; i < fifo_fill; i++)
532 *((u32 *)start + i) = fsi_reg_read(fsi, DIDT);
533 break;
534 default:
535 return -EINVAL;
536 }
537
538 fsi->byte_offset += fifo_fill * width;
539
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900540 status = fsi_reg_read(fsi, DIFF_ST);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900541 if (!startup) {
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900542 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900543
544 if (status & ERR_OVER)
545 dev_err(dai->dev, "over run\n");
546 if (status & ERR_UNDER)
547 dev_err(dai->dev, "under run\n");
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900548 }
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900549 fsi_reg_write(fsi, DIFF_ST, 0);
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900550
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900551 fsi_irq_enable(fsi, 0);
552
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900553 if (over_period)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900554 snd_pcm_period_elapsed(substream);
555
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900556 return 0;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900557}
558
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900559static irqreturn_t fsi_interrupt(int irq, void *data)
560{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900561 struct fsi_master *master = data;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900562 u32 int_st = fsi_master_read(master, INT_ST);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900563
564 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900565 fsi_master_mask_set(master, SOFT_RST, IR, 0);
566 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900567
568 if (int_st & INT_A_OUT)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900569 fsi_data_push(&master->fsia, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900570 if (int_st & INT_B_OUT)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900571 fsi_data_push(&master->fsib, 0);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900572 if (int_st & INT_A_IN)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900573 fsi_data_pop(&master->fsia, 0);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900574 if (int_st & INT_B_IN)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900575 fsi_data_pop(&master->fsib, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900576
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900577 fsi_master_write(master, INT_ST, 0x0000000);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900578
579 return IRQ_HANDLED;
580}
581
582/************************************************************************
583
584
585 dai ops
586
587
588************************************************************************/
589static int fsi_dai_startup(struct snd_pcm_substream *substream,
590 struct snd_soc_dai *dai)
591{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900592 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900593 const char *msg;
594 u32 flags = fsi_get_info_flags(fsi);
595 u32 fmt;
596 u32 reg;
597 u32 data;
598 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
599 int is_master;
600 int ret = 0;
601
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900602 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900603
604 /* CKG1 */
605 data = is_play ? (1 << 0) : (1 << 4);
606 is_master = fsi_is_master_mode(fsi, is_play);
607 if (is_master)
608 fsi_reg_mask_set(fsi, CKG1, data, data);
609 else
610 fsi_reg_mask_set(fsi, CKG1, data, 0);
611
612 /* clock inversion (CKG2) */
613 data = 0;
614 switch (SH_FSI_INVERSION_MASK & flags) {
615 case SH_FSI_LRM_INV:
616 data = 1 << 12;
617 break;
618 case SH_FSI_BRM_INV:
619 data = 1 << 8;
620 break;
621 case SH_FSI_LRS_INV:
622 data = 1 << 4;
623 break;
624 case SH_FSI_BRS_INV:
625 data = 1 << 0;
626 break;
627 }
628 fsi_reg_write(fsi, CKG2, data);
629
630 /* do fmt, di fmt */
631 data = 0;
632 reg = is_play ? DO_FMT : DI_FMT;
633 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
634 switch (fmt) {
635 case SH_FSI_FMT_MONO:
636 msg = "MONO";
637 data = CR_FMT(CR_MONO);
638 fsi->chan = 1;
639 break;
640 case SH_FSI_FMT_MONO_DELAY:
641 msg = "MONO Delay";
642 data = CR_FMT(CR_MONO_D);
643 fsi->chan = 1;
644 break;
645 case SH_FSI_FMT_PCM:
646 msg = "PCM";
647 data = CR_FMT(CR_PCM);
648 fsi->chan = 2;
649 break;
650 case SH_FSI_FMT_I2S:
651 msg = "I2S";
652 data = CR_FMT(CR_I2S);
653 fsi->chan = 2;
654 break;
655 case SH_FSI_FMT_TDM:
656 msg = "TDM";
657 data = CR_FMT(CR_TDM) | (fsi->chan - 1);
658 fsi->chan = is_play ?
659 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
660 break;
661 case SH_FSI_FMT_TDM_DELAY:
662 msg = "TDM Delay";
663 data = CR_FMT(CR_TDM_D) | (fsi->chan - 1);
664 fsi->chan = is_play ?
665 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
666 break;
667 default:
668 dev_err(dai->dev, "unknown format.\n");
669 return -EINVAL;
670 }
671
672 switch (fsi->chan) {
673 case 1:
674 fsi->fifo_max = 256;
675 break;
676 case 2:
677 fsi->fifo_max = 128;
678 break;
679 case 3:
680 case 4:
681 fsi->fifo_max = 64;
682 break;
683 case 5:
684 case 6:
685 case 7:
686 case 8:
687 fsi->fifo_max = 32;
688 break;
689 default:
690 dev_err(dai->dev, "channel size error.\n");
691 return -EINVAL;
692 }
693
694 fsi_reg_write(fsi, reg, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900695
696 /*
697 * clear clk reset if master mode
698 */
699 if (is_master)
700 fsi_clk_ctrl(fsi, 1);
701
702 /* irq setting */
703 fsi_irq_init(fsi, is_play);
704
705 return ret;
706}
707
708static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
709 struct snd_soc_dai *dai)
710{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900711 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900712 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
713
714 fsi_irq_disable(fsi, is_play);
715 fsi_clk_ctrl(fsi, 0);
716
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900717 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900718}
719
720static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
721 struct snd_soc_dai *dai)
722{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900723 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900724 struct snd_pcm_runtime *runtime = substream->runtime;
725 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
726 int ret = 0;
727
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900728 switch (cmd) {
729 case SNDRV_PCM_TRIGGER_START:
730 fsi_stream_push(fsi, substream,
731 frames_to_bytes(runtime, runtime->buffer_size),
732 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900733 ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900734 break;
735 case SNDRV_PCM_TRIGGER_STOP:
736 fsi_irq_disable(fsi, is_play);
737 fsi_stream_pop(fsi);
738 break;
739 }
740
741 return ret;
742}
743
744static struct snd_soc_dai_ops fsi_dai_ops = {
745 .startup = fsi_dai_startup,
746 .shutdown = fsi_dai_shutdown,
747 .trigger = fsi_dai_trigger,
748};
749
750/************************************************************************
751
752
753 pcm ops
754
755
756************************************************************************/
757static struct snd_pcm_hardware fsi_pcm_hardware = {
758 .info = SNDRV_PCM_INFO_INTERLEAVED |
759 SNDRV_PCM_INFO_MMAP |
760 SNDRV_PCM_INFO_MMAP_VALID |
761 SNDRV_PCM_INFO_PAUSE,
762 .formats = FSI_FMTS,
763 .rates = FSI_RATES,
764 .rate_min = 8000,
765 .rate_max = 192000,
766 .channels_min = 1,
767 .channels_max = 2,
768 .buffer_bytes_max = 64 * 1024,
769 .period_bytes_min = 32,
770 .period_bytes_max = 8192,
771 .periods_min = 1,
772 .periods_max = 32,
773 .fifo_size = 256,
774};
775
776static int fsi_pcm_open(struct snd_pcm_substream *substream)
777{
778 struct snd_pcm_runtime *runtime = substream->runtime;
779 int ret = 0;
780
781 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
782
783 ret = snd_pcm_hw_constraint_integer(runtime,
784 SNDRV_PCM_HW_PARAM_PERIODS);
785
786 return ret;
787}
788
789static int fsi_hw_params(struct snd_pcm_substream *substream,
790 struct snd_pcm_hw_params *hw_params)
791{
792 return snd_pcm_lib_malloc_pages(substream,
793 params_buffer_bytes(hw_params));
794}
795
796static int fsi_hw_free(struct snd_pcm_substream *substream)
797{
798 return snd_pcm_lib_free_pages(substream);
799}
800
801static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
802{
803 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900804 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900805 long location;
806
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900807 location = (fsi->byte_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900808 if (location < 0)
809 location = 0;
810
811 return bytes_to_frames(runtime, location);
812}
813
814static struct snd_pcm_ops fsi_pcm_ops = {
815 .open = fsi_pcm_open,
816 .ioctl = snd_pcm_lib_ioctl,
817 .hw_params = fsi_hw_params,
818 .hw_free = fsi_hw_free,
819 .pointer = fsi_pointer,
820};
821
822/************************************************************************
823
824
825 snd_soc_platform
826
827
828************************************************************************/
829#define PREALLOC_BUFFER (32 * 1024)
830#define PREALLOC_BUFFER_MAX (32 * 1024)
831
832static void fsi_pcm_free(struct snd_pcm *pcm)
833{
834 snd_pcm_lib_preallocate_free_for_all(pcm);
835}
836
837static int fsi_pcm_new(struct snd_card *card,
838 struct snd_soc_dai *dai,
839 struct snd_pcm *pcm)
840{
841 /*
842 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
843 * in MMAP mode (i.e. aplay -M)
844 */
845 return snd_pcm_lib_preallocate_pages_for_all(
846 pcm,
847 SNDRV_DMA_TYPE_CONTINUOUS,
848 snd_dma_continuous_data(GFP_KERNEL),
849 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
850}
851
852/************************************************************************
853
854
855 alsa struct
856
857
858************************************************************************/
859struct snd_soc_dai fsi_soc_dai[] = {
860 {
861 .name = "FSIA",
862 .id = 0,
863 .playback = {
864 .rates = FSI_RATES,
865 .formats = FSI_FMTS,
866 .channels_min = 1,
867 .channels_max = 8,
868 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900869 .capture = {
870 .rates = FSI_RATES,
871 .formats = FSI_FMTS,
872 .channels_min = 1,
873 .channels_max = 8,
874 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900875 .ops = &fsi_dai_ops,
876 },
877 {
878 .name = "FSIB",
879 .id = 1,
880 .playback = {
881 .rates = FSI_RATES,
882 .formats = FSI_FMTS,
883 .channels_min = 1,
884 .channels_max = 8,
885 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900886 .capture = {
887 .rates = FSI_RATES,
888 .formats = FSI_FMTS,
889 .channels_min = 1,
890 .channels_max = 8,
891 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900892 .ops = &fsi_dai_ops,
893 },
894};
895EXPORT_SYMBOL_GPL(fsi_soc_dai);
896
897struct snd_soc_platform fsi_soc_platform = {
898 .name = "fsi-pcm",
899 .pcm_ops = &fsi_pcm_ops,
900 .pcm_new = fsi_pcm_new,
901 .pcm_free = fsi_pcm_free,
902};
903EXPORT_SYMBOL_GPL(fsi_soc_platform);
904
905/************************************************************************
906
907
908 platform function
909
910
911************************************************************************/
912static int fsi_probe(struct platform_device *pdev)
913{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900914 struct fsi_master *master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900915 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900916 unsigned int irq;
917 int ret;
918
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900919 if (0 != pdev->id) {
920 dev_err(&pdev->dev, "current fsi support id 0 only now\n");
921 return -ENODEV;
922 }
923
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900924 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
925 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +0100926 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900927 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
928 ret = -ENODEV;
929 goto exit;
930 }
931
932 master = kzalloc(sizeof(*master), GFP_KERNEL);
933 if (!master) {
934 dev_err(&pdev->dev, "Could not allocate master\n");
935 ret = -ENOMEM;
936 goto exit;
937 }
938
939 master->base = ioremap_nocache(res->start, resource_size(res));
940 if (!master->base) {
941 ret = -ENXIO;
942 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
943 goto exit_kfree;
944 }
945
946 master->irq = irq;
947 master->info = pdev->dev.platform_data;
948 master->fsia.base = master->base;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900949 master->fsia.master = master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900950 master->fsib.base = master->base + 0x40;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900951 master->fsib.master = master;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900952 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900953
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900954 pm_runtime_enable(&pdev->dev);
955 pm_runtime_resume(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900956
957 fsi_soc_dai[0].dev = &pdev->dev;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900958 fsi_soc_dai[0].private_data = &master->fsia;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900959 fsi_soc_dai[1].dev = &pdev->dev;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900960 fsi_soc_dai[1].private_data = &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900961
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900962 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900963
964 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, "fsi", master);
965 if (ret) {
966 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900967 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900968 }
969
970 ret = snd_soc_register_platform(&fsi_soc_platform);
971 if (ret < 0) {
972 dev_err(&pdev->dev, "cannot snd soc register\n");
973 goto exit_free_irq;
974 }
975
976 return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
977
978exit_free_irq:
979 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900980exit_iounmap:
981 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900982 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900983exit_kfree:
984 kfree(master);
985 master = NULL;
986exit:
987 return ret;
988}
989
990static int fsi_remove(struct platform_device *pdev)
991{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900992 struct fsi_master *master;
993
994 master = fsi_get_master(fsi_soc_dai[0].private_data);
995
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900996 snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
997 snd_soc_unregister_platform(&fsi_soc_platform);
998
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900999 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001000
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001001 free_irq(master->irq, master);
1002
1003 iounmap(master->base);
1004 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001005
1006 fsi_soc_dai[0].dev = NULL;
1007 fsi_soc_dai[0].private_data = NULL;
1008 fsi_soc_dai[1].dev = NULL;
1009 fsi_soc_dai[1].private_data = NULL;
1010
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001011 return 0;
1012}
1013
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001014static int fsi_runtime_nop(struct device *dev)
1015{
1016 /* Runtime PM callback shared between ->runtime_suspend()
1017 * and ->runtime_resume(). Simply returns success.
1018 *
1019 * This driver re-initializes all registers after
1020 * pm_runtime_get_sync() anyway so there is no need
1021 * to save and restore registers here.
1022 */
1023 return 0;
1024}
1025
1026static struct dev_pm_ops fsi_pm_ops = {
1027 .runtime_suspend = fsi_runtime_nop,
1028 .runtime_resume = fsi_runtime_nop,
1029};
1030
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001031static struct platform_driver fsi_driver = {
1032 .driver = {
1033 .name = "sh_fsi",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001034 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001035 },
1036 .probe = fsi_probe,
1037 .remove = fsi_remove,
1038};
1039
1040static int __init fsi_mobile_init(void)
1041{
1042 return platform_driver_register(&fsi_driver);
1043}
1044
1045static void __exit fsi_mobile_exit(void)
1046{
1047 platform_driver_unregister(&fsi_driver);
1048}
1049module_init(fsi_mobile_init);
1050module_exit(fsi_mobile_exit);
1051
1052MODULE_LICENSE("GPL");
1053MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1054MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");