blob: 685bb7c4f039246034236ae47ea4b7cbb7ab996b [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
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090015#include <linux/delay.h>
Kuninori Morimoto785d1c42009-11-30 20:24:48 +090016#include <linux/pm_runtime.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090017#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090019#include <sound/soc.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090020#include <sound/sh_fsi.h>
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090021
22#define DO_FMT 0x0000
23#define DOFF_CTL 0x0004
24#define DOFF_ST 0x0008
25#define DI_FMT 0x000C
26#define DIFF_CTL 0x0010
27#define DIFF_ST 0x0014
28#define CKG1 0x0018
29#define CKG2 0x001C
30#define DIDT 0x0020
31#define DODT 0x0024
32#define MUTE_ST 0x0028
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090033#define OUT_SEL 0x0030
34#define REG_END OUT_SEL
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090035
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090036#define A_MST_CTLR 0x0180
37#define B_MST_CTLR 0x01A0
Kuninori Morimotocc780d32010-03-25 19:15:53 +090038#define CPU_INT_ST 0x01F4
39#define CPU_IEMSK 0x01F8
40#define CPU_IMSK 0x01FC
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090041#define INT_ST 0x0200
42#define IEMSK 0x0204
43#define IMSK 0x0208
44#define MUTE 0x020C
45#define CLK_RST 0x0210
46#define SOFT_RST 0x0214
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090047#define FIFO_SZ 0x0218
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090048#define MREG_START A_MST_CTLR
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090049#define MREG_END FIFO_SZ
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090050
51/* DO_FMT */
52/* DI_FMT */
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +090053#define CR_MONO (0x0 << 4)
54#define CR_MONO_D (0x1 << 4)
55#define CR_PCM (0x2 << 4)
56#define CR_I2S (0x3 << 4)
57#define CR_TDM (0x4 << 4)
58#define CR_TDM_D (0x5 << 4)
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090059#define CR_SPDIF 0x00100120
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090060
61/* DOFF_CTL */
62/* DIFF_CTL */
63#define IRQ_HALF 0x00100000
64#define FIFO_CLR 0x00000001
65
66/* DOFF_ST */
67#define ERR_OVER 0x00000010
68#define ERR_UNDER 0x00000001
Kuninori Morimoto59c3b002009-12-28 14:09:16 +090069#define ST_ERR (ERR_OVER | ERR_UNDER)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090070
Kuninori Morimotoccad7b42010-07-13 12:13:14 +090071/* CKG1 */
72#define ACKMD_MASK 0x00007000
73#define BPFMD_MASK 0x00000700
74
Kuninori Morimoto3bc28072010-07-29 16:48:32 +090075/* A/B MST_CTLR */
76#define BP (1 << 4) /* Fix the signal of Biphase output */
77#define SE (1 << 0) /* Fix the master clock */
78
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090079/* CLK_RST */
80#define B_CLK 0x00000010
81#define A_CLK 0x00000001
82
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090083/* IO SHIFT / MACRO */
84#define BI_SHIFT 12
85#define BO_SHIFT 8
86#define AI_SHIFT 4
87#define AO_SHIFT 0
88#define AB_IO(param, shift) (param << shift)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +090089
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +090090/* SOFT_RST */
91#define PBSR (1 << 12) /* Port B Software Reset */
92#define PASR (1 << 8) /* Port A Software Reset */
93#define IR (1 << 4) /* Interrupt Reset */
94#define FSISR (1 << 0) /* Software Reset */
95
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090096/* FIFO_SZ */
Kuninori Morimotocf6edd02010-10-12 11:40:53 +090097#define FIFO_SZ_MASK 0x7
Kuninori Morimoto4a942b42010-03-25 19:15:51 +090098
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
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900103/*
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900104 * FSI driver use below type name for variable
105 *
106 * xxx_len : data length
107 * xxx_width : data width
108 * xxx_offset : data offset
109 * xxx_num : number of data
110 */
111
112/*
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900113 * struct
114 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900115
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900116struct fsi_priv {
117 void __iomem *base;
118 struct snd_pcm_substream *substream;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900119 struct fsi_master *master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900120
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900121 int fifo_max_num;
122 int chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900123
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900124 int buff_offset;
125 int buff_len;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900126 int period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900127 int period_num;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900128
129 u32 mst_ctrl;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900130};
131
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900132struct fsi_core {
133 int ver;
134
Kuninori Morimotocc780d32010-03-25 19:15:53 +0900135 u32 int_st;
136 u32 iemsk;
137 u32 imsk;
138};
139
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900140struct fsi_master {
141 void __iomem *base;
142 int irq;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900143 struct fsi_priv fsia;
144 struct fsi_priv fsib;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900145 struct fsi_core *core;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900146 struct sh_fsi_platform_info *info;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900147 spinlock_t lock;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900148};
149
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900150/*
151 * basic read write function
152 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900153
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100154static void __fsi_reg_write(u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900155{
156 /* valid data area is 24bit */
157 data &= 0x00ffffff;
158
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100159 __raw_writel(data, reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900160}
161
162static u32 __fsi_reg_read(u32 reg)
163{
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100164 return __raw_readl(reg);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900165}
166
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100167static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900168{
169 u32 val = __fsi_reg_read(reg);
170
171 val &= ~mask;
172 val |= data & mask;
173
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100174 __fsi_reg_write(reg, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900175}
176
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100177static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900178{
Kuninori Morimotod7854142010-07-13 12:13:09 +0900179 if (reg > REG_END) {
180 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100181 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900182 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900183
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100184 __fsi_reg_write((u32)(fsi->base + reg), data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900185}
186
187static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
188{
Kuninori Morimotod7854142010-07-13 12:13:09 +0900189 if (reg > REG_END) {
190 pr_err("fsi: register access err (%s)\n", __func__);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900191 return 0;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900192 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900193
194 return __fsi_reg_read((u32)(fsi->base + reg));
195}
196
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100197static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900198{
Kuninori Morimotod7854142010-07-13 12:13:09 +0900199 if (reg > REG_END) {
200 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100201 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900202 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900203
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100204 __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900205}
206
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100207static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900208{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900209 unsigned long flags;
210
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900211 if ((reg < MREG_START) ||
Kuninori Morimotod7854142010-07-13 12:13:09 +0900212 (reg > MREG_END)) {
213 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100214 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900215 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900216
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900217 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100218 __fsi_reg_write((u32)(master->base + reg), data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900219 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900220}
221
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900222static u32 fsi_master_read(struct fsi_master *master, u32 reg)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900223{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900224 u32 ret;
225 unsigned long flags;
226
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900227 if ((reg < MREG_START) ||
Kuninori Morimotod7854142010-07-13 12:13:09 +0900228 (reg > MREG_END)) {
229 pr_err("fsi: register access err (%s)\n", __func__);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900230 return 0;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900231 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900232
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900233 spin_lock_irqsave(&master->lock, flags);
234 ret = __fsi_reg_read((u32)(master->base + reg));
235 spin_unlock_irqrestore(&master->lock, flags);
236
237 return ret;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900238}
239
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100240static void fsi_master_mask_set(struct fsi_master *master,
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900241 u32 reg, u32 mask, u32 data)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900242{
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900243 unsigned long flags;
244
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900245 if ((reg < MREG_START) ||
Kuninori Morimotod7854142010-07-13 12:13:09 +0900246 (reg > MREG_END)) {
247 pr_err("fsi: register access err (%s)\n", __func__);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100248 return;
Kuninori Morimotod7854142010-07-13 12:13:09 +0900249 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900250
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900251 spin_lock_irqsave(&master->lock, flags);
Guennadi Liakhovetski0f69d972010-02-03 17:37:23 +0100252 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +0900253 spin_unlock_irqrestore(&master->lock, flags);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900254}
255
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900256/*
257 * basic function
258 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900259
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900260static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900261{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900262 return fsi->master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900263}
264
265static int fsi_is_port_a(struct fsi_priv *fsi)
266{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900267 return fsi->master->base == fsi->base;
268}
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900269
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900270static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900271{
272 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900273
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000274 return rtd->cpu_dai;
Kuninori Morimoto142e8172009-12-28 14:09:11 +0900275}
276
277static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
278{
279 struct snd_soc_dai *dai = fsi_get_dai(substream);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900281
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000282 if (dai->id == 0)
283 return &master->fsia;
284 else
285 return &master->fsib;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900286}
287
288static u32 fsi_get_info_flags(struct fsi_priv *fsi)
289{
290 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900291 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900292
293 return is_porta ? master->info->porta_flags :
294 master->info->portb_flags;
295}
296
297static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
298{
299 u32 mode;
300 u32 flags = fsi_get_info_flags(fsi);
301
302 mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
303
304 /* return
305 * 1 : master mode
306 * 0 : slave mode
307 */
308
309 return (mode & flags) != mode;
310}
311
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900312static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900313{
314 int is_porta = fsi_is_port_a(fsi);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900315 u32 shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900316
317 if (is_porta)
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900318 shift = is_play ? AO_SHIFT : AI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900319 else
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900320 shift = is_play ? BO_SHIFT : BI_SHIFT;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900321
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900322 return shift;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900323}
324
325static void fsi_stream_push(struct fsi_priv *fsi,
326 struct snd_pcm_substream *substream,
327 u32 buffer_len,
328 u32 period_len)
329{
330 fsi->substream = substream;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900331 fsi->buff_len = buffer_len;
332 fsi->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900333 fsi->period_len = period_len;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900334 fsi->period_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900335}
336
337static void fsi_stream_pop(struct fsi_priv *fsi)
338{
339 fsi->substream = NULL;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900340 fsi->buff_len = 0;
341 fsi->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900342 fsi->period_len = 0;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900343 fsi->period_num = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900344}
345
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900346static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900347{
348 u32 status;
349 u32 reg = is_play ? DOFF_ST : DIFF_ST;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900350 int data_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900351
352 status = fsi_reg_read(fsi, reg);
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900353 data_num = 0x1ff & (status >> 8);
354 data_num *= fsi->chan_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900355
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900356 return data_num;
357}
358
359static int fsi_len2num(int len, int width)
360{
361 return len / width;
362}
363
364#define fsi_num2offset(a, b) fsi_num2len(a, b)
365static int fsi_num2len(int num, int width)
366{
367 return num * width;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900368}
369
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900370static int fsi_get_frame_width(struct fsi_priv *fsi)
371{
372 struct snd_pcm_substream *substream = fsi->substream;
373 struct snd_pcm_runtime *runtime = substream->runtime;
374
375 return frames_to_bytes(runtime, 1) / fsi->chan_num;
376}
377
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900378/*
379 * dma function
380 */
381
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900382static u8 *fsi_dma_get_area(struct fsi_priv *fsi)
383{
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900384 return fsi->substream->runtime->dma_area + fsi->buff_offset;
Kuninori Morimotoc79eab32010-09-17 13:48:05 +0900385}
386
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900387static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900388{
389 u16 *start;
390 int i;
391
392 start = (u16 *)fsi_dma_get_area(fsi);
393
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900394 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900395 fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
396}
397
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900398static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900399{
400 u16 *start;
401 int i;
402
403 start = (u16 *)fsi_dma_get_area(fsi);
404
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900405 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900406 *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
407}
408
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900409static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900410{
411 u32 *start;
412 int i;
413
414 start = (u32 *)fsi_dma_get_area(fsi);
415
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900416 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900417 fsi_reg_write(fsi, DODT, *(start + i));
418}
419
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900420static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900421{
422 u32 *start;
423 int i;
424
425 start = (u32 *)fsi_dma_get_area(fsi);
426
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900427 for (i = 0; i < num; i++)
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900428 *(start + i) = fsi_reg_read(fsi, DIDT);
429}
430
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900431/*
432 * irq function
433 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900434
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900435static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
436{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900437 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900438 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900439
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900440 fsi_master_mask_set(master, master->core->imsk, data, data);
441 fsi_master_mask_set(master, master->core->iemsk, data, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900442}
443
444static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
445{
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900446 u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900447 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900448
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900449 fsi_master_mask_set(master, master->core->imsk, data, 0);
450 fsi_master_mask_set(master, master->core->iemsk, data, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900451}
452
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900453static u32 fsi_irq_get_status(struct fsi_master *master)
454{
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900455 return fsi_master_read(master, master->core->int_st);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900456}
457
458static void fsi_irq_clear_all_status(struct fsi_master *master)
459{
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900460 fsi_master_write(master, master->core->int_st, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900461}
462
463static void fsi_irq_clear_status(struct fsi_priv *fsi)
464{
465 u32 data = 0;
466 struct fsi_master *master = fsi_get_master(fsi);
467
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900468 data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
469 data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900470
471 /* clear interrupt factor */
Kuninori Morimoto73b92c12010-07-13 12:13:04 +0900472 fsi_master_mask_set(master, master->core->int_st, data, 0);
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900473}
474
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900475/*
476 * SPDIF master clock function
477 *
478 * These functions are used later FSI2
479 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900480static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
481{
482 struct fsi_master *master = fsi_get_master(fsi);
483 u32 val = BP | SE;
484
485 if (master->core->ver < 2) {
486 pr_err("fsi: register access err (%s)\n", __func__);
487 return;
488 }
489
490 if (enable)
491 fsi_master_mask_set(master, fsi->mst_ctrl, val, val);
492 else
493 fsi_master_mask_set(master, fsi->mst_ctrl, val, 0);
494}
495
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900496/*
497 * ctrl function
498 */
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900499
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900500static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
501{
502 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900503 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900504
505 if (enable)
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900506 fsi_master_mask_set(master, CLK_RST, val, val);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900507 else
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900508 fsi_master_mask_set(master, CLK_RST, val, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900509}
510
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900511static void fsi_fifo_init(struct fsi_priv *fsi,
512 int is_play,
513 struct snd_soc_dai *dai)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900514{
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900515 struct fsi_master *master = fsi_get_master(fsi);
516 u32 ctrl, shift, i;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900517
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900518 /* get on-chip RAM capacity */
519 shift = fsi_master_read(master, FIFO_SZ);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900520 shift >>= fsi_get_port_shift(fsi, is_play);
521 shift &= FIFO_SZ_MASK;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900522 fsi->fifo_max_num = 256 << shift;
523 dev_dbg(dai->dev, "fifo = %d words\n", fsi->fifo_max_num);
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900524
525 /*
526 * The maximum number of sample data varies depending
527 * on the number of channels selected for the format.
528 *
529 * FIFOs are used in 4-channel units in 3-channel mode
530 * and in 8-channel units in 5- to 7-channel mode
531 * meaning that more FIFOs than the required size of DPRAM
532 * are used.
533 *
534 * ex) if 256 words of DP-RAM is connected
535 * 1 channel: 256 (256 x 1 = 256)
536 * 2 channels: 128 (128 x 2 = 256)
537 * 3 channels: 64 ( 64 x 3 = 192)
538 * 4 channels: 64 ( 64 x 4 = 256)
539 * 5 channels: 32 ( 32 x 5 = 160)
540 * 6 channels: 32 ( 32 x 6 = 192)
541 * 7 channels: 32 ( 32 x 7 = 224)
542 * 8 channels: 32 ( 32 x 8 = 256)
543 */
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900544 for (i = 1; i < fsi->chan_num; i <<= 1)
545 fsi->fifo_max_num >>= 1;
546 dev_dbg(dai->dev, "%d channel %d store\n",
547 fsi->chan_num, fsi->fifo_max_num);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900548
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900549 ctrl = is_play ? DOFF_CTL : DIFF_CTL;
550
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900551 /* set interrupt generation factor */
552 fsi_reg_write(fsi, ctrl, IRQ_HALF);
553
554 /* clear FIFO */
555 fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900556}
557
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900558static void fsi_soft_all_reset(struct fsi_master *master)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900559{
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900560 /* port AB reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900561 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900562 mdelay(10);
563
564 /* soft reset */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900565 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
566 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900567 mdelay(10);
568}
569
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900570static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int startup, int is_play)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900571{
572 struct snd_pcm_runtime *runtime;
573 struct snd_pcm_substream *substream = NULL;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900574 u32 status_reg = is_play ? DOFF_ST : DIFF_ST;
575 int data_residue_num;
576 int data_num;
577 int data_num_max;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900578 int ch_width;
Kuninori Morimotob9fde182010-09-17 13:48:32 +0900579 int over_period;
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900580 void (*fn)(struct fsi_priv *fsi, int size);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900581
582 if (!fsi ||
583 !fsi->substream ||
584 !fsi->substream->runtime)
585 return -EINVAL;
586
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900587 over_period = 0;
588 substream = fsi->substream;
589 runtime = substream->runtime;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900590
591 /* FSI FIFO has limit.
592 * So, this driver can not send periods data at a time
593 */
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900594 if (fsi->buff_offset >=
595 fsi_num2offset(fsi->period_num + 1, fsi->period_len)) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900596
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900597 over_period = 1;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900598 fsi->period_num = (fsi->period_num + 1) % runtime->periods;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900599
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900600 if (0 == fsi->period_num)
601 fsi->buff_offset = 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900602 }
603
604 /* get 1 channel data width */
Kuninori Morimotocca1b232010-10-12 11:39:25 +0900605 ch_width = fsi_get_frame_width(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900606
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900607 /* get residue data number of alsa */
608 data_residue_num = fsi_len2num(fsi->buff_len - fsi->buff_offset,
609 ch_width);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900610
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900611 if (is_play) {
612 /*
613 * for play-back
614 *
615 * data_num_max : number of FSI fifo free space
616 * data_num : number of ALSA residue data
617 */
618 data_num_max = fsi->fifo_max_num * fsi->chan_num;
619 data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900620
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900621 data_num = data_residue_num;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900622
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900623 switch (ch_width) {
624 case 2:
625 fn = fsi_dma_soft_push16;
626 break;
627 case 4:
628 fn = fsi_dma_soft_push32;
629 break;
630 default:
631 return -EINVAL;
632 }
633 } else {
634 /*
635 * for capture
636 *
637 * data_num_max : number of ALSA free space
638 * data_num : number of data in FSI fifo
639 */
640 data_num_max = data_residue_num;
641 data_num = fsi_get_fifo_data_num(fsi, is_play);
642
643 switch (ch_width) {
644 case 2:
645 fn = fsi_dma_soft_pop16;
646 break;
647 case 4:
648 fn = fsi_dma_soft_pop32;
649 break;
650 default:
651 return -EINVAL;
652 }
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +0900653 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900654
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900655 data_num = min(data_num, data_num_max);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900656
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900657 fn(fsi, data_num);
658
659 /* update buff_offset */
660 fsi->buff_offset += fsi_num2offset(data_num, ch_width);
661
662 /* check fifo status */
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900663 if (!startup) {
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900664 struct snd_soc_dai *dai = fsi_get_dai(substream);
Kuninori Morimoto75eda9682010-10-12 11:40:14 +0900665 u32 status = fsi_reg_read(fsi, status_reg);
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900666
667 if (status & ERR_OVER)
668 dev_err(dai->dev, "over run\n");
669 if (status & ERR_UNDER)
670 dev_err(dai->dev, "under run\n");
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900671 }
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900672 fsi_reg_write(fsi, status_reg, 0);
Kuninori Morimoto59c3b002009-12-28 14:09:16 +0900673
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900674 /* re-enable irq */
675 fsi_irq_enable(fsi, is_play);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900676
Kuninori Morimoto1c418d12009-12-28 14:09:05 +0900677 if (over_period)
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900678 snd_pcm_period_elapsed(substream);
679
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900680 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900681}
682
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900683static int fsi_data_pop(struct fsi_priv *fsi, int startup)
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900684{
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900685 return fsi_fifo_data_ctrl(fsi, startup, 0);
686}
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900687
Kuninori Morimotod8b33532010-09-17 13:49:05 +0900688static int fsi_data_push(struct fsi_priv *fsi, int startup)
689{
690 return fsi_fifo_data_ctrl(fsi, startup, 1);
Kuninori Morimoto07102f32009-10-30 12:02:44 +0900691}
692
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900693static irqreturn_t fsi_interrupt(int irq, void *data)
694{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900695 struct fsi_master *master = data;
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900696 u32 int_st = fsi_irq_get_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900697
698 /* clear irq status */
Kuninori Morimotofeb58cf2010-03-24 15:27:24 +0900699 fsi_master_mask_set(master, SOFT_RST, IR, 0);
700 fsi_master_mask_set(master, SOFT_RST, IR, IR);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900701
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900702 if (int_st & AB_IO(1, AO_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900703 fsi_data_push(&master->fsia, 0);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900704 if (int_st & AB_IO(1, BO_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900705 fsi_data_push(&master->fsib, 0);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900706 if (int_st & AB_IO(1, AI_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900707 fsi_data_pop(&master->fsia, 0);
Kuninori Morimotocf6edd02010-10-12 11:40:53 +0900708 if (int_st & AB_IO(1, BI_SHIFT))
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900709 fsi_data_pop(&master->fsib, 0);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900710
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900711 fsi_irq_clear_all_status(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900712
713 return IRQ_HANDLED;
714}
715
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900716/*
717 * dai ops
718 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900719
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900720static int fsi_dai_startup(struct snd_pcm_substream *substream,
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 u32 flags = fsi_get_info_flags(fsi);
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900725 struct fsi_master *master = fsi_get_master(fsi);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900726 u32 fmt;
727 u32 reg;
728 u32 data;
729 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
730 int is_master;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900731
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900732 pm_runtime_get_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900733
734 /* CKG1 */
735 data = is_play ? (1 << 0) : (1 << 4);
736 is_master = fsi_is_master_mode(fsi, is_play);
737 if (is_master)
738 fsi_reg_mask_set(fsi, CKG1, data, data);
739 else
740 fsi_reg_mask_set(fsi, CKG1, data, 0);
741
742 /* clock inversion (CKG2) */
743 data = 0;
Kuninori Morimotob427b442010-07-13 12:01:15 +0900744 if (SH_FSI_LRM_INV & flags)
745 data |= 1 << 12;
746 if (SH_FSI_BRM_INV & flags)
747 data |= 1 << 8;
748 if (SH_FSI_LRS_INV & flags)
749 data |= 1 << 4;
750 if (SH_FSI_BRS_INV & flags)
751 data |= 1 << 0;
752
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900753 fsi_reg_write(fsi, CKG2, data);
754
755 /* do fmt, di fmt */
756 data = 0;
757 reg = is_play ? DO_FMT : DI_FMT;
758 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
759 switch (fmt) {
760 case SH_FSI_FMT_MONO:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900761 data = CR_MONO;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900762 fsi->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900763 break;
764 case SH_FSI_FMT_MONO_DELAY:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900765 data = CR_MONO_D;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900766 fsi->chan_num = 1;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900767 break;
768 case SH_FSI_FMT_PCM:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900769 data = CR_PCM;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900770 fsi->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900771 break;
772 case SH_FSI_FMT_I2S:
Kuninori Morimotoa7ffb522010-07-13 12:13:00 +0900773 data = CR_I2S;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900774 fsi->chan_num = 2;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900775 break;
776 case SH_FSI_FMT_TDM:
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900777 fsi->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900778 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900779 data = CR_TDM | (fsi->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900780 break;
781 case SH_FSI_FMT_TDM_DELAY:
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900782 fsi->chan_num = is_play ?
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900783 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900784 data = CR_TDM_D | (fsi->chan_num - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900785 break;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900786 case SH_FSI_FMT_SPDIF:
787 if (master->core->ver < 2) {
788 dev_err(dai->dev, "This FSI can not use SPDIF\n");
789 return -EINVAL;
790 }
791 data = CR_SPDIF;
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900792 fsi->chan_num = 2;
Kuninori Morimoto3bc28072010-07-29 16:48:32 +0900793 fsi_spdif_clk_ctrl(fsi, 1);
794 fsi_reg_mask_set(fsi, OUT_SEL, 0x0010, 0x0010);
795 break;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900796 default:
797 dev_err(dai->dev, "unknown format.\n");
798 return -EINVAL;
799 }
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900800 fsi_reg_write(fsi, reg, data);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900801
Kuninori Morimoto10ea76c2010-03-23 11:47:54 +0900802 /* irq clear */
803 fsi_irq_disable(fsi, is_play);
804 fsi_irq_clear_status(fsi);
805
806 /* fifo init */
Kuninori Morimoto4a942b42010-03-25 19:15:51 +0900807 fsi_fifo_init(fsi, is_play, dai);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900808
Kuninori Morimotoa68a3b42010-10-12 11:39:50 +0900809 return 0;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900810}
811
812static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
813 struct snd_soc_dai *dai)
814{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900815 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900816 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
817
818 fsi_irq_disable(fsi, is_play);
819 fsi_clk_ctrl(fsi, 0);
820
Kuninori Morimoto785d1c42009-11-30 20:24:48 +0900821 pm_runtime_put_sync(dai->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900822}
823
824static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
825 struct snd_soc_dai *dai)
826{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900827 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900828 struct snd_pcm_runtime *runtime = substream->runtime;
829 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
830 int ret = 0;
831
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900832 switch (cmd) {
833 case SNDRV_PCM_TRIGGER_START:
834 fsi_stream_push(fsi, substream,
835 frames_to_bytes(runtime, runtime->buffer_size),
836 frames_to_bytes(runtime, runtime->period_size));
Kuninori Morimoto47fc9a02010-02-22 16:41:57 +0900837 ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900838 break;
839 case SNDRV_PCM_TRIGGER_STOP:
840 fsi_irq_disable(fsi, is_play);
841 fsi_stream_pop(fsi);
842 break;
843 }
844
845 return ret;
846}
847
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900848static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
849 struct snd_pcm_hw_params *params,
850 struct snd_soc_dai *dai)
851{
852 struct fsi_priv *fsi = fsi_get_priv(substream);
853 struct fsi_master *master = fsi_get_master(fsi);
854 int (*set_rate)(int is_porta, int rate) = master->info->set_rate;
855 int fsi_ver = master->core->ver;
856 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
857 int ret;
858
859 /* if slave mode, set_rate is not needed */
860 if (!fsi_is_master_mode(fsi, is_play))
861 return 0;
862
863 /* it is error if no set_rate */
864 if (!set_rate)
865 return -EIO;
866
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900867 ret = set_rate(fsi_is_port_a(fsi), params_rate(params));
868 if (ret > 0) {
869 u32 data = 0;
870
871 switch (ret & SH_FSI_ACKMD_MASK) {
872 default:
873 /* FALL THROUGH */
874 case SH_FSI_ACKMD_512:
875 data |= (0x0 << 12);
876 break;
877 case SH_FSI_ACKMD_256:
878 data |= (0x1 << 12);
879 break;
880 case SH_FSI_ACKMD_128:
881 data |= (0x2 << 12);
882 break;
883 case SH_FSI_ACKMD_64:
884 data |= (0x3 << 12);
885 break;
886 case SH_FSI_ACKMD_32:
887 if (fsi_ver < 2)
888 dev_err(dai->dev, "unsupported ACKMD\n");
889 else
890 data |= (0x4 << 12);
891 break;
892 }
893
894 switch (ret & SH_FSI_BPFMD_MASK) {
895 default:
896 /* FALL THROUGH */
897 case SH_FSI_BPFMD_32:
898 data |= (0x0 << 8);
899 break;
900 case SH_FSI_BPFMD_64:
901 data |= (0x1 << 8);
902 break;
903 case SH_FSI_BPFMD_128:
904 data |= (0x2 << 8);
905 break;
906 case SH_FSI_BPFMD_256:
907 data |= (0x3 << 8);
908 break;
909 case SH_FSI_BPFMD_512:
910 data |= (0x4 << 8);
911 break;
912 case SH_FSI_BPFMD_16:
913 if (fsi_ver < 2)
914 dev_err(dai->dev, "unsupported ACKMD\n");
915 else
916 data |= (0x7 << 8);
917 break;
918 }
919
920 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
921 udelay(10);
922 fsi_clk_ctrl(fsi, 1);
923 ret = 0;
924 }
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900925
926 return ret;
927
928}
929
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900930static struct snd_soc_dai_ops fsi_dai_ops = {
931 .startup = fsi_dai_startup,
932 .shutdown = fsi_dai_shutdown,
933 .trigger = fsi_dai_trigger,
Kuninori Morimotoccad7b42010-07-13 12:13:14 +0900934 .hw_params = fsi_dai_hw_params,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900935};
936
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +0900937/*
938 * pcm ops
939 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900940
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900941static struct snd_pcm_hardware fsi_pcm_hardware = {
942 .info = SNDRV_PCM_INFO_INTERLEAVED |
943 SNDRV_PCM_INFO_MMAP |
944 SNDRV_PCM_INFO_MMAP_VALID |
945 SNDRV_PCM_INFO_PAUSE,
946 .formats = FSI_FMTS,
947 .rates = FSI_RATES,
948 .rate_min = 8000,
949 .rate_max = 192000,
950 .channels_min = 1,
951 .channels_max = 2,
952 .buffer_bytes_max = 64 * 1024,
953 .period_bytes_min = 32,
954 .period_bytes_max = 8192,
955 .periods_min = 1,
956 .periods_max = 32,
957 .fifo_size = 256,
958};
959
960static int fsi_pcm_open(struct snd_pcm_substream *substream)
961{
962 struct snd_pcm_runtime *runtime = substream->runtime;
963 int ret = 0;
964
965 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
966
967 ret = snd_pcm_hw_constraint_integer(runtime,
968 SNDRV_PCM_HW_PARAM_PERIODS);
969
970 return ret;
971}
972
973static int fsi_hw_params(struct snd_pcm_substream *substream,
974 struct snd_pcm_hw_params *hw_params)
975{
976 return snd_pcm_lib_malloc_pages(substream,
977 params_buffer_bytes(hw_params));
978}
979
980static int fsi_hw_free(struct snd_pcm_substream *substream)
981{
982 return snd_pcm_lib_free_pages(substream);
983}
984
985static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
986{
987 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto71f6e062009-12-02 15:11:08 +0900988 struct fsi_priv *fsi = fsi_get_priv(substream);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900989 long location;
990
Kuninori Morimoto5bfb9ad2010-09-17 13:48:45 +0900991 location = (fsi->buff_offset - 1);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +0900992 if (location < 0)
993 location = 0;
994
995 return bytes_to_frames(runtime, location);
996}
997
998static struct snd_pcm_ops fsi_pcm_ops = {
999 .open = fsi_pcm_open,
1000 .ioctl = snd_pcm_lib_ioctl,
1001 .hw_params = fsi_hw_params,
1002 .hw_free = fsi_hw_free,
1003 .pointer = fsi_pointer,
1004};
1005
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001006/*
1007 * snd_soc_platform
1008 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001009
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001010#define PREALLOC_BUFFER (32 * 1024)
1011#define PREALLOC_BUFFER_MAX (32 * 1024)
1012
1013static void fsi_pcm_free(struct snd_pcm *pcm)
1014{
1015 snd_pcm_lib_preallocate_free_for_all(pcm);
1016}
1017
1018static int fsi_pcm_new(struct snd_card *card,
1019 struct snd_soc_dai *dai,
1020 struct snd_pcm *pcm)
1021{
1022 /*
1023 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1024 * in MMAP mode (i.e. aplay -M)
1025 */
1026 return snd_pcm_lib_preallocate_pages_for_all(
1027 pcm,
1028 SNDRV_DMA_TYPE_CONTINUOUS,
1029 snd_dma_continuous_data(GFP_KERNEL),
1030 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1031}
1032
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001033/*
1034 * alsa struct
1035 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001036
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001037static struct snd_soc_dai_driver fsi_soc_dai[] = {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001038 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039 .name = "fsia-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001040 .playback = {
1041 .rates = FSI_RATES,
1042 .formats = FSI_FMTS,
1043 .channels_min = 1,
1044 .channels_max = 8,
1045 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001046 .capture = {
1047 .rates = FSI_RATES,
1048 .formats = FSI_FMTS,
1049 .channels_min = 1,
1050 .channels_max = 8,
1051 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001052 .ops = &fsi_dai_ops,
1053 },
1054 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001055 .name = "fsib-dai",
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001056 .playback = {
1057 .rates = FSI_RATES,
1058 .formats = FSI_FMTS,
1059 .channels_min = 1,
1060 .channels_max = 8,
1061 },
Kuninori Morimoto07102f32009-10-30 12:02:44 +09001062 .capture = {
1063 .rates = FSI_RATES,
1064 .formats = FSI_FMTS,
1065 .channels_min = 1,
1066 .channels_max = 8,
1067 },
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001068 .ops = &fsi_dai_ops,
1069 },
1070};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001071
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001072static struct snd_soc_platform_driver fsi_soc_platform = {
1073 .ops = &fsi_pcm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001074 .pcm_new = fsi_pcm_new,
1075 .pcm_free = fsi_pcm_free,
1076};
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001077
Kuninori Morimotoc8fe2572010-09-17 13:48:17 +09001078/*
1079 * platform function
1080 */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001081
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001082static int fsi_probe(struct platform_device *pdev)
1083{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001084 struct fsi_master *master;
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001085 const struct platform_device_id *id_entry;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001086 struct resource *res;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001087 unsigned int irq;
1088 int ret;
1089
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001090 id_entry = pdev->id_entry;
1091 if (!id_entry) {
1092 dev_err(&pdev->dev, "unknown fsi device\n");
1093 return -ENODEV;
1094 }
1095
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001096 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1097 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königb6aa1792009-12-16 17:10:09 +01001098 if (!res || (int)irq <= 0) {
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001099 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1100 ret = -ENODEV;
1101 goto exit;
1102 }
1103
1104 master = kzalloc(sizeof(*master), GFP_KERNEL);
1105 if (!master) {
1106 dev_err(&pdev->dev, "Could not allocate master\n");
1107 ret = -ENOMEM;
1108 goto exit;
1109 }
1110
1111 master->base = ioremap_nocache(res->start, resource_size(res));
1112 if (!master->base) {
1113 ret = -ENXIO;
1114 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1115 goto exit_kfree;
1116 }
1117
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001118 /* master setting */
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001119 master->irq = irq;
1120 master->info = pdev->dev.platform_data;
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001121 master->core = (struct fsi_core *)id_entry->driver_data;
Kuninori Morimoto8fc176d2010-01-28 13:46:16 +09001122 spin_lock_init(&master->lock);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001123
Kuninori Morimoto3bc28072010-07-29 16:48:32 +09001124 /* FSI A setting */
1125 master->fsia.base = master->base;
1126 master->fsia.master = master;
1127 master->fsia.mst_ctrl = A_MST_CTLR;
1128
1129 /* FSI B setting */
1130 master->fsib.base = master->base + 0x40;
1131 master->fsib.master = master;
1132 master->fsib.mst_ctrl = B_MST_CTLR;
1133
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001134 pm_runtime_enable(&pdev->dev);
1135 pm_runtime_resume(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001136 dev_set_drvdata(&pdev->dev, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001137
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001138 fsi_soft_all_reset(master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001139
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001140 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
1141 id_entry->name, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001142 if (ret) {
1143 dev_err(&pdev->dev, "irq request err\n");
Kuninori Morimoto9ddc9aa2009-10-30 12:02:39 +09001144 goto exit_iounmap;
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001145 }
1146
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001147 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001148 if (ret < 0) {
1149 dev_err(&pdev->dev, "cannot snd soc register\n");
1150 goto exit_free_irq;
1151 }
1152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153 return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001154
1155exit_free_irq:
1156 free_irq(irq, master);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001157exit_iounmap:
1158 iounmap(master->base);
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001159 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001160exit_kfree:
1161 kfree(master);
1162 master = NULL;
1163exit:
1164 return ret;
1165}
1166
1167static int fsi_remove(struct platform_device *pdev)
1168{
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001169 struct fsi_master *master;
1170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001171 master = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001172
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001173 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1174 snd_soc_unregister_platform(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001175
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001176 pm_runtime_disable(&pdev->dev);
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001177
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001178 free_irq(master->irq, master);
1179
1180 iounmap(master->base);
1181 kfree(master);
Kuninori Morimoto71f6e062009-12-02 15:11:08 +09001182
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001183 return 0;
1184}
1185
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001186static int fsi_runtime_nop(struct device *dev)
1187{
1188 /* Runtime PM callback shared between ->runtime_suspend()
1189 * and ->runtime_resume(). Simply returns success.
1190 *
1191 * This driver re-initializes all registers after
1192 * pm_runtime_get_sync() anyway so there is no need
1193 * to save and restore registers here.
1194 */
1195 return 0;
1196}
1197
1198static struct dev_pm_ops fsi_pm_ops = {
1199 .runtime_suspend = fsi_runtime_nop,
1200 .runtime_resume = fsi_runtime_nop,
1201};
1202
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001203static struct fsi_core fsi1_core = {
1204 .ver = 1,
1205
1206 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001207 .int_st = INT_ST,
1208 .iemsk = IEMSK,
1209 .imsk = IMSK,
1210};
1211
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001212static struct fsi_core fsi2_core = {
1213 .ver = 2,
1214
1215 /* Interrupt */
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001216 .int_st = CPU_INT_ST,
1217 .iemsk = CPU_IEMSK,
1218 .imsk = CPU_IMSK,
1219};
1220
1221static struct platform_device_id fsi_id_table[] = {
Kuninori Morimoto73b92c12010-07-13 12:13:04 +09001222 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1223 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
Guennadi Liakhovetski05c69452010-10-05 17:54:28 +02001224 {},
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001225};
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001226MODULE_DEVICE_TABLE(platform, fsi_id_table);
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001227
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001228static struct platform_driver fsi_driver = {
1229 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001230 .name = "fsi-pcm-audio",
Kuninori Morimoto785d1c42009-11-30 20:24:48 +09001231 .pm = &fsi_pm_ops,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001232 },
1233 .probe = fsi_probe,
1234 .remove = fsi_remove,
Kuninori Morimotocc780d32010-03-25 19:15:53 +09001235 .id_table = fsi_id_table,
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001236};
1237
1238static int __init fsi_mobile_init(void)
1239{
1240 return platform_driver_register(&fsi_driver);
1241}
1242
1243static void __exit fsi_mobile_exit(void)
1244{
1245 platform_driver_unregister(&fsi_driver);
1246}
Dzianis Kahanovichd85a6d72010-09-17 16:42:05 +03001247
Kuninori Morimotoa4d7d552009-08-20 21:01:05 +09001248module_init(fsi_mobile_init);
1249module_exit(fsi_mobile_exit);
1250
1251MODULE_LICENSE("GPL");
1252MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1253MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");