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