blob: 3396a0db06ba08d795b942eed61ba42d81697e62 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/initval.h>
26#include <sound/soc.h>
27#include <sound/pcm_params.h>
28#include <sound/sh_fsi.h>
29#include <asm/atomic.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090030
31#define DO_FMT 0x0000
32#define DOFF_CTL 0x0004
33#define DOFF_ST 0x0008
34#define DI_FMT 0x000C
35#define DIFF_CTL 0x0010
36#define DIFF_ST 0x0014
37#define CKG1 0x0018
38#define CKG2 0x001C
39#define DIDT 0x0020
40#define DODT 0x0024
41#define MUTE_ST 0x0028
42#define REG_END MUTE_ST
43
Kuninori Morimotocc780d32010-03-25 19:15:53 +090044
45#define CPU_INT_ST 0x01F4
46#define CPU_IEMSK 0x01F8
47#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090048#define INT_ST 0x0200
49#define IEMSK 0x0204
50#define IMSK 0x0208
51#define MUTE 0x020C
52#define CLK_RST 0x0210
53#define SOFT_RST 0x0214
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090054#define FIFO_SZ 0x0218
Kuninori Morimotocc780d32010-03-25 19:15:53 +090055#define MREG_START CPU_INT_ST
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090056#define MREG_END FIFO_SZ
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090057
58/* DO_FMT */
59/* DI_FMT */
60#define CR_FMT(param) ((param) << 4)
61# define CR_MONO 0x0
62# define CR_MONO_D 0x1
63# define CR_PCM 0x2
64# define CR_I2S 0x3
65# define CR_TDM 0x4
66# define CR_TDM_D 0x5
67
68/* DOFF_CTL */
69/* DIFF_CTL */
70#define IRQ_HALF 0x00100000
71#define FIFO_CLR 0x00000001
72
73/* DOFF_ST */
74#define ERR_OVER 0x00000010
75#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090076#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090077
78/* CLK_RST */
79#define B_CLK 0x00000010
80#define A_CLK 0x00000001
81
82/* INT_ST */
83#define INT_B_IN (1 << 12)
84#define INT_B_OUT (1 << 8)
85#define INT_A_IN (1 << 4)
86#define INT_A_OUT (1 << 0)
87
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +090088/* SOFT_RST */
89#define PBSR (1 << 12) /* Port B Software Reset */
90#define PASR (1 << 8) /* Port A Software Reset */
91#define IR (1 << 4) /* Interrupt Reset */
92#define FSISR (1 << 0) /* Software Reset */
93
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090094/* FIFO_SZ */
95#define OUT_SZ_MASK 0x7
96#define BO_SZ_SHIFT 8
97#define AO_SZ_SHIFT 0
98
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090099#define FSI_RATES SNDRV_PCM_RATE_8000_96000
100
101#define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102
103/************************************************************************
104
105
106 struct
107
108
109************************************************************************/
110struct fsi_priv {
111 void __iomem *base;
112 struct snd_pcm_substream *substream;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900113 struct fsi_master *master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900114
115 int fifo_max;
116 int chan;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900117
118 int byte_offset;
119 int period_len;
120 int buffer_len;
121 int periods;
122};
123
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900124struct fsi_regs {
125 u32 int_st;
126 u32 iemsk;
127 u32 imsk;
128};
129
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900130struct fsi_master {
131 void __iomem *base;
132 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900133 struct fsi_priv fsia;
134 struct fsi_priv fsib;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900135 struct fsi_regs *regs;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900136 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900137 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900138};
139
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900140/************************************************************************
141
142
143 basic read write function
144
145
146************************************************************************/
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100147static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900148{
149 /* valid data area is 24bit */
150 data &= 0x00ffffff;
151
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100152 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900153}
154
155static u32 __fsi_reg_read(u32 reg)
156{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100157 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900158}
159
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100160static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900161{
162 u32 val = __fsi_reg_read(reg);
163
164 val &= ~mask;
165 val |= data & mask;
166
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100167 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900168}
169
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100170static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900171{
172 if (reg > REG_END)
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100173 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900174
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100175 __fsi_reg_write((u32)(fsi->base + reg), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900176}
177
178static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
179{
180 if (reg > REG_END)
181 return 0;
182
183 return __fsi_reg_read((u32)(fsi->base + reg));
184}
185
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100186static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900187{
188 if (reg > REG_END)
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100189 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900190
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100191 __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900192}
193
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100194static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900195{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900196 unsigned long flags;
197
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900198 if ((reg < MREG_START) ||
199 (reg > MREG_END))
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100200 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900201
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900202 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100203 __fsi_reg_write((u32)(master->base + reg), data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900204 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900205}
206
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900207static u32 fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900208{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900209 u32 ret;
210 unsigned long flags;
211
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900212 if ((reg < MREG_START) ||
213 (reg > MREG_END))
214 return 0;
215
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900216 spin_lock_irqsave(&master->lock, flags);
217 ret = __fsi_reg_read((u32)(master->base + reg));
218 spin_unlock_irqrestore(&master->lock, flags);
219
220 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900221}
222
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100223static void fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900224 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900225{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900226 unsigned long flags;
227
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900228 if ((reg < MREG_START) ||
229 (reg > MREG_END))
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100230 return;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900231
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900232 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100233 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900234 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900235}
236
237/************************************************************************
238
239
240 basic function
241
242
243************************************************************************/
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900244static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900245{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900246 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900247}
248
249static int fsi_is_port_a(struct fsi_priv *fsi)
250{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900251 return fsi->master->base == fsi->base;
252}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900253
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900254static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900255{
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai_link *machine = rtd->dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900258
259 return machine->cpu_dai;
260}
261
262static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
263{
264 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900265
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900266 return dai->private_data;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900267}
268
269static u32 fsi_get_info_flags(struct fsi_priv *fsi)
270{
271 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900272 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900273
274 return is_porta ? master->info->porta_flags :
275 master->info->portb_flags;
276}
277
278static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
279{
280 u32 mode;
281 u32 flags = fsi_get_info_flags(fsi);
282
283 mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
284
285 /* return
286 * 1 : master mode
287 * 0 : slave mode
288 */
289
290 return (mode & flags) != mode;
291}
292
293static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play)
294{
295 int is_porta = fsi_is_port_a(fsi);
296 u32 data;
297
298 if (is_porta)
299 data = is_play ? (1 << 0) : (1 << 4);
300 else
301 data = is_play ? (1 << 8) : (1 << 12);
302
303 return data;
304}
305
306static void fsi_stream_push(struct fsi_priv *fsi,
307 struct snd_pcm_substream *substream,
308 u32 buffer_len,
309 u32 period_len)
310{
311 fsi->substream = substream;
312 fsi->buffer_len = buffer_len;
313 fsi->period_len = period_len;
314 fsi->byte_offset = 0;
315 fsi->periods = 0;
316}
317
318static void fsi_stream_pop(struct fsi_priv *fsi)
319{
320 fsi->substream = NULL;
321 fsi->buffer_len = 0;
322 fsi->period_len = 0;
323 fsi->byte_offset = 0;
324 fsi->periods = 0;
325}
326
327static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
328{
329 u32 status;
330 u32 reg = is_play ? DOFF_ST : DIFF_ST;
331 int residue;
332
333 status = fsi_reg_read(fsi, reg);
334 residue = 0x1ff & (status >> 8);
335 residue *= fsi->chan;
336
337 return residue;
338}
339
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900340/************************************************************************
341
342
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900343 irq function
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900344
345
346************************************************************************/
347static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
348{
349 u32 data = fsi_port_ab_io_bit(fsi, is_play);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900350 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900351
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900352 fsi_master_mask_set(master, master->regs->imsk, data, data);
353 fsi_master_mask_set(master, master->regs->iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900354}
355
356static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
357{
358 u32 data = fsi_port_ab_io_bit(fsi, is_play);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900359 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900360
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900361 fsi_master_mask_set(master, master->regs->imsk, data, 0);
362 fsi_master_mask_set(master, master->regs->iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900363}
364
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900365static u32 fsi_irq_get_status(struct fsi_master *master)
366{
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900367 return fsi_master_read(master, master->regs->int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900368}
369
370static void fsi_irq_clear_all_status(struct fsi_master *master)
371{
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900372 fsi_master_write(master, master->regs->int_st, 0x0000000);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900373}
374
375static void fsi_irq_clear_status(struct fsi_priv *fsi)
376{
377 u32 data = 0;
378 struct fsi_master *master = fsi_get_master(fsi);
379
380 data |= fsi_port_ab_io_bit(fsi, 0);
381 data |= fsi_port_ab_io_bit(fsi, 1);
382
383 /* clear interrupt factor */
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900384 fsi_master_mask_set(master, master->regs->int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900385}
386
387/************************************************************************
388
389
390 ctrl function
391
392
393************************************************************************/
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900394static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
395{
396 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900397 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900398
399 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900400 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900401 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900402 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900403}
404
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900405static void fsi_fifo_init(struct fsi_priv *fsi,
406 int is_play,
407 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900408{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900409 struct fsi_master *master = fsi_get_master(fsi);
410 u32 ctrl, shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900411
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900412 /* get on-chip RAM capacity */
413 shift = fsi_master_read(master, FIFO_SZ);
414 shift >>= fsi_is_port_a(fsi) ? AO_SZ_SHIFT : BO_SZ_SHIFT;
415 shift &= OUT_SZ_MASK;
416 fsi->fifo_max = 256 << shift;
417 dev_dbg(dai->dev, "fifo = %d words\n", fsi->fifo_max);
418
419 /*
420 * The maximum number of sample data varies depending
421 * on the number of channels selected for the format.
422 *
423 * FIFOs are used in 4-channel units in 3-channel mode
424 * and in 8-channel units in 5- to 7-channel mode
425 * meaning that more FIFOs than the required size of DPRAM
426 * are used.
427 *
428 * ex) if 256 words of DP-RAM is connected
429 * 1 channel: 256 (256 x 1 = 256)
430 * 2 channels: 128 (128 x 2 = 256)
431 * 3 channels: 64 ( 64 x 3 = 192)
432 * 4 channels: 64 ( 64 x 4 = 256)
433 * 5 channels: 32 ( 32 x 5 = 160)
434 * 6 channels: 32 ( 32 x 6 = 192)
435 * 7 channels: 32 ( 32 x 7 = 224)
436 * 8 channels: 32 ( 32 x 8 = 256)
437 */
438 for (i = 1; i < fsi->chan; i <<= 1)
439 fsi->fifo_max >>= 1;
440 dev_dbg(dai->dev, "%d channel %d store\n", fsi->chan, fsi->fifo_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900441
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900442 ctrl = is_play ? DOFF_CTL : DIFF_CTL;
443
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900444 /* set interrupt generation factor */
445 fsi_reg_write(fsi, ctrl, IRQ_HALF);
446
447 /* clear FIFO */
448 fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900449}
450
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900451static void fsi_soft_all_reset(struct fsi_master *master)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900452{
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900453 /* port AB reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900454 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900455 mdelay(10);
456
457 /* soft reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900458 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
459 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900460 mdelay(10);
461}
462
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900463/* playback interrupt */
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900464static int fsi_data_push(struct fsi_priv *fsi, int startup)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900465{
466 struct snd_pcm_runtime *runtime;
467 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900468 u32 status;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900469 int send;
470 int fifo_free;
471 int width;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900472 u8 *start;
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900473 int i, over_period;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900474
475 if (!fsi ||
476 !fsi->substream ||
477 !fsi->substream->runtime)
478 return -EINVAL;
479
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900480 over_period = 0;
481 substream = fsi->substream;
482 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900483
484 /* FSI FIFO has limit.
485 * So, this driver can not send periods data at a time
486 */
487 if (fsi->byte_offset >=
488 fsi->period_len * (fsi->periods + 1)) {
489
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900490 over_period = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900491 fsi->periods = (fsi->periods + 1) % runtime->periods;
492
493 if (0 == fsi->periods)
494 fsi->byte_offset = 0;
495 }
496
497 /* get 1 channel data width */
498 width = frames_to_bytes(runtime, 1) / fsi->chan;
499
500 /* get send size for alsa */
501 send = (fsi->buffer_len - fsi->byte_offset) / width;
502
503 /* get FIFO free size */
504 fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
505
506 /* size check */
507 if (fifo_free < send)
508 send = fifo_free;
509
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900510 start = runtime->dma_area;
511 start += fsi->byte_offset;
512
513 switch (width) {
514 case 2:
515 for (i = 0; i < send; i++)
516 fsi_reg_write(fsi, DODT,
517 ((u32)*((u16 *)start + i) << 8));
518 break;
519 case 4:
520 for (i = 0; i < send; i++)
521 fsi_reg_write(fsi, DODT, *((u32 *)start + i));
522 break;
523 default:
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900524 return -EINVAL;
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900525 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900526
527 fsi->byte_offset += send * width;
528
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900529 status = fsi_reg_read(fsi, DOFF_ST);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900530 if (!startup) {
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900531 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900532
533 if (status & ERR_OVER)
534 dev_err(dai->dev, "over run\n");
535 if (status & ERR_UNDER)
536 dev_err(dai->dev, "under run\n");
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900537 }
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900538 fsi_reg_write(fsi, DOFF_ST, 0);
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900539
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900540 fsi_irq_enable(fsi, 1);
541
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900542 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900543 snd_pcm_period_elapsed(substream);
544
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900545 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900546}
547
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900548static int fsi_data_pop(struct fsi_priv *fsi, int startup)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900549{
550 struct snd_pcm_runtime *runtime;
551 struct snd_pcm_substream *substream = NULL;
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900552 u32 status;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900553 int free;
554 int fifo_fill;
555 int width;
556 u8 *start;
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900557 int i, over_period;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900558
559 if (!fsi ||
560 !fsi->substream ||
561 !fsi->substream->runtime)
562 return -EINVAL;
563
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900564 over_period = 0;
565 substream = fsi->substream;
566 runtime = substream->runtime;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900567
568 /* FSI FIFO has limit.
569 * So, this driver can not send periods data at a time
570 */
571 if (fsi->byte_offset >=
572 fsi->period_len * (fsi->periods + 1)) {
573
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900574 over_period = 1;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900575 fsi->periods = (fsi->periods + 1) % runtime->periods;
576
577 if (0 == fsi->periods)
578 fsi->byte_offset = 0;
579 }
580
581 /* get 1 channel data width */
582 width = frames_to_bytes(runtime, 1) / fsi->chan;
583
584 /* get free space for alsa */
585 free = (fsi->buffer_len - fsi->byte_offset) / width;
586
587 /* get recv size */
588 fifo_fill = fsi_get_fifo_residue(fsi, 0);
589
590 if (free < fifo_fill)
591 fifo_fill = free;
592
593 start = runtime->dma_area;
594 start += fsi->byte_offset;
595
596 switch (width) {
597 case 2:
598 for (i = 0; i < fifo_fill; i++)
599 *((u16 *)start + i) =
600 (u16)(fsi_reg_read(fsi, DIDT) >> 8);
601 break;
602 case 4:
603 for (i = 0; i < fifo_fill; i++)
604 *((u32 *)start + i) = fsi_reg_read(fsi, DIDT);
605 break;
606 default:
607 return -EINVAL;
608 }
609
610 fsi->byte_offset += fifo_fill * width;
611
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900612 status = fsi_reg_read(fsi, DIFF_ST);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900613 if (!startup) {
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900614 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900615
616 if (status & ERR_OVER)
617 dev_err(dai->dev, "over run\n");
618 if (status & ERR_UNDER)
619 dev_err(dai->dev, "under run\n");
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900620 }
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900621 fsi_reg_write(fsi, DIFF_ST, 0);
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900622
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900623 fsi_irq_enable(fsi, 0);
624
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900625 if (over_period)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900626 snd_pcm_period_elapsed(substream);
627
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900628 return 0;
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900629}
630
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900631static irqreturn_t fsi_interrupt(int irq, void *data)
632{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900633 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900634 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900635
636 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900637 fsi_master_mask_set(master, SOFT_RST, IR, 0);
638 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900639
640 if (int_st & INT_A_OUT)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900641 fsi_data_push(&master->fsia, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900642 if (int_st & INT_B_OUT)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900643 fsi_data_push(&master->fsib, 0);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900644 if (int_st & INT_A_IN)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900645 fsi_data_pop(&master->fsia, 0);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900646 if (int_st & INT_B_IN)
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900647 fsi_data_pop(&master->fsib, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900648
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900649 fsi_irq_clear_all_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900650
651 return IRQ_HANDLED;
652}
653
654/************************************************************************
655
656
657 dai ops
658
659
660************************************************************************/
661static int fsi_dai_startup(struct snd_pcm_substream *substream,
662 struct snd_soc_dai *dai)
663{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900664 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900665 const char *msg;
666 u32 flags = fsi_get_info_flags(fsi);
667 u32 fmt;
668 u32 reg;
669 u32 data;
670 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
671 int is_master;
672 int ret = 0;
673
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900674 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900675
676 /* CKG1 */
677 data = is_play ? (1 << 0) : (1 << 4);
678 is_master = fsi_is_master_mode(fsi, is_play);
679 if (is_master)
680 fsi_reg_mask_set(fsi, CKG1, data, data);
681 else
682 fsi_reg_mask_set(fsi, CKG1, data, 0);
683
684 /* clock inversion (CKG2) */
685 data = 0;
686 switch (SH_FSI_INVERSION_MASK & flags) {
687 case SH_FSI_LRM_INV:
688 data = 1 << 12;
689 break;
690 case SH_FSI_BRM_INV:
691 data = 1 << 8;
692 break;
693 case SH_FSI_LRS_INV:
694 data = 1 << 4;
695 break;
696 case SH_FSI_BRS_INV:
697 data = 1 << 0;
698 break;
699 }
700 fsi_reg_write(fsi, CKG2, data);
701
702 /* do fmt, di fmt */
703 data = 0;
704 reg = is_play ? DO_FMT : DI_FMT;
705 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
706 switch (fmt) {
707 case SH_FSI_FMT_MONO:
708 msg = "MONO";
709 data = CR_FMT(CR_MONO);
710 fsi->chan = 1;
711 break;
712 case SH_FSI_FMT_MONO_DELAY:
713 msg = "MONO Delay";
714 data = CR_FMT(CR_MONO_D);
715 fsi->chan = 1;
716 break;
717 case SH_FSI_FMT_PCM:
718 msg = "PCM";
719 data = CR_FMT(CR_PCM);
720 fsi->chan = 2;
721 break;
722 case SH_FSI_FMT_I2S:
723 msg = "I2S";
724 data = CR_FMT(CR_I2S);
725 fsi->chan = 2;
726 break;
727 case SH_FSI_FMT_TDM:
728 msg = "TDM";
729 data = CR_FMT(CR_TDM) | (fsi->chan - 1);
730 fsi->chan = is_play ?
731 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
732 break;
733 case SH_FSI_FMT_TDM_DELAY:
734 msg = "TDM Delay";
735 data = CR_FMT(CR_TDM_D) | (fsi->chan - 1);
736 fsi->chan = is_play ?
737 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
738 break;
739 default:
740 dev_err(dai->dev, "unknown format.\n");
741 return -EINVAL;
742 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900743 fsi_reg_write(fsi, reg, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900744
745 /*
746 * clear clk reset if master mode
747 */
748 if (is_master)
749 fsi_clk_ctrl(fsi, 1);
750
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900751 /* irq clear */
752 fsi_irq_disable(fsi, is_play);
753 fsi_irq_clear_status(fsi);
754
755 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900756 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900757
758 return ret;
759}
760
761static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
762 struct snd_soc_dai *dai)
763{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900764 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900765 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
766
767 fsi_irq_disable(fsi, is_play);
768 fsi_clk_ctrl(fsi, 0);
769
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900770 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900771}
772
773static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
774 struct snd_soc_dai *dai)
775{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900776 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900777 struct snd_pcm_runtime *runtime = substream->runtime;
778 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
779 int ret = 0;
780
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900781 switch (cmd) {
782 case SNDRV_PCM_TRIGGER_START:
783 fsi_stream_push(fsi, substream,
784 frames_to_bytes(runtime, runtime->buffer_size),
785 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900786 ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900787 break;
788 case SNDRV_PCM_TRIGGER_STOP:
789 fsi_irq_disable(fsi, is_play);
790 fsi_stream_pop(fsi);
791 break;
792 }
793
794 return ret;
795}
796
797static struct snd_soc_dai_ops fsi_dai_ops = {
798 .startup = fsi_dai_startup,
799 .shutdown = fsi_dai_shutdown,
800 .trigger = fsi_dai_trigger,
801};
802
803/************************************************************************
804
805
806 pcm ops
807
808
809************************************************************************/
810static struct snd_pcm_hardware fsi_pcm_hardware = {
811 .info = SNDRV_PCM_INFO_INTERLEAVED |
812 SNDRV_PCM_INFO_MMAP |
813 SNDRV_PCM_INFO_MMAP_VALID |
814 SNDRV_PCM_INFO_PAUSE,
815 .formats = FSI_FMTS,
816 .rates = FSI_RATES,
817 .rate_min = 8000,
818 .rate_max = 192000,
819 .channels_min = 1,
820 .channels_max = 2,
821 .buffer_bytes_max = 64 * 1024,
822 .period_bytes_min = 32,
823 .period_bytes_max = 8192,
824 .periods_min = 1,
825 .periods_max = 32,
826 .fifo_size = 256,
827};
828
829static int fsi_pcm_open(struct snd_pcm_substream *substream)
830{
831 struct snd_pcm_runtime *runtime = substream->runtime;
832 int ret = 0;
833
834 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
835
836 ret = snd_pcm_hw_constraint_integer(runtime,
837 SNDRV_PCM_HW_PARAM_PERIODS);
838
839 return ret;
840}
841
842static int fsi_hw_params(struct snd_pcm_substream *substream,
843 struct snd_pcm_hw_params *hw_params)
844{
845 return snd_pcm_lib_malloc_pages(substream,
846 params_buffer_bytes(hw_params));
847}
848
849static int fsi_hw_free(struct snd_pcm_substream *substream)
850{
851 return snd_pcm_lib_free_pages(substream);
852}
853
854static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
855{
856 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900857 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900858 long location;
859
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900860 location = (fsi->byte_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900861 if (location < 0)
862 location = 0;
863
864 return bytes_to_frames(runtime, location);
865}
866
867static struct snd_pcm_ops fsi_pcm_ops = {
868 .open = fsi_pcm_open,
869 .ioctl = snd_pcm_lib_ioctl,
870 .hw_params = fsi_hw_params,
871 .hw_free = fsi_hw_free,
872 .pointer = fsi_pointer,
873};
874
875/************************************************************************
876
877
878 snd_soc_platform
879
880
881************************************************************************/
882#define PREALLOC_BUFFER (32 * 1024)
883#define PREALLOC_BUFFER_MAX (32 * 1024)
884
885static void fsi_pcm_free(struct snd_pcm *pcm)
886{
887 snd_pcm_lib_preallocate_free_for_all(pcm);
888}
889
890static int fsi_pcm_new(struct snd_card *card,
891 struct snd_soc_dai *dai,
892 struct snd_pcm *pcm)
893{
894 /*
895 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
896 * in MMAP mode (i.e. aplay -M)
897 */
898 return snd_pcm_lib_preallocate_pages_for_all(
899 pcm,
900 SNDRV_DMA_TYPE_CONTINUOUS,
901 snd_dma_continuous_data(GFP_KERNEL),
902 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
903}
904
905/************************************************************************
906
907
908 alsa struct
909
910
911************************************************************************/
912struct snd_soc_dai fsi_soc_dai[] = {
913 {
914 .name = "FSIA",
915 .id = 0,
916 .playback = {
917 .rates = FSI_RATES,
918 .formats = FSI_FMTS,
919 .channels_min = 1,
920 .channels_max = 8,
921 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900922 .capture = {
923 .rates = FSI_RATES,
924 .formats = FSI_FMTS,
925 .channels_min = 1,
926 .channels_max = 8,
927 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900928 .ops = &fsi_dai_ops,
929 },
930 {
931 .name = "FSIB",
932 .id = 1,
933 .playback = {
934 .rates = FSI_RATES,
935 .formats = FSI_FMTS,
936 .channels_min = 1,
937 .channels_max = 8,
938 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900939 .capture = {
940 .rates = FSI_RATES,
941 .formats = FSI_FMTS,
942 .channels_min = 1,
943 .channels_max = 8,
944 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900945 .ops = &fsi_dai_ops,
946 },
947};
948EXPORT_SYMBOL_GPL(fsi_soc_dai);
949
950struct snd_soc_platform fsi_soc_platform = {
951 .name = "fsi-pcm",
952 .pcm_ops = &fsi_pcm_ops,
953 .pcm_new = fsi_pcm_new,
954 .pcm_free = fsi_pcm_free,
955};
956EXPORT_SYMBOL_GPL(fsi_soc_platform);
957
958/************************************************************************
959
960
961 platform function
962
963
964************************************************************************/
965static int fsi_probe(struct platform_device *pdev)
966{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900967 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900968 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900969 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900970 unsigned int irq;
971 int ret;
972
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900973 if (0 != pdev->id) {
974 dev_err(&pdev->dev, "current fsi support id 0 only now\n");
975 return -ENODEV;
976 }
977
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900978 id_entry = pdev->id_entry;
979 if (!id_entry) {
980 dev_err(&pdev->dev, "unknown fsi device\n");
981 return -ENODEV;
982 }
983
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900984 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
985 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +0100986 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900987 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
988 ret = -ENODEV;
989 goto exit;
990 }
991
992 master = kzalloc(sizeof(*master), GFP_KERNEL);
993 if (!master) {
994 dev_err(&pdev->dev, "Could not allocate master\n");
995 ret = -ENOMEM;
996 goto exit;
997 }
998
999 master->base = ioremap_nocache(res->start, resource_size(res));
1000 if (!master->base) {
1001 ret = -ENXIO;
1002 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1003 goto exit_kfree;
1004 }
1005
1006 master->irq = irq;
1007 master->info = pdev->dev.platform_data;
1008 master->fsia.base = master->base;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001009 master->fsia.master = master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001010 master->fsib.base = master->base + 0x40;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001011 master->fsib.master = master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001012 master->regs = (struct fsi_regs *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001013 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001014
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001015 pm_runtime_enable(&pdev->dev);
1016 pm_runtime_resume(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001017
1018 fsi_soc_dai[0].dev = &pdev->dev;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001019 fsi_soc_dai[0].private_data = &master->fsia;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001020 fsi_soc_dai[1].dev = &pdev->dev;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001021 fsi_soc_dai[1].private_data = &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001022
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001023 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001024
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001025 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1026 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001027 if (ret) {
1028 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001029 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001030 }
1031
1032 ret = snd_soc_register_platform(&fsi_soc_platform);
1033 if (ret < 0) {
1034 dev_err(&pdev->dev, "cannot snd soc register\n");
1035 goto exit_free_irq;
1036 }
1037
1038 return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
1039
1040exit_free_irq:
1041 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001042exit_iounmap:
1043 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001044 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001045exit_kfree:
1046 kfree(master);
1047 master = NULL;
1048exit:
1049 return ret;
1050}
1051
1052static int fsi_remove(struct platform_device *pdev)
1053{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001054 struct fsi_master *master;
1055
1056 master = fsi_get_master(fsi_soc_dai[0].private_data);
1057
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001058 snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
1059 snd_soc_unregister_platform(&fsi_soc_platform);
1060
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001061 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001062
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001063 free_irq(master->irq, master);
1064
1065 iounmap(master->base);
1066 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001067
1068 fsi_soc_dai[0].dev = NULL;
1069 fsi_soc_dai[0].private_data = NULL;
1070 fsi_soc_dai[1].dev = NULL;
1071 fsi_soc_dai[1].private_data = NULL;
1072
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001073 return 0;
1074}
1075
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001076static int fsi_runtime_nop(struct device *dev)
1077{
1078 /* Runtime PM callback shared between ->runtime_suspend()
1079 * and ->runtime_resume(). Simply returns success.
1080 *
1081 * This driver re-initializes all registers after
1082 * pm_runtime_get_sync() anyway so there is no need
1083 * to save and restore registers here.
1084 */
1085 return 0;
1086}
1087
1088static struct dev_pm_ops fsi_pm_ops = {
1089 .runtime_suspend = fsi_runtime_nop,
1090 .runtime_resume = fsi_runtime_nop,
1091};
1092
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001093static struct fsi_regs fsi_regs = {
1094 .int_st = INT_ST,
1095 .iemsk = IEMSK,
1096 .imsk = IMSK,
1097};
1098
1099static struct fsi_regs fsi2_regs = {
1100 .int_st = CPU_INT_ST,
1101 .iemsk = CPU_IEMSK,
1102 .imsk = CPU_IMSK,
1103};
1104
1105static struct platform_device_id fsi_id_table[] = {
1106 { "sh_fsi", (kernel_ulong_t)&fsi_regs },
1107 { "sh_fsi2", (kernel_ulong_t)&fsi2_regs },
1108};
1109
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001110static struct platform_driver fsi_driver = {
1111 .driver = {
1112 .name = "sh_fsi",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001113 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001114 },
1115 .probe = fsi_probe,
1116 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001117 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001118};
1119
1120static int __init fsi_mobile_init(void)
1121{
1122 return platform_driver_register(&fsi_driver);
1123}
1124
1125static void __exit fsi_mobile_exit(void)
1126{
1127 platform_driver_unregister(&fsi_driver);
1128}
1129module_init(fsi_mobile_init);
1130module_exit(fsi_mobile_exit);
1131
1132MODULE_LICENSE("GPL");
1133MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1134MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");