]>
Commit | Line | Data |
---|---|---|
a4d7d550 KM |
1 | /* |
2 | * Fifo-attached Serial Interface (FSI) support for SH7724 | |
3 | * | |
4 | * Copyright (C) 2009 Renesas Solutions Corp. | |
5 | * Kuninori Morimoto <[email protected]> | |
6 | * | |
7 | * Based on ssi.c | |
8 | * Copyright (c) 2007 Manuel Lauss <[email protected]> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | */ | |
14 | ||
a4d7d550 | 15 | #include <linux/delay.h> |
785d1c45 | 16 | #include <linux/pm_runtime.h> |
a4d7d550 | 17 | #include <linux/io.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
da155d5b | 19 | #include <linux/module.h> |
a4d7d550 | 20 | #include <sound/soc.h> |
a4d7d550 | 21 | #include <sound/sh_fsi.h> |
a4d7d550 | 22 | |
e8c8b631 KM |
23 | /* PortA/PortB register */ |
24 | #define REG_DO_FMT 0x0000 | |
25 | #define REG_DOFF_CTL 0x0004 | |
26 | #define REG_DOFF_ST 0x0008 | |
27 | #define REG_DI_FMT 0x000C | |
28 | #define REG_DIFF_CTL 0x0010 | |
29 | #define REG_DIFF_ST 0x0014 | |
30 | #define REG_CKG1 0x0018 | |
31 | #define REG_CKG2 0x001C | |
32 | #define REG_DIDT 0x0020 | |
33 | #define REG_DODT 0x0024 | |
34 | #define REG_MUTE_ST 0x0028 | |
65ff03f4 | 35 | #define REG_OUT_DMAC 0x002C |
e8c8b631 | 36 | #define REG_OUT_SEL 0x0030 |
65ff03f4 | 37 | #define REG_IN_DMAC 0x0038 |
cc780d38 | 38 | |
43fa95ca KM |
39 | /* master register */ |
40 | #define MST_CLK_RST 0x0210 | |
41 | #define MST_SOFT_RST 0x0214 | |
42 | #define MST_FIFO_SZ 0x0218 | |
43 | ||
44 | /* core register (depend on FSI version) */ | |
3bc28070 KM |
45 | #define A_MST_CTLR 0x0180 |
46 | #define B_MST_CTLR 0x01A0 | |
cc780d38 KM |
47 | #define CPU_INT_ST 0x01F4 |
48 | #define CPU_IEMSK 0x01F8 | |
49 | #define CPU_IMSK 0x01FC | |
a4d7d550 KM |
50 | #define INT_ST 0x0200 |
51 | #define IEMSK 0x0204 | |
52 | #define IMSK 0x0208 | |
a4d7d550 KM |
53 | |
54 | /* DO_FMT */ | |
55 | /* DI_FMT */ | |
f7d711e3 KM |
56 | #define CR_BWS_24 (0x0 << 20) /* FSI2 */ |
57 | #define CR_BWS_16 (0x1 << 20) /* FSI2 */ | |
58 | #define CR_BWS_20 (0x2 << 20) /* FSI2 */ | |
59 | ||
60 | #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */ | |
61 | #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */ | |
62 | #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */ | |
63 | ||
a7ffb52b KM |
64 | #define CR_MONO (0x0 << 4) |
65 | #define CR_MONO_D (0x1 << 4) | |
66 | #define CR_PCM (0x2 << 4) | |
67 | #define CR_I2S (0x3 << 4) | |
68 | #define CR_TDM (0x4 << 4) | |
69 | #define CR_TDM_D (0x5 << 4) | |
a4d7d550 KM |
70 | |
71 | /* DOFF_CTL */ | |
72 | /* DIFF_CTL */ | |
73 | #define IRQ_HALF 0x00100000 | |
74 | #define FIFO_CLR 0x00000001 | |
75 | ||
76 | /* DOFF_ST */ | |
77 | #define ERR_OVER 0x00000010 | |
78 | #define ERR_UNDER 0x00000001 | |
59c3b003 | 79 | #define ST_ERR (ERR_OVER | ERR_UNDER) |
a4d7d550 | 80 | |
ccad7b44 KM |
81 | /* CKG1 */ |
82 | #define ACKMD_MASK 0x00007000 | |
83 | #define BPFMD_MASK 0x00000700 | |
4d805f7b KM |
84 | #define DIMD (1 << 4) |
85 | #define DOMD (1 << 0) | |
ccad7b44 | 86 | |
3bc28070 KM |
87 | /* A/B MST_CTLR */ |
88 | #define BP (1 << 4) /* Fix the signal of Biphase output */ | |
89 | #define SE (1 << 0) /* Fix the master clock */ | |
90 | ||
a4d7d550 | 91 | /* CLK_RST */ |
1f5e2a31 KM |
92 | #define CRB (1 << 4) |
93 | #define CRA (1 << 0) | |
a4d7d550 | 94 | |
cf6edd00 KM |
95 | /* IO SHIFT / MACRO */ |
96 | #define BI_SHIFT 12 | |
97 | #define BO_SHIFT 8 | |
98 | #define AI_SHIFT 4 | |
99 | #define AO_SHIFT 0 | |
100 | #define AB_IO(param, shift) (param << shift) | |
a4d7d550 | 101 | |
feb58cff KM |
102 | /* SOFT_RST */ |
103 | #define PBSR (1 << 12) /* Port B Software Reset */ | |
104 | #define PASR (1 << 8) /* Port A Software Reset */ | |
105 | #define IR (1 << 4) /* Interrupt Reset */ | |
106 | #define FSISR (1 << 0) /* Software Reset */ | |
107 | ||
f7d711e3 KM |
108 | /* OUT_SEL (FSI2) */ |
109 | #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */ | |
110 | /* 1: Biphase and serial */ | |
111 | ||
4a942b45 | 112 | /* FIFO_SZ */ |
cf6edd00 | 113 | #define FIFO_SZ_MASK 0x7 |
4a942b45 | 114 | |
a4d7d550 KM |
115 | #define FSI_RATES SNDRV_PCM_RATE_8000_96000 |
116 | ||
117 | #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE) | |
118 | ||
d7c5762b KM |
119 | typedef int (*set_rate_func)(struct device *dev, int is_porta, int rate, int enable); |
120 | ||
5bfb9ad0 KM |
121 | /* |
122 | * FSI driver use below type name for variable | |
123 | * | |
5bfb9ad0 | 124 | * xxx_num : number of data |
2e651baf KM |
125 | * xxx_pos : position of data |
126 | * xxx_capa : capacity of data | |
127 | */ | |
128 | ||
129 | /* | |
130 | * period/frame/sample image | |
131 | * | |
132 | * ex) PCM (2ch) | |
133 | * | |
134 | * period pos period pos | |
135 | * [n] [n + 1] | |
136 | * |<-------------------- period--------------------->| | |
137 | * ==|============================================ ... =|== | |
138 | * | | | |
139 | * ||<----- frame ----->|<------ frame ----->| ... | | |
140 | * |+--------------------+--------------------+- ... | | |
141 | * ||[ sample ][ sample ]|[ sample ][ sample ]| ... | | |
142 | * |+--------------------+--------------------+- ... | | |
143 | * ==|============================================ ... =|== | |
144 | */ | |
145 | ||
146 | /* | |
147 | * FSI FIFO image | |
148 | * | |
149 | * | | | |
150 | * | | | |
151 | * | [ sample ] | | |
152 | * | [ sample ] | | |
153 | * | [ sample ] | | |
154 | * | [ sample ] | | |
155 | * --> go to codecs | |
5bfb9ad0 KM |
156 | */ |
157 | ||
c8fe2574 KM |
158 | /* |
159 | * struct | |
160 | */ | |
a4d7d550 | 161 | |
93193c2b | 162 | struct fsi_stream { |
a4d7d550 KM |
163 | struct snd_pcm_substream *substream; |
164 | ||
2e651baf KM |
165 | int fifo_sample_capa; /* sample capacity of FSI FIFO */ |
166 | int buff_sample_capa; /* sample capacity of ALSA buffer */ | |
167 | int buff_sample_pos; /* sample position of ALSA buffer */ | |
168 | int period_samples; /* sample number / 1 period */ | |
169 | int period_pos; /* current period position */ | |
1ec9bc35 KM |
170 | |
171 | int uerr_num; | |
172 | int oerr_num; | |
93193c2b KM |
173 | }; |
174 | ||
175 | struct fsi_priv { | |
176 | void __iomem *base; | |
177 | struct fsi_master *master; | |
178 | ||
179 | struct fsi_stream playback; | |
180 | struct fsi_stream capture; | |
3bc28070 | 181 | |
9478e0b6 KM |
182 | u32 do_fmt; |
183 | u32 di_fmt; | |
184 | ||
6a9ebad8 KM |
185 | int chan_num:16; |
186 | int clk_master:1; | |
9478e0b6 | 187 | int spdif:1; |
6a9ebad8 | 188 | |
d4bc99b9 | 189 | long rate; |
a4d7d550 KM |
190 | }; |
191 | ||
73b92c1f KM |
192 | struct fsi_core { |
193 | int ver; | |
194 | ||
cc780d38 KM |
195 | u32 int_st; |
196 | u32 iemsk; | |
197 | u32 imsk; | |
2b0e7302 KM |
198 | u32 a_mclk; |
199 | u32 b_mclk; | |
cc780d38 KM |
200 | }; |
201 | ||
a4d7d550 KM |
202 | struct fsi_master { |
203 | void __iomem *base; | |
204 | int irq; | |
a4d7d550 KM |
205 | struct fsi_priv fsia; |
206 | struct fsi_priv fsib; | |
73b92c1f | 207 | struct fsi_core *core; |
a4d7d550 | 208 | struct sh_fsi_platform_info *info; |
8fc176d5 | 209 | spinlock_t lock; |
a4d7d550 KM |
210 | }; |
211 | ||
c8fe2574 KM |
212 | /* |
213 | * basic read write function | |
214 | */ | |
a4d7d550 | 215 | |
ca7aceef | 216 | static void __fsi_reg_write(u32 __iomem *reg, u32 data) |
a4d7d550 KM |
217 | { |
218 | /* valid data area is 24bit */ | |
219 | data &= 0x00ffffff; | |
220 | ||
0f69d978 | 221 | __raw_writel(data, reg); |
a4d7d550 KM |
222 | } |
223 | ||
ca7aceef | 224 | static u32 __fsi_reg_read(u32 __iomem *reg) |
a4d7d550 | 225 | { |
0f69d978 | 226 | return __raw_readl(reg); |
a4d7d550 KM |
227 | } |
228 | ||
ca7aceef | 229 | static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data) |
a4d7d550 KM |
230 | { |
231 | u32 val = __fsi_reg_read(reg); | |
232 | ||
233 | val &= ~mask; | |
234 | val |= data & mask; | |
235 | ||
0f69d978 | 236 | __fsi_reg_write(reg, val); |
a4d7d550 KM |
237 | } |
238 | ||
e8c8b631 | 239 | #define fsi_reg_write(p, r, d)\ |
8918b843 | 240 | __fsi_reg_write((p->base + REG_##r), d) |
a4d7d550 | 241 | |
e8c8b631 | 242 | #define fsi_reg_read(p, r)\ |
8918b843 | 243 | __fsi_reg_read((p->base + REG_##r)) |
a4d7d550 | 244 | |
e8c8b631 | 245 | #define fsi_reg_mask_set(p, r, m, d)\ |
8918b843 | 246 | __fsi_reg_mask_set((p->base + REG_##r), m, d) |
a4d7d550 | 247 | |
43fa95ca KM |
248 | #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r) |
249 | #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r) | |
250 | static u32 _fsi_master_read(struct fsi_master *master, u32 reg) | |
a4d7d550 | 251 | { |
8fc176d5 KM |
252 | u32 ret; |
253 | unsigned long flags; | |
254 | ||
8fc176d5 | 255 | spin_lock_irqsave(&master->lock, flags); |
ca7aceef | 256 | ret = __fsi_reg_read(master->base + reg); |
8fc176d5 KM |
257 | spin_unlock_irqrestore(&master->lock, flags); |
258 | ||
259 | return ret; | |
a4d7d550 KM |
260 | } |
261 | ||
43fa95ca KM |
262 | #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d) |
263 | #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d) | |
264 | static void _fsi_master_mask_set(struct fsi_master *master, | |
71f6e064 | 265 | u32 reg, u32 mask, u32 data) |
a4d7d550 | 266 | { |
8fc176d5 KM |
267 | unsigned long flags; |
268 | ||
8fc176d5 | 269 | spin_lock_irqsave(&master->lock, flags); |
ca7aceef | 270 | __fsi_reg_mask_set(master->base + reg, mask, data); |
8fc176d5 | 271 | spin_unlock_irqrestore(&master->lock, flags); |
a4d7d550 KM |
272 | } |
273 | ||
c8fe2574 KM |
274 | /* |
275 | * basic function | |
276 | */ | |
a4d7d550 | 277 | |
71f6e064 | 278 | static struct fsi_master *fsi_get_master(struct fsi_priv *fsi) |
a4d7d550 | 279 | { |
71f6e064 | 280 | return fsi->master; |
a4d7d550 KM |
281 | } |
282 | ||
6a9ebad8 KM |
283 | static int fsi_is_clk_master(struct fsi_priv *fsi) |
284 | { | |
285 | return fsi->clk_master; | |
286 | } | |
287 | ||
a4d7d550 KM |
288 | static int fsi_is_port_a(struct fsi_priv *fsi) |
289 | { | |
71f6e064 KM |
290 | return fsi->master->base == fsi->base; |
291 | } | |
a4d7d550 | 292 | |
9478e0b6 KM |
293 | static int fsi_is_spdif(struct fsi_priv *fsi) |
294 | { | |
295 | return fsi->spdif; | |
296 | } | |
297 | ||
142e8174 | 298 | static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream) |
71f6e064 KM |
299 | { |
300 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
142e8174 | 301 | |
f0fba2ad | 302 | return rtd->cpu_dai; |
142e8174 KM |
303 | } |
304 | ||
0d032c19 | 305 | static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai) |
142e8174 | 306 | { |
f0fba2ad | 307 | struct fsi_master *master = snd_soc_dai_get_drvdata(dai); |
a4d7d550 | 308 | |
f0fba2ad LG |
309 | if (dai->id == 0) |
310 | return &master->fsia; | |
311 | else | |
312 | return &master->fsib; | |
a4d7d550 KM |
313 | } |
314 | ||
0d032c19 KM |
315 | static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream) |
316 | { | |
317 | return fsi_get_priv_frm_dai(fsi_get_dai(substream)); | |
318 | } | |
319 | ||
d7c5762b KM |
320 | static set_rate_func fsi_get_info_set_rate(struct fsi_master *master) |
321 | { | |
322 | if (!master->info) | |
323 | return NULL; | |
324 | ||
325 | return master->info->set_rate; | |
326 | } | |
327 | ||
a4d7d550 KM |
328 | static u32 fsi_get_info_flags(struct fsi_priv *fsi) |
329 | { | |
330 | int is_porta = fsi_is_port_a(fsi); | |
71f6e064 | 331 | struct fsi_master *master = fsi_get_master(fsi); |
a4d7d550 | 332 | |
d7c5762b KM |
333 | if (!master->info) |
334 | return 0; | |
335 | ||
a4d7d550 KM |
336 | return is_porta ? master->info->porta_flags : |
337 | master->info->portb_flags; | |
338 | } | |
339 | ||
93193c2b KM |
340 | static inline int fsi_stream_is_play(int stream) |
341 | { | |
342 | return stream == SNDRV_PCM_STREAM_PLAYBACK; | |
343 | } | |
344 | ||
00545785 KM |
345 | static inline int fsi_is_play(struct snd_pcm_substream *substream) |
346 | { | |
93193c2b KM |
347 | return fsi_stream_is_play(substream->stream); |
348 | } | |
349 | ||
350 | static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi, | |
351 | int is_play) | |
352 | { | |
353 | return is_play ? &fsi->playback : &fsi->capture; | |
00545785 KM |
354 | } |
355 | ||
cf6edd00 | 356 | static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play) |
a4d7d550 KM |
357 | { |
358 | int is_porta = fsi_is_port_a(fsi); | |
cf6edd00 | 359 | u32 shift; |
a4d7d550 KM |
360 | |
361 | if (is_porta) | |
cf6edd00 | 362 | shift = is_play ? AO_SHIFT : AI_SHIFT; |
a4d7d550 | 363 | else |
cf6edd00 | 364 | shift = is_play ? BO_SHIFT : BI_SHIFT; |
a4d7d550 | 365 | |
cf6edd00 | 366 | return shift; |
a4d7d550 KM |
367 | } |
368 | ||
2e651baf KM |
369 | static int fsi_frame2sample(struct fsi_priv *fsi, int frames) |
370 | { | |
371 | return frames * fsi->chan_num; | |
372 | } | |
373 | ||
374 | static int fsi_sample2frame(struct fsi_priv *fsi, int samples) | |
375 | { | |
376 | return samples / fsi->chan_num; | |
377 | } | |
378 | ||
cda828ca KM |
379 | static int fsi_stream_is_working(struct fsi_priv *fsi, |
380 | int is_play) | |
381 | { | |
382 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); | |
383 | struct fsi_master *master = fsi_get_master(fsi); | |
384 | unsigned long flags; | |
385 | int ret; | |
386 | ||
387 | spin_lock_irqsave(&master->lock, flags); | |
388 | ret = !!io->substream; | |
389 | spin_unlock_irqrestore(&master->lock, flags); | |
390 | ||
391 | return ret; | |
392 | } | |
393 | ||
a4d7d550 | 394 | static void fsi_stream_push(struct fsi_priv *fsi, |
93193c2b | 395 | int is_play, |
0ffe296a | 396 | struct snd_pcm_substream *substream) |
a4d7d550 | 397 | { |
93193c2b | 398 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
0ffe296a | 399 | struct snd_pcm_runtime *runtime = substream->runtime; |
2da65892 KM |
400 | struct fsi_master *master = fsi_get_master(fsi); |
401 | unsigned long flags; | |
93193c2b | 402 | |
2da65892 | 403 | spin_lock_irqsave(&master->lock, flags); |
93193c2b | 404 | io->substream = substream; |
2e651baf KM |
405 | io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size); |
406 | io->buff_sample_pos = 0; | |
407 | io->period_samples = fsi_frame2sample(fsi, runtime->period_size); | |
408 | io->period_pos = 0; | |
1ec9bc35 KM |
409 | io->oerr_num = -1; /* ignore 1st err */ |
410 | io->uerr_num = -1; /* ignore 1st err */ | |
2da65892 | 411 | spin_unlock_irqrestore(&master->lock, flags); |
a4d7d550 KM |
412 | } |
413 | ||
93193c2b | 414 | static void fsi_stream_pop(struct fsi_priv *fsi, int is_play) |
a4d7d550 | 415 | { |
93193c2b | 416 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
1ec9bc35 | 417 | struct snd_soc_dai *dai = fsi_get_dai(io->substream); |
2da65892 KM |
418 | struct fsi_master *master = fsi_get_master(fsi); |
419 | unsigned long flags; | |
1ec9bc35 | 420 | |
2da65892 | 421 | spin_lock_irqsave(&master->lock, flags); |
1ec9bc35 KM |
422 | |
423 | if (io->oerr_num > 0) | |
424 | dev_err(dai->dev, "over_run = %d\n", io->oerr_num); | |
425 | ||
426 | if (io->uerr_num > 0) | |
427 | dev_err(dai->dev, "under_run = %d\n", io->uerr_num); | |
93193c2b KM |
428 | |
429 | io->substream = NULL; | |
2e651baf KM |
430 | io->buff_sample_capa = 0; |
431 | io->buff_sample_pos = 0; | |
432 | io->period_samples = 0; | |
433 | io->period_pos = 0; | |
1ec9bc35 KM |
434 | io->oerr_num = 0; |
435 | io->uerr_num = 0; | |
2da65892 | 436 | spin_unlock_irqrestore(&master->lock, flags); |
a4d7d550 KM |
437 | } |
438 | ||
2e651baf | 439 | static int fsi_get_current_fifo_samples(struct fsi_priv *fsi, int is_play) |
a4d7d550 KM |
440 | { |
441 | u32 status; | |
2e651baf | 442 | int frames; |
a4d7d550 | 443 | |
e8c8b631 KM |
444 | status = is_play ? |
445 | fsi_reg_read(fsi, DOFF_ST) : | |
446 | fsi_reg_read(fsi, DIFF_ST); | |
447 | ||
2e651baf | 448 | frames = 0x1ff & (status >> 8); |
5bfb9ad0 | 449 | |
2e651baf | 450 | return fsi_frame2sample(fsi, frames); |
cca1b235 KM |
451 | } |
452 | ||
1ec9bc35 KM |
453 | static void fsi_count_fifo_err(struct fsi_priv *fsi) |
454 | { | |
455 | u32 ostatus = fsi_reg_read(fsi, DOFF_ST); | |
456 | u32 istatus = fsi_reg_read(fsi, DIFF_ST); | |
457 | ||
458 | if (ostatus & ERR_OVER) | |
459 | fsi->playback.oerr_num++; | |
460 | ||
461 | if (ostatus & ERR_UNDER) | |
462 | fsi->playback.uerr_num++; | |
463 | ||
464 | if (istatus & ERR_OVER) | |
465 | fsi->capture.oerr_num++; | |
466 | ||
467 | if (istatus & ERR_UNDER) | |
468 | fsi->capture.uerr_num++; | |
469 | ||
470 | fsi_reg_write(fsi, DOFF_ST, 0); | |
471 | fsi_reg_write(fsi, DIFF_ST, 0); | |
472 | } | |
473 | ||
b9fde18c KM |
474 | /* |
475 | * dma function | |
476 | */ | |
477 | ||
93193c2b | 478 | static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream) |
c79eab3e | 479 | { |
93193c2b KM |
480 | int is_play = fsi_stream_is_play(stream); |
481 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); | |
2e651baf | 482 | struct snd_pcm_runtime *runtime = io->substream->runtime; |
93193c2b | 483 | |
2e651baf KM |
484 | return runtime->dma_area + |
485 | samples_to_bytes(runtime, io->buff_sample_pos); | |
c79eab3e KM |
486 | } |
487 | ||
5bfb9ad0 | 488 | static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num) |
b9fde18c KM |
489 | { |
490 | u16 *start; | |
491 | int i; | |
492 | ||
93193c2b | 493 | start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK); |
b9fde18c | 494 | |
5bfb9ad0 | 495 | for (i = 0; i < num; i++) |
b9fde18c KM |
496 | fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8)); |
497 | } | |
498 | ||
5bfb9ad0 | 499 | static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num) |
b9fde18c KM |
500 | { |
501 | u16 *start; | |
502 | int i; | |
503 | ||
93193c2b KM |
504 | start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE); |
505 | ||
b9fde18c | 506 | |
5bfb9ad0 | 507 | for (i = 0; i < num; i++) |
b9fde18c KM |
508 | *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8); |
509 | } | |
510 | ||
5bfb9ad0 | 511 | static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num) |
b9fde18c KM |
512 | { |
513 | u32 *start; | |
514 | int i; | |
515 | ||
93193c2b KM |
516 | start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK); |
517 | ||
b9fde18c | 518 | |
5bfb9ad0 | 519 | for (i = 0; i < num; i++) |
b9fde18c KM |
520 | fsi_reg_write(fsi, DODT, *(start + i)); |
521 | } | |
522 | ||
5bfb9ad0 | 523 | static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num) |
b9fde18c KM |
524 | { |
525 | u32 *start; | |
526 | int i; | |
527 | ||
93193c2b | 528 | start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE); |
b9fde18c | 529 | |
5bfb9ad0 | 530 | for (i = 0; i < num; i++) |
b9fde18c KM |
531 | *(start + i) = fsi_reg_read(fsi, DIDT); |
532 | } | |
533 | ||
c8fe2574 KM |
534 | /* |
535 | * irq function | |
536 | */ | |
a4d7d550 | 537 | |
a4d7d550 KM |
538 | static void fsi_irq_enable(struct fsi_priv *fsi, int is_play) |
539 | { | |
cf6edd00 | 540 | u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play)); |
71f6e064 | 541 | struct fsi_master *master = fsi_get_master(fsi); |
a4d7d550 | 542 | |
43fa95ca KM |
543 | fsi_core_mask_set(master, imsk, data, data); |
544 | fsi_core_mask_set(master, iemsk, data, data); | |
a4d7d550 KM |
545 | } |
546 | ||
547 | static void fsi_irq_disable(struct fsi_priv *fsi, int is_play) | |
548 | { | |
cf6edd00 | 549 | u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play)); |
71f6e064 | 550 | struct fsi_master *master = fsi_get_master(fsi); |
a4d7d550 | 551 | |
43fa95ca KM |
552 | fsi_core_mask_set(master, imsk, data, 0); |
553 | fsi_core_mask_set(master, iemsk, data, 0); | |
a4d7d550 KM |
554 | } |
555 | ||
10ea76cc KM |
556 | static u32 fsi_irq_get_status(struct fsi_master *master) |
557 | { | |
43fa95ca | 558 | return fsi_core_read(master, int_st); |
10ea76cc KM |
559 | } |
560 | ||
10ea76cc KM |
561 | static void fsi_irq_clear_status(struct fsi_priv *fsi) |
562 | { | |
563 | u32 data = 0; | |
564 | struct fsi_master *master = fsi_get_master(fsi); | |
565 | ||
cf6edd00 KM |
566 | data |= AB_IO(1, fsi_get_port_shift(fsi, 0)); |
567 | data |= AB_IO(1, fsi_get_port_shift(fsi, 1)); | |
10ea76cc KM |
568 | |
569 | /* clear interrupt factor */ | |
43fa95ca | 570 | fsi_core_mask_set(master, int_st, data, 0); |
10ea76cc KM |
571 | } |
572 | ||
c8fe2574 KM |
573 | /* |
574 | * SPDIF master clock function | |
575 | * | |
576 | * These functions are used later FSI2 | |
577 | */ | |
3bc28070 KM |
578 | static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable) |
579 | { | |
580 | struct fsi_master *master = fsi_get_master(fsi); | |
2b0e7302 | 581 | u32 mask, val; |
3bc28070 KM |
582 | |
583 | if (master->core->ver < 2) { | |
584 | pr_err("fsi: register access err (%s)\n", __func__); | |
585 | return; | |
586 | } | |
587 | ||
2b0e7302 KM |
588 | mask = BP | SE; |
589 | val = enable ? mask : 0; | |
590 | ||
591 | fsi_is_port_a(fsi) ? | |
43fa95ca KM |
592 | fsi_core_mask_set(master, a_mclk, mask, val) : |
593 | fsi_core_mask_set(master, b_mclk, mask, val); | |
3bc28070 KM |
594 | } |
595 | ||
c8fe2574 | 596 | /* |
1f5e2a31 | 597 | * clock function |
c8fe2574 | 598 | */ |
4f56cde1 KM |
599 | static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi, |
600 | long rate, int enable) | |
601 | { | |
602 | struct fsi_master *master = fsi_get_master(fsi); | |
603 | set_rate_func set_rate = fsi_get_info_set_rate(master); | |
604 | int fsi_ver = master->core->ver; | |
605 | int ret; | |
606 | ||
607 | ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable); | |
608 | if (ret < 0) /* error */ | |
609 | return ret; | |
610 | ||
611 | if (!enable) | |
612 | return 0; | |
613 | ||
614 | if (ret > 0) { | |
615 | u32 data = 0; | |
616 | ||
617 | switch (ret & SH_FSI_ACKMD_MASK) { | |
618 | default: | |
619 | /* FALL THROUGH */ | |
620 | case SH_FSI_ACKMD_512: | |
621 | data |= (0x0 << 12); | |
622 | break; | |
623 | case SH_FSI_ACKMD_256: | |
624 | data |= (0x1 << 12); | |
625 | break; | |
626 | case SH_FSI_ACKMD_128: | |
627 | data |= (0x2 << 12); | |
628 | break; | |
629 | case SH_FSI_ACKMD_64: | |
630 | data |= (0x3 << 12); | |
631 | break; | |
632 | case SH_FSI_ACKMD_32: | |
633 | if (fsi_ver < 2) | |
634 | dev_err(dev, "unsupported ACKMD\n"); | |
635 | else | |
636 | data |= (0x4 << 12); | |
637 | break; | |
638 | } | |
639 | ||
640 | switch (ret & SH_FSI_BPFMD_MASK) { | |
641 | default: | |
642 | /* FALL THROUGH */ | |
643 | case SH_FSI_BPFMD_32: | |
644 | data |= (0x0 << 8); | |
645 | break; | |
646 | case SH_FSI_BPFMD_64: | |
647 | data |= (0x1 << 8); | |
648 | break; | |
649 | case SH_FSI_BPFMD_128: | |
650 | data |= (0x2 << 8); | |
651 | break; | |
652 | case SH_FSI_BPFMD_256: | |
653 | data |= (0x3 << 8); | |
654 | break; | |
655 | case SH_FSI_BPFMD_512: | |
656 | data |= (0x4 << 8); | |
657 | break; | |
658 | case SH_FSI_BPFMD_16: | |
659 | if (fsi_ver < 2) | |
660 | dev_err(dev, "unsupported ACKMD\n"); | |
661 | else | |
662 | data |= (0x7 << 8); | |
663 | break; | |
664 | } | |
665 | ||
666 | fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data); | |
667 | udelay(10); | |
668 | ret = 0; | |
669 | } | |
670 | ||
671 | return ret; | |
4f56cde1 KM |
672 | } |
673 | ||
1ddddd36 KM |
674 | #define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1) |
675 | #define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0) | |
676 | static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable) | |
a4d7d550 | 677 | { |
71f6e064 | 678 | struct fsi_master *master = fsi_get_master(fsi); |
1f5e2a31 | 679 | u32 clk = fsi_is_port_a(fsi) ? CRA : CRB; |
a4d7d550 | 680 | |
1ddddd36 KM |
681 | if (enable) |
682 | fsi_irq_enable(fsi, is_play); | |
683 | else | |
684 | fsi_irq_disable(fsi, is_play); | |
685 | ||
cda828ca | 686 | if (fsi_is_clk_master(fsi)) |
1f5e2a31 | 687 | fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0); |
a4d7d550 KM |
688 | } |
689 | ||
1f5e2a31 KM |
690 | /* |
691 | * ctrl function | |
692 | */ | |
4a942b45 KM |
693 | static void fsi_fifo_init(struct fsi_priv *fsi, |
694 | int is_play, | |
23ca8533 | 695 | struct device *dev) |
a4d7d550 | 696 | { |
4a942b45 | 697 | struct fsi_master *master = fsi_get_master(fsi); |
93193c2b | 698 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
e8c8b631 | 699 | u32 shift, i; |
2e651baf | 700 | int frame_capa; |
a4d7d550 | 701 | |
4a942b45 KM |
702 | /* get on-chip RAM capacity */ |
703 | shift = fsi_master_read(master, FIFO_SZ); | |
cf6edd00 KM |
704 | shift >>= fsi_get_port_shift(fsi, is_play); |
705 | shift &= FIFO_SZ_MASK; | |
2e651baf | 706 | frame_capa = 256 << shift; |
23ca8533 | 707 | dev_dbg(dev, "fifo = %d words\n", frame_capa); |
a4d7d550 | 708 | |
4a942b45 KM |
709 | /* |
710 | * The maximum number of sample data varies depending | |
711 | * on the number of channels selected for the format. | |
712 | * | |
713 | * FIFOs are used in 4-channel units in 3-channel mode | |
714 | * and in 8-channel units in 5- to 7-channel mode | |
715 | * meaning that more FIFOs than the required size of DPRAM | |
716 | * are used. | |
717 | * | |
718 | * ex) if 256 words of DP-RAM is connected | |
719 | * 1 channel: 256 (256 x 1 = 256) | |
720 | * 2 channels: 128 (128 x 2 = 256) | |
721 | * 3 channels: 64 ( 64 x 3 = 192) | |
722 | * 4 channels: 64 ( 64 x 4 = 256) | |
723 | * 5 channels: 32 ( 32 x 5 = 160) | |
724 | * 6 channels: 32 ( 32 x 6 = 192) | |
725 | * 7 channels: 32 ( 32 x 7 = 224) | |
726 | * 8 channels: 32 ( 32 x 8 = 256) | |
727 | */ | |
160afa7f | 728 | for (i = 1; i < fsi->chan_num; i <<= 1) |
2e651baf | 729 | frame_capa >>= 1; |
23ca8533 | 730 | dev_dbg(dev, "%d channel %d store\n", |
2e651baf KM |
731 | fsi->chan_num, frame_capa); |
732 | ||
733 | io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa); | |
a4d7d550 | 734 | |
e8c8b631 KM |
735 | /* |
736 | * set interrupt generation factor | |
737 | * clear FIFO | |
738 | */ | |
739 | if (is_play) { | |
740 | fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF); | |
741 | fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR); | |
742 | } else { | |
743 | fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF); | |
744 | fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR); | |
745 | } | |
a4d7d550 KM |
746 | } |
747 | ||
1ec9bc35 | 748 | static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream) |
a4d7d550 KM |
749 | { |
750 | struct snd_pcm_runtime *runtime; | |
751 | struct snd_pcm_substream *substream = NULL; | |
93193c2b KM |
752 | int is_play = fsi_stream_is_play(stream); |
753 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); | |
2e651baf KM |
754 | int sample_residues; |
755 | int sample_width; | |
756 | int samples; | |
757 | int samples_max; | |
b9fde18c | 758 | int over_period; |
d8b33534 | 759 | void (*fn)(struct fsi_priv *fsi, int size); |
a4d7d550 KM |
760 | |
761 | if (!fsi || | |
93193c2b KM |
762 | !io->substream || |
763 | !io->substream->runtime) | |
a4d7d550 KM |
764 | return -EINVAL; |
765 | ||
1c418d1f | 766 | over_period = 0; |
93193c2b | 767 | substream = io->substream; |
1c418d1f | 768 | runtime = substream->runtime; |
a4d7d550 KM |
769 | |
770 | /* FSI FIFO has limit. | |
771 | * So, this driver can not send periods data at a time | |
772 | */ | |
2e651baf KM |
773 | if (io->buff_sample_pos >= |
774 | io->period_samples * (io->period_pos + 1)) { | |
a4d7d550 | 775 | |
1c418d1f | 776 | over_period = 1; |
2e651baf | 777 | io->period_pos = (io->period_pos + 1) % runtime->periods; |
a4d7d550 | 778 | |
2e651baf KM |
779 | if (0 == io->period_pos) |
780 | io->buff_sample_pos = 0; | |
a4d7d550 KM |
781 | } |
782 | ||
2e651baf KM |
783 | /* get 1 sample data width */ |
784 | sample_width = samples_to_bytes(runtime, 1); | |
a4d7d550 | 785 | |
2e651baf KM |
786 | /* get number of residue samples */ |
787 | sample_residues = io->buff_sample_capa - io->buff_sample_pos; | |
d8b33534 KM |
788 | |
789 | if (is_play) { | |
790 | /* | |
791 | * for play-back | |
792 | * | |
2e651baf KM |
793 | * samples_max : number of FSI fifo free samples space |
794 | * samples : number of ALSA residue samples | |
d8b33534 | 795 | */ |
2e651baf KM |
796 | samples_max = io->fifo_sample_capa; |
797 | samples_max -= fsi_get_current_fifo_samples(fsi, is_play); | |
d8b33534 | 798 | |
2e651baf | 799 | samples = sample_residues; |
d8b33534 | 800 | |
2e651baf | 801 | switch (sample_width) { |
d8b33534 KM |
802 | case 2: |
803 | fn = fsi_dma_soft_push16; | |
804 | break; | |
805 | case 4: | |
806 | fn = fsi_dma_soft_push32; | |
807 | break; | |
808 | default: | |
809 | return -EINVAL; | |
810 | } | |
811 | } else { | |
812 | /* | |
813 | * for capture | |
814 | * | |
2e651baf KM |
815 | * samples_max : number of ALSA free samples space |
816 | * samples : number of samples in FSI fifo | |
d8b33534 | 817 | */ |
2e651baf KM |
818 | samples_max = sample_residues; |
819 | samples = fsi_get_current_fifo_samples(fsi, is_play); | |
d8b33534 | 820 | |
2e651baf | 821 | switch (sample_width) { |
d8b33534 KM |
822 | case 2: |
823 | fn = fsi_dma_soft_pop16; | |
824 | break; | |
825 | case 4: | |
826 | fn = fsi_dma_soft_pop32; | |
827 | break; | |
828 | default: | |
829 | return -EINVAL; | |
830 | } | |
831 | } | |
a4d7d550 | 832 | |
2e651baf | 833 | samples = min(samples, samples_max); |
a4d7d550 | 834 | |
2e651baf | 835 | fn(fsi, samples); |
a4d7d550 | 836 | |
2e651baf KM |
837 | /* update buff_sample_pos */ |
838 | io->buff_sample_pos += samples; | |
a4d7d550 | 839 | |
1c418d1f | 840 | if (over_period) |
a4d7d550 KM |
841 | snd_pcm_period_elapsed(substream); |
842 | ||
47fc9a0a | 843 | return 0; |
a4d7d550 KM |
844 | } |
845 | ||
1ec9bc35 | 846 | static int fsi_data_pop(struct fsi_priv *fsi) |
07102f3c | 847 | { |
1ec9bc35 | 848 | return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE); |
d8b33534 | 849 | } |
07102f3c | 850 | |
1ec9bc35 | 851 | static int fsi_data_push(struct fsi_priv *fsi) |
d8b33534 | 852 | { |
1ec9bc35 | 853 | return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK); |
07102f3c KM |
854 | } |
855 | ||
a4d7d550 KM |
856 | static irqreturn_t fsi_interrupt(int irq, void *data) |
857 | { | |
71f6e064 | 858 | struct fsi_master *master = data; |
10ea76cc | 859 | u32 int_st = fsi_irq_get_status(master); |
a4d7d550 KM |
860 | |
861 | /* clear irq status */ | |
feb58cff KM |
862 | fsi_master_mask_set(master, SOFT_RST, IR, 0); |
863 | fsi_master_mask_set(master, SOFT_RST, IR, IR); | |
a4d7d550 | 864 | |
cf6edd00 | 865 | if (int_st & AB_IO(1, AO_SHIFT)) |
1ec9bc35 | 866 | fsi_data_push(&master->fsia); |
cf6edd00 | 867 | if (int_st & AB_IO(1, BO_SHIFT)) |
1ec9bc35 | 868 | fsi_data_push(&master->fsib); |
cf6edd00 | 869 | if (int_st & AB_IO(1, AI_SHIFT)) |
1ec9bc35 | 870 | fsi_data_pop(&master->fsia); |
cf6edd00 | 871 | if (int_st & AB_IO(1, BI_SHIFT)) |
1ec9bc35 KM |
872 | fsi_data_pop(&master->fsib); |
873 | ||
874 | fsi_count_fifo_err(&master->fsia); | |
875 | fsi_count_fifo_err(&master->fsib); | |
a4d7d550 | 876 | |
48d78e58 KM |
877 | fsi_irq_clear_status(&master->fsia); |
878 | fsi_irq_clear_status(&master->fsib); | |
a4d7d550 KM |
879 | |
880 | return IRQ_HANDLED; | |
881 | } | |
882 | ||
c8fe2574 KM |
883 | /* |
884 | * dai ops | |
885 | */ | |
a4d7d550 | 886 | |
23ca8533 KM |
887 | static int fsi_hw_startup(struct fsi_priv *fsi, |
888 | int is_play, | |
889 | struct device *dev) | |
a4d7d550 | 890 | { |
65ff03f4 KM |
891 | struct fsi_master *master = fsi_get_master(fsi); |
892 | int fsi_ver = master->core->ver; | |
93193c2b | 893 | u32 flags = fsi_get_info_flags(fsi); |
9478e0b6 | 894 | u32 data = 0; |
a4d7d550 | 895 | |
9478e0b6 KM |
896 | /* clock setting */ |
897 | if (fsi_is_clk_master(fsi)) | |
898 | data = DIMD | DOMD; | |
899 | ||
900 | fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data); | |
a4d7d550 KM |
901 | |
902 | /* clock inversion (CKG2) */ | |
903 | data = 0; | |
b427b44c KM |
904 | if (SH_FSI_LRM_INV & flags) |
905 | data |= 1 << 12; | |
906 | if (SH_FSI_BRM_INV & flags) | |
907 | data |= 1 << 8; | |
908 | if (SH_FSI_LRS_INV & flags) | |
909 | data |= 1 << 4; | |
910 | if (SH_FSI_BRS_INV & flags) | |
911 | data |= 1 << 0; | |
912 | ||
a4d7d550 KM |
913 | fsi_reg_write(fsi, CKG2, data); |
914 | ||
9478e0b6 KM |
915 | /* set format */ |
916 | fsi_reg_write(fsi, DO_FMT, fsi->do_fmt); | |
917 | fsi_reg_write(fsi, DI_FMT, fsi->di_fmt); | |
918 | ||
919 | /* spdif ? */ | |
920 | if (fsi_is_spdif(fsi)) { | |
921 | fsi_spdif_clk_ctrl(fsi, 1); | |
922 | fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD); | |
923 | } | |
924 | ||
65ff03f4 KM |
925 | /* |
926 | * FIXME | |
927 | * | |
928 | * FSI driver assumed that data package is in-back. | |
929 | * FSI2 chip can select it. | |
930 | */ | |
931 | if (fsi_ver >= 2) { | |
932 | fsi_reg_write(fsi, OUT_DMAC, (1 << 4)); | |
933 | fsi_reg_write(fsi, IN_DMAC, (1 << 4)); | |
934 | } | |
935 | ||
10ea76cc KM |
936 | /* irq clear */ |
937 | fsi_irq_disable(fsi, is_play); | |
938 | fsi_irq_clear_status(fsi); | |
939 | ||
940 | /* fifo init */ | |
23ca8533 | 941 | fsi_fifo_init(fsi, is_play, dev); |
a4d7d550 | 942 | |
a68a3b4e | 943 | return 0; |
a4d7d550 KM |
944 | } |
945 | ||
23ca8533 KM |
946 | static void fsi_hw_shutdown(struct fsi_priv *fsi, |
947 | int is_play, | |
948 | struct device *dev) | |
949 | { | |
950 | if (fsi_is_clk_master(fsi)) | |
951 | fsi_set_master_clk(dev, fsi, fsi->rate, 0); | |
23ca8533 KM |
952 | } |
953 | ||
954 | static int fsi_dai_startup(struct snd_pcm_substream *substream, | |
955 | struct snd_soc_dai *dai) | |
956 | { | |
957 | struct fsi_priv *fsi = fsi_get_priv(substream); | |
958 | int is_play = fsi_is_play(substream); | |
959 | ||
960 | return fsi_hw_startup(fsi, is_play, dai->dev); | |
961 | } | |
962 | ||
a4d7d550 KM |
963 | static void fsi_dai_shutdown(struct snd_pcm_substream *substream, |
964 | struct snd_soc_dai *dai) | |
965 | { | |
71f6e064 | 966 | struct fsi_priv *fsi = fsi_get_priv(substream); |
23ca8533 | 967 | int is_play = fsi_is_play(substream); |
a4d7d550 | 968 | |
23ca8533 | 969 | fsi_hw_shutdown(fsi, is_play, dai->dev); |
d4bc99b9 | 970 | fsi->rate = 0; |
a4d7d550 KM |
971 | } |
972 | ||
973 | static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd, | |
974 | struct snd_soc_dai *dai) | |
975 | { | |
71f6e064 | 976 | struct fsi_priv *fsi = fsi_get_priv(substream); |
00545785 | 977 | int is_play = fsi_is_play(substream); |
a4d7d550 KM |
978 | int ret = 0; |
979 | ||
a4d7d550 KM |
980 | switch (cmd) { |
981 | case SNDRV_PCM_TRIGGER_START: | |
0ffe296a | 982 | fsi_stream_push(fsi, is_play, substream); |
1ec9bc35 | 983 | ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi); |
1ddddd36 | 984 | fsi_port_start(fsi, is_play); |
a4d7d550 KM |
985 | break; |
986 | case SNDRV_PCM_TRIGGER_STOP: | |
1ddddd36 | 987 | fsi_port_stop(fsi, is_play); |
93193c2b | 988 | fsi_stream_pop(fsi, is_play); |
a4d7d550 KM |
989 | break; |
990 | } | |
991 | ||
992 | return ret; | |
993 | } | |
994 | ||
f17c13ca KM |
995 | static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt) |
996 | { | |
997 | u32 data = 0; | |
998 | ||
999 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
1000 | case SND_SOC_DAIFMT_I2S: | |
1001 | data = CR_I2S; | |
1002 | fsi->chan_num = 2; | |
1003 | break; | |
1004 | case SND_SOC_DAIFMT_LEFT_J: | |
1005 | data = CR_PCM; | |
1006 | fsi->chan_num = 2; | |
1007 | break; | |
1008 | default: | |
1009 | return -EINVAL; | |
1010 | } | |
1011 | ||
9478e0b6 KM |
1012 | fsi->do_fmt = data; |
1013 | fsi->di_fmt = data; | |
f17c13ca KM |
1014 | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | static int fsi_set_fmt_spdif(struct fsi_priv *fsi) | |
1019 | { | |
1020 | struct fsi_master *master = fsi_get_master(fsi); | |
1021 | u32 data = 0; | |
1022 | ||
1023 | if (master->core->ver < 2) | |
1024 | return -EINVAL; | |
1025 | ||
1026 | data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM; | |
1027 | fsi->chan_num = 2; | |
9478e0b6 | 1028 | fsi->spdif = 1; |
f17c13ca | 1029 | |
9478e0b6 KM |
1030 | fsi->do_fmt = data; |
1031 | fsi->di_fmt = data; | |
f17c13ca KM |
1032 | |
1033 | return 0; | |
1034 | } | |
1035 | ||
4d805f7b KM |
1036 | static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
1037 | { | |
1038 | struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai); | |
6a9ebad8 KM |
1039 | struct fsi_master *master = fsi_get_master(fsi); |
1040 | set_rate_func set_rate = fsi_get_info_set_rate(master); | |
f17c13ca | 1041 | u32 flags = fsi_get_info_flags(fsi); |
4d805f7b KM |
1042 | int ret; |
1043 | ||
4d805f7b KM |
1044 | /* set master/slave audio interface */ |
1045 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
1046 | case SND_SOC_DAIFMT_CBM_CFM: | |
6a9ebad8 | 1047 | fsi->clk_master = 1; |
4d805f7b KM |
1048 | break; |
1049 | case SND_SOC_DAIFMT_CBS_CFS: | |
1050 | break; | |
1051 | default: | |
9478e0b6 | 1052 | return -EINVAL; |
4d805f7b | 1053 | } |
6a9ebad8 KM |
1054 | |
1055 | if (fsi_is_clk_master(fsi) && !set_rate) { | |
1056 | dev_err(dai->dev, "platform doesn't have set_rate\n"); | |
9478e0b6 | 1057 | return -EINVAL; |
6a9ebad8 KM |
1058 | } |
1059 | ||
f17c13ca KM |
1060 | /* set format */ |
1061 | switch (flags & SH_FSI_FMT_MASK) { | |
1062 | case SH_FSI_FMT_DAI: | |
1063 | ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK); | |
1064 | break; | |
1065 | case SH_FSI_FMT_SPDIF: | |
1066 | ret = fsi_set_fmt_spdif(fsi); | |
1067 | break; | |
1068 | default: | |
1069 | ret = -EINVAL; | |
1070 | } | |
4d805f7b | 1071 | |
4d805f7b KM |
1072 | return ret; |
1073 | } | |
1074 | ||
ccad7b44 KM |
1075 | static int fsi_dai_hw_params(struct snd_pcm_substream *substream, |
1076 | struct snd_pcm_hw_params *params, | |
1077 | struct snd_soc_dai *dai) | |
1078 | { | |
1079 | struct fsi_priv *fsi = fsi_get_priv(substream); | |
d4bc99b9 | 1080 | long rate = params_rate(params); |
ccad7b44 KM |
1081 | int ret; |
1082 | ||
6a9ebad8 | 1083 | if (!fsi_is_clk_master(fsi)) |
ccad7b44 KM |
1084 | return 0; |
1085 | ||
4f56cde1 KM |
1086 | ret = fsi_set_master_clk(dai->dev, fsi, rate, 1); |
1087 | if (ret < 0) | |
d4bc99b9 | 1088 | return ret; |
ccad7b44 | 1089 | |
d4bc99b9 | 1090 | fsi->rate = rate; |
ccad7b44 KM |
1091 | |
1092 | return ret; | |
ccad7b44 KM |
1093 | } |
1094 | ||
85e7652d | 1095 | static const struct snd_soc_dai_ops fsi_dai_ops = { |
a4d7d550 KM |
1096 | .startup = fsi_dai_startup, |
1097 | .shutdown = fsi_dai_shutdown, | |
1098 | .trigger = fsi_dai_trigger, | |
4d805f7b | 1099 | .set_fmt = fsi_dai_set_fmt, |
ccad7b44 | 1100 | .hw_params = fsi_dai_hw_params, |
a4d7d550 KM |
1101 | }; |
1102 | ||
c8fe2574 KM |
1103 | /* |
1104 | * pcm ops | |
1105 | */ | |
a4d7d550 | 1106 | |
a4d7d550 KM |
1107 | static struct snd_pcm_hardware fsi_pcm_hardware = { |
1108 | .info = SNDRV_PCM_INFO_INTERLEAVED | | |
1109 | SNDRV_PCM_INFO_MMAP | | |
1110 | SNDRV_PCM_INFO_MMAP_VALID | | |
1111 | SNDRV_PCM_INFO_PAUSE, | |
1112 | .formats = FSI_FMTS, | |
1113 | .rates = FSI_RATES, | |
1114 | .rate_min = 8000, | |
1115 | .rate_max = 192000, | |
1116 | .channels_min = 1, | |
1117 | .channels_max = 2, | |
1118 | .buffer_bytes_max = 64 * 1024, | |
1119 | .period_bytes_min = 32, | |
1120 | .period_bytes_max = 8192, | |
1121 | .periods_min = 1, | |
1122 | .periods_max = 32, | |
1123 | .fifo_size = 256, | |
1124 | }; | |
1125 | ||
1126 | static int fsi_pcm_open(struct snd_pcm_substream *substream) | |
1127 | { | |
1128 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1129 | int ret = 0; | |
1130 | ||
1131 | snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware); | |
1132 | ||
1133 | ret = snd_pcm_hw_constraint_integer(runtime, | |
1134 | SNDRV_PCM_HW_PARAM_PERIODS); | |
1135 | ||
1136 | return ret; | |
1137 | } | |
1138 | ||
1139 | static int fsi_hw_params(struct snd_pcm_substream *substream, | |
1140 | struct snd_pcm_hw_params *hw_params) | |
1141 | { | |
1142 | return snd_pcm_lib_malloc_pages(substream, | |
1143 | params_buffer_bytes(hw_params)); | |
1144 | } | |
1145 | ||
1146 | static int fsi_hw_free(struct snd_pcm_substream *substream) | |
1147 | { | |
1148 | return snd_pcm_lib_free_pages(substream); | |
1149 | } | |
1150 | ||
1151 | static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream) | |
1152 | { | |
71f6e064 | 1153 | struct fsi_priv *fsi = fsi_get_priv(substream); |
93193c2b | 1154 | struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream)); |
a4d7d550 | 1155 | |
1987877d | 1156 | return fsi_sample2frame(fsi, io->buff_sample_pos); |
a4d7d550 KM |
1157 | } |
1158 | ||
1159 | static struct snd_pcm_ops fsi_pcm_ops = { | |
1160 | .open = fsi_pcm_open, | |
1161 | .ioctl = snd_pcm_lib_ioctl, | |
1162 | .hw_params = fsi_hw_params, | |
1163 | .hw_free = fsi_hw_free, | |
1164 | .pointer = fsi_pointer, | |
1165 | }; | |
1166 | ||
c8fe2574 KM |
1167 | /* |
1168 | * snd_soc_platform | |
1169 | */ | |
a4d7d550 | 1170 | |
a4d7d550 KM |
1171 | #define PREALLOC_BUFFER (32 * 1024) |
1172 | #define PREALLOC_BUFFER_MAX (32 * 1024) | |
1173 | ||
1174 | static void fsi_pcm_free(struct snd_pcm *pcm) | |
1175 | { | |
1176 | snd_pcm_lib_preallocate_free_for_all(pcm); | |
1177 | } | |
1178 | ||
552d1ef6 | 1179 | static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd) |
a4d7d550 | 1180 | { |
552d1ef6 LG |
1181 | struct snd_pcm *pcm = rtd->pcm; |
1182 | ||
a4d7d550 KM |
1183 | /* |
1184 | * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel | |
1185 | * in MMAP mode (i.e. aplay -M) | |
1186 | */ | |
1187 | return snd_pcm_lib_preallocate_pages_for_all( | |
1188 | pcm, | |
1189 | SNDRV_DMA_TYPE_CONTINUOUS, | |
1190 | snd_dma_continuous_data(GFP_KERNEL), | |
1191 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); | |
1192 | } | |
1193 | ||
c8fe2574 KM |
1194 | /* |
1195 | * alsa struct | |
1196 | */ | |
a4d7d550 | 1197 | |
f0fba2ad | 1198 | static struct snd_soc_dai_driver fsi_soc_dai[] = { |
a4d7d550 | 1199 | { |
f0fba2ad | 1200 | .name = "fsia-dai", |
a4d7d550 KM |
1201 | .playback = { |
1202 | .rates = FSI_RATES, | |
1203 | .formats = FSI_FMTS, | |
1204 | .channels_min = 1, | |
1205 | .channels_max = 8, | |
1206 | }, | |
07102f3c KM |
1207 | .capture = { |
1208 | .rates = FSI_RATES, | |
1209 | .formats = FSI_FMTS, | |
1210 | .channels_min = 1, | |
1211 | .channels_max = 8, | |
1212 | }, | |
a4d7d550 KM |
1213 | .ops = &fsi_dai_ops, |
1214 | }, | |
1215 | { | |
f0fba2ad | 1216 | .name = "fsib-dai", |
a4d7d550 KM |
1217 | .playback = { |
1218 | .rates = FSI_RATES, | |
1219 | .formats = FSI_FMTS, | |
1220 | .channels_min = 1, | |
1221 | .channels_max = 8, | |
1222 | }, | |
07102f3c KM |
1223 | .capture = { |
1224 | .rates = FSI_RATES, | |
1225 | .formats = FSI_FMTS, | |
1226 | .channels_min = 1, | |
1227 | .channels_max = 8, | |
1228 | }, | |
a4d7d550 KM |
1229 | .ops = &fsi_dai_ops, |
1230 | }, | |
1231 | }; | |
a4d7d550 | 1232 | |
f0fba2ad LG |
1233 | static struct snd_soc_platform_driver fsi_soc_platform = { |
1234 | .ops = &fsi_pcm_ops, | |
a4d7d550 KM |
1235 | .pcm_new = fsi_pcm_new, |
1236 | .pcm_free = fsi_pcm_free, | |
1237 | }; | |
a4d7d550 | 1238 | |
c8fe2574 KM |
1239 | /* |
1240 | * platform function | |
1241 | */ | |
a4d7d550 | 1242 | |
a4d7d550 KM |
1243 | static int fsi_probe(struct platform_device *pdev) |
1244 | { | |
71f6e064 | 1245 | struct fsi_master *master; |
cc780d38 | 1246 | const struct platform_device_id *id_entry; |
a4d7d550 | 1247 | struct resource *res; |
a4d7d550 KM |
1248 | unsigned int irq; |
1249 | int ret; | |
1250 | ||
cc780d38 KM |
1251 | id_entry = pdev->id_entry; |
1252 | if (!id_entry) { | |
1253 | dev_err(&pdev->dev, "unknown fsi device\n"); | |
1254 | return -ENODEV; | |
1255 | } | |
1256 | ||
a4d7d550 KM |
1257 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
1258 | irq = platform_get_irq(pdev, 0); | |
b6aa1793 | 1259 | if (!res || (int)irq <= 0) { |
a4d7d550 KM |
1260 | dev_err(&pdev->dev, "Not enough FSI platform resources.\n"); |
1261 | ret = -ENODEV; | |
1262 | goto exit; | |
1263 | } | |
1264 | ||
1265 | master = kzalloc(sizeof(*master), GFP_KERNEL); | |
1266 | if (!master) { | |
1267 | dev_err(&pdev->dev, "Could not allocate master\n"); | |
1268 | ret = -ENOMEM; | |
1269 | goto exit; | |
1270 | } | |
1271 | ||
1272 | master->base = ioremap_nocache(res->start, resource_size(res)); | |
1273 | if (!master->base) { | |
1274 | ret = -ENXIO; | |
1275 | dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n"); | |
1276 | goto exit_kfree; | |
1277 | } | |
1278 | ||
3bc28070 | 1279 | /* master setting */ |
a4d7d550 KM |
1280 | master->irq = irq; |
1281 | master->info = pdev->dev.platform_data; | |
3bc28070 KM |
1282 | master->core = (struct fsi_core *)id_entry->driver_data; |
1283 | spin_lock_init(&master->lock); | |
1284 | ||
1285 | /* FSI A setting */ | |
a4d7d550 | 1286 | master->fsia.base = master->base; |
71f6e064 | 1287 | master->fsia.master = master; |
3bc28070 KM |
1288 | |
1289 | /* FSI B setting */ | |
a4d7d550 | 1290 | master->fsib.base = master->base + 0x40; |
71f6e064 | 1291 | master->fsib.master = master; |
a4d7d550 | 1292 | |
785d1c45 | 1293 | pm_runtime_enable(&pdev->dev); |
f0fba2ad | 1294 | dev_set_drvdata(&pdev->dev, master); |
a4d7d550 | 1295 | |
88e24c3a | 1296 | ret = request_irq(irq, &fsi_interrupt, 0, |
cc780d38 | 1297 | id_entry->name, master); |
a4d7d550 KM |
1298 | if (ret) { |
1299 | dev_err(&pdev->dev, "irq request err\n"); | |
9ddc9aa9 | 1300 | goto exit_iounmap; |
a4d7d550 KM |
1301 | } |
1302 | ||
f0fba2ad | 1303 | ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform); |
a4d7d550 KM |
1304 | if (ret < 0) { |
1305 | dev_err(&pdev->dev, "cannot snd soc register\n"); | |
1306 | goto exit_free_irq; | |
1307 | } | |
1308 | ||
0b5ec87d KM |
1309 | ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai, |
1310 | ARRAY_SIZE(fsi_soc_dai)); | |
1311 | if (ret < 0) { | |
1312 | dev_err(&pdev->dev, "cannot snd dai register\n"); | |
1313 | goto exit_snd_soc; | |
1314 | } | |
a4d7d550 | 1315 | |
0b5ec87d KM |
1316 | return ret; |
1317 | ||
1318 | exit_snd_soc: | |
1319 | snd_soc_unregister_platform(&pdev->dev); | |
a4d7d550 KM |
1320 | exit_free_irq: |
1321 | free_irq(irq, master); | |
a4d7d550 KM |
1322 | exit_iounmap: |
1323 | iounmap(master->base); | |
785d1c45 | 1324 | pm_runtime_disable(&pdev->dev); |
a4d7d550 KM |
1325 | exit_kfree: |
1326 | kfree(master); | |
1327 | master = NULL; | |
1328 | exit: | |
1329 | return ret; | |
1330 | } | |
1331 | ||
1332 | static int fsi_remove(struct platform_device *pdev) | |
1333 | { | |
71f6e064 KM |
1334 | struct fsi_master *master; |
1335 | ||
f0fba2ad | 1336 | master = dev_get_drvdata(&pdev->dev); |
71f6e064 | 1337 | |
d985f27e | 1338 | free_irq(master->irq, master); |
785d1c45 | 1339 | pm_runtime_disable(&pdev->dev); |
a4d7d550 | 1340 | |
d985f27e KM |
1341 | snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai)); |
1342 | snd_soc_unregister_platform(&pdev->dev); | |
a4d7d550 KM |
1343 | |
1344 | iounmap(master->base); | |
1345 | kfree(master); | |
71f6e064 | 1346 | |
a4d7d550 KM |
1347 | return 0; |
1348 | } | |
1349 | ||
106c79ec | 1350 | static void __fsi_suspend(struct fsi_priv *fsi, |
cda828ca | 1351 | int is_play, |
4f56cde1 | 1352 | struct device *dev) |
106c79ec | 1353 | { |
cda828ca KM |
1354 | if (!fsi_stream_is_working(fsi, is_play)) |
1355 | return; | |
106c79ec | 1356 | |
cda828ca KM |
1357 | fsi_port_stop(fsi, is_play); |
1358 | fsi_hw_shutdown(fsi, is_play, dev); | |
106c79ec KM |
1359 | } |
1360 | ||
1361 | static void __fsi_resume(struct fsi_priv *fsi, | |
cda828ca | 1362 | int is_play, |
4f56cde1 | 1363 | struct device *dev) |
106c79ec | 1364 | { |
cda828ca KM |
1365 | if (!fsi_stream_is_working(fsi, is_play)) |
1366 | return; | |
106c79ec | 1367 | |
cda828ca KM |
1368 | fsi_hw_startup(fsi, is_play, dev); |
1369 | ||
1370 | if (fsi_is_clk_master(fsi) && fsi->rate) | |
4f56cde1 | 1371 | fsi_set_master_clk(dev, fsi, fsi->rate, 1); |
cda828ca KM |
1372 | |
1373 | fsi_port_start(fsi, is_play); | |
1374 | ||
106c79ec KM |
1375 | } |
1376 | ||
1377 | static int fsi_suspend(struct device *dev) | |
1378 | { | |
1379 | struct fsi_master *master = dev_get_drvdata(dev); | |
cda828ca KM |
1380 | struct fsi_priv *fsia = &master->fsia; |
1381 | struct fsi_priv *fsib = &master->fsib; | |
106c79ec | 1382 | |
cda828ca KM |
1383 | __fsi_suspend(fsia, 1, dev); |
1384 | __fsi_suspend(fsia, 0, dev); | |
106c79ec | 1385 | |
cda828ca KM |
1386 | __fsi_suspend(fsib, 1, dev); |
1387 | __fsi_suspend(fsib, 0, dev); | |
106c79ec KM |
1388 | |
1389 | return 0; | |
1390 | } | |
1391 | ||
1392 | static int fsi_resume(struct device *dev) | |
1393 | { | |
1394 | struct fsi_master *master = dev_get_drvdata(dev); | |
cda828ca KM |
1395 | struct fsi_priv *fsia = &master->fsia; |
1396 | struct fsi_priv *fsib = &master->fsib; | |
106c79ec | 1397 | |
cda828ca KM |
1398 | __fsi_resume(fsia, 1, dev); |
1399 | __fsi_resume(fsia, 0, dev); | |
106c79ec | 1400 | |
cda828ca KM |
1401 | __fsi_resume(fsib, 1, dev); |
1402 | __fsi_resume(fsib, 0, dev); | |
106c79ec KM |
1403 | |
1404 | return 0; | |
1405 | } | |
1406 | ||
785d1c45 KM |
1407 | static int fsi_runtime_nop(struct device *dev) |
1408 | { | |
1409 | /* Runtime PM callback shared between ->runtime_suspend() | |
1410 | * and ->runtime_resume(). Simply returns success. | |
1411 | * | |
1412 | * This driver re-initializes all registers after | |
1413 | * pm_runtime_get_sync() anyway so there is no need | |
1414 | * to save and restore registers here. | |
1415 | */ | |
1416 | return 0; | |
1417 | } | |
1418 | ||
1419 | static struct dev_pm_ops fsi_pm_ops = { | |
106c79ec KM |
1420 | .suspend = fsi_suspend, |
1421 | .resume = fsi_resume, | |
785d1c45 KM |
1422 | .runtime_suspend = fsi_runtime_nop, |
1423 | .runtime_resume = fsi_runtime_nop, | |
1424 | }; | |
1425 | ||
73b92c1f KM |
1426 | static struct fsi_core fsi1_core = { |
1427 | .ver = 1, | |
1428 | ||
1429 | /* Interrupt */ | |
cc780d38 KM |
1430 | .int_st = INT_ST, |
1431 | .iemsk = IEMSK, | |
1432 | .imsk = IMSK, | |
1433 | }; | |
1434 | ||
73b92c1f KM |
1435 | static struct fsi_core fsi2_core = { |
1436 | .ver = 2, | |
1437 | ||
1438 | /* Interrupt */ | |
cc780d38 KM |
1439 | .int_st = CPU_INT_ST, |
1440 | .iemsk = CPU_IEMSK, | |
1441 | .imsk = CPU_IMSK, | |
2b0e7302 KM |
1442 | .a_mclk = A_MST_CTLR, |
1443 | .b_mclk = B_MST_CTLR, | |
cc780d38 KM |
1444 | }; |
1445 | ||
1446 | static struct platform_device_id fsi_id_table[] = { | |
73b92c1f KM |
1447 | { "sh_fsi", (kernel_ulong_t)&fsi1_core }, |
1448 | { "sh_fsi2", (kernel_ulong_t)&fsi2_core }, | |
05c69450 | 1449 | {}, |
cc780d38 | 1450 | }; |
d85a6d7b | 1451 | MODULE_DEVICE_TABLE(platform, fsi_id_table); |
cc780d38 | 1452 | |
a4d7d550 KM |
1453 | static struct platform_driver fsi_driver = { |
1454 | .driver = { | |
f0fba2ad | 1455 | .name = "fsi-pcm-audio", |
785d1c45 | 1456 | .pm = &fsi_pm_ops, |
a4d7d550 KM |
1457 | }, |
1458 | .probe = fsi_probe, | |
1459 | .remove = fsi_remove, | |
cc780d38 | 1460 | .id_table = fsi_id_table, |
a4d7d550 KM |
1461 | }; |
1462 | ||
cb5e8738 | 1463 | module_platform_driver(fsi_driver); |
a4d7d550 KM |
1464 | |
1465 | MODULE_LICENSE("GPL"); | |
1466 | MODULE_DESCRIPTION("SuperH onchip FSI audio driver"); | |
1467 | MODULE_AUTHOR("Kuninori Morimoto <[email protected]>"); | |
b3c27b51 | 1468 | MODULE_ALIAS("platform:fsi-pcm-audio"); |