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 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/list.h> |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 20 | #include <linux/pm_runtime.h> |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 21 | #include <linux/io.h> |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/initval.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/sh_fsi.h> |
| 28 | #include <asm/atomic.h> |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 29 | |
| 30 | #define DO_FMT 0x0000 |
| 31 | #define DOFF_CTL 0x0004 |
| 32 | #define DOFF_ST 0x0008 |
| 33 | #define DI_FMT 0x000C |
| 34 | #define DIFF_CTL 0x0010 |
| 35 | #define DIFF_ST 0x0014 |
| 36 | #define CKG1 0x0018 |
| 37 | #define CKG2 0x001C |
| 38 | #define DIDT 0x0020 |
| 39 | #define DODT 0x0024 |
| 40 | #define MUTE_ST 0x0028 |
| 41 | #define REG_END MUTE_ST |
| 42 | |
| 43 | #define INT_ST 0x0200 |
| 44 | #define IEMSK 0x0204 |
| 45 | #define IMSK 0x0208 |
| 46 | #define MUTE 0x020C |
| 47 | #define CLK_RST 0x0210 |
| 48 | #define SOFT_RST 0x0214 |
| 49 | #define MREG_START INT_ST |
| 50 | #define MREG_END SOFT_RST |
| 51 | |
| 52 | /* DO_FMT */ |
| 53 | /* DI_FMT */ |
| 54 | #define CR_FMT(param) ((param) << 4) |
| 55 | # define CR_MONO 0x0 |
| 56 | # define CR_MONO_D 0x1 |
| 57 | # define CR_PCM 0x2 |
| 58 | # define CR_I2S 0x3 |
| 59 | # define CR_TDM 0x4 |
| 60 | # define CR_TDM_D 0x5 |
| 61 | |
| 62 | /* DOFF_CTL */ |
| 63 | /* DIFF_CTL */ |
| 64 | #define IRQ_HALF 0x00100000 |
| 65 | #define FIFO_CLR 0x00000001 |
| 66 | |
| 67 | /* DOFF_ST */ |
| 68 | #define ERR_OVER 0x00000010 |
| 69 | #define ERR_UNDER 0x00000001 |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 70 | #define ST_ERR (ERR_OVER | ERR_UNDER) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 71 | |
| 72 | /* CLK_RST */ |
| 73 | #define B_CLK 0x00000010 |
| 74 | #define A_CLK 0x00000001 |
| 75 | |
| 76 | /* INT_ST */ |
| 77 | #define INT_B_IN (1 << 12) |
| 78 | #define INT_B_OUT (1 << 8) |
| 79 | #define INT_A_IN (1 << 4) |
| 80 | #define INT_A_OUT (1 << 0) |
| 81 | |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 82 | /* SOFT_RST */ |
| 83 | #define PBSR (1 << 12) /* Port B Software Reset */ |
| 84 | #define PASR (1 << 8) /* Port A Software Reset */ |
| 85 | #define IR (1 << 4) /* Interrupt Reset */ |
| 86 | #define FSISR (1 << 0) /* Software Reset */ |
| 87 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 88 | #define FSI_RATES SNDRV_PCM_RATE_8000_96000 |
| 89 | |
| 90 | #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE) |
| 91 | |
| 92 | /************************************************************************ |
| 93 | |
| 94 | |
| 95 | struct |
| 96 | |
| 97 | |
| 98 | ************************************************************************/ |
| 99 | struct fsi_priv { |
| 100 | void __iomem *base; |
| 101 | struct snd_pcm_substream *substream; |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 102 | struct fsi_master *master; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 103 | |
| 104 | int fifo_max; |
| 105 | int chan; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 106 | |
| 107 | int byte_offset; |
| 108 | int period_len; |
| 109 | int buffer_len; |
| 110 | int periods; |
| 111 | }; |
| 112 | |
| 113 | struct fsi_master { |
| 114 | void __iomem *base; |
| 115 | int irq; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 116 | struct fsi_priv fsia; |
| 117 | struct fsi_priv fsib; |
| 118 | struct sh_fsi_platform_info *info; |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 119 | spinlock_t lock; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 120 | }; |
| 121 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 122 | /************************************************************************ |
| 123 | |
| 124 | |
| 125 | basic read write function |
| 126 | |
| 127 | |
| 128 | ************************************************************************/ |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 129 | static void __fsi_reg_write(u32 reg, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 130 | { |
| 131 | /* valid data area is 24bit */ |
| 132 | data &= 0x00ffffff; |
| 133 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 134 | __raw_writel(data, reg); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static u32 __fsi_reg_read(u32 reg) |
| 138 | { |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 139 | return __raw_readl(reg); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 140 | } |
| 141 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 142 | static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 143 | { |
| 144 | u32 val = __fsi_reg_read(reg); |
| 145 | |
| 146 | val &= ~mask; |
| 147 | val |= data & mask; |
| 148 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 149 | __fsi_reg_write(reg, val); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 150 | } |
| 151 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 152 | static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 153 | { |
| 154 | if (reg > REG_END) |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 155 | return; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 156 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 157 | __fsi_reg_write((u32)(fsi->base + reg), data); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg) |
| 161 | { |
| 162 | if (reg > REG_END) |
| 163 | return 0; |
| 164 | |
| 165 | return __fsi_reg_read((u32)(fsi->base + reg)); |
| 166 | } |
| 167 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 168 | static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 169 | { |
| 170 | if (reg > REG_END) |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 171 | return; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 172 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 173 | __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 174 | } |
| 175 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 176 | static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 177 | { |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 178 | unsigned long flags; |
| 179 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 180 | if ((reg < MREG_START) || |
| 181 | (reg > MREG_END)) |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 182 | return; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 183 | |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 184 | spin_lock_irqsave(&master->lock, flags); |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 185 | __fsi_reg_write((u32)(master->base + reg), data); |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 186 | spin_unlock_irqrestore(&master->lock, flags); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 187 | } |
| 188 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 189 | static u32 fsi_master_read(struct fsi_master *master, u32 reg) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 190 | { |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 191 | u32 ret; |
| 192 | unsigned long flags; |
| 193 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 194 | if ((reg < MREG_START) || |
| 195 | (reg > MREG_END)) |
| 196 | return 0; |
| 197 | |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 198 | spin_lock_irqsave(&master->lock, flags); |
| 199 | ret = __fsi_reg_read((u32)(master->base + reg)); |
| 200 | spin_unlock_irqrestore(&master->lock, flags); |
| 201 | |
| 202 | return ret; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 203 | } |
| 204 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 205 | static void fsi_master_mask_set(struct fsi_master *master, |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 206 | u32 reg, u32 mask, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 207 | { |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 208 | unsigned long flags; |
| 209 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 210 | if ((reg < MREG_START) || |
| 211 | (reg > MREG_END)) |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 212 | return; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 213 | |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 214 | spin_lock_irqsave(&master->lock, flags); |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 215 | __fsi_reg_mask_set((u32)(master->base + reg), mask, data); |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 216 | spin_unlock_irqrestore(&master->lock, flags); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /************************************************************************ |
| 220 | |
| 221 | |
| 222 | basic function |
| 223 | |
| 224 | |
| 225 | ************************************************************************/ |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 226 | static struct fsi_master *fsi_get_master(struct fsi_priv *fsi) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 227 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 228 | return fsi->master; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int fsi_is_port_a(struct fsi_priv *fsi) |
| 232 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 233 | return fsi->master->base == fsi->base; |
| 234 | } |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 235 | |
Kuninori Morimoto | 142e817 | 2009-12-28 14:09:11 +0900 | [diff] [blame] | 236 | 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] | 237 | { |
| 238 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 239 | struct snd_soc_dai_link *machine = rtd->dai; |
Kuninori Morimoto | 142e817 | 2009-12-28 14:09:11 +0900 | [diff] [blame] | 240 | |
| 241 | return machine->cpu_dai; |
| 242 | } |
| 243 | |
| 244 | static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream) |
| 245 | { |
| 246 | struct snd_soc_dai *dai = fsi_get_dai(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 247 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 248 | return dai->private_data; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static u32 fsi_get_info_flags(struct fsi_priv *fsi) |
| 252 | { |
| 253 | int is_porta = fsi_is_port_a(fsi); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 254 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 255 | |
| 256 | return is_porta ? master->info->porta_flags : |
| 257 | master->info->portb_flags; |
| 258 | } |
| 259 | |
| 260 | static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play) |
| 261 | { |
| 262 | u32 mode; |
| 263 | u32 flags = fsi_get_info_flags(fsi); |
| 264 | |
| 265 | mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE; |
| 266 | |
| 267 | /* return |
| 268 | * 1 : master mode |
| 269 | * 0 : slave mode |
| 270 | */ |
| 271 | |
| 272 | return (mode & flags) != mode; |
| 273 | } |
| 274 | |
| 275 | static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play) |
| 276 | { |
| 277 | int is_porta = fsi_is_port_a(fsi); |
| 278 | u32 data; |
| 279 | |
| 280 | if (is_porta) |
| 281 | data = is_play ? (1 << 0) : (1 << 4); |
| 282 | else |
| 283 | data = is_play ? (1 << 8) : (1 << 12); |
| 284 | |
| 285 | return data; |
| 286 | } |
| 287 | |
| 288 | static void fsi_stream_push(struct fsi_priv *fsi, |
| 289 | struct snd_pcm_substream *substream, |
| 290 | u32 buffer_len, |
| 291 | u32 period_len) |
| 292 | { |
| 293 | fsi->substream = substream; |
| 294 | fsi->buffer_len = buffer_len; |
| 295 | fsi->period_len = period_len; |
| 296 | fsi->byte_offset = 0; |
| 297 | fsi->periods = 0; |
| 298 | } |
| 299 | |
| 300 | static void fsi_stream_pop(struct fsi_priv *fsi) |
| 301 | { |
| 302 | fsi->substream = NULL; |
| 303 | fsi->buffer_len = 0; |
| 304 | fsi->period_len = 0; |
| 305 | fsi->byte_offset = 0; |
| 306 | fsi->periods = 0; |
| 307 | } |
| 308 | |
| 309 | static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play) |
| 310 | { |
| 311 | u32 status; |
| 312 | u32 reg = is_play ? DOFF_ST : DIFF_ST; |
| 313 | int residue; |
| 314 | |
| 315 | status = fsi_reg_read(fsi, reg); |
| 316 | residue = 0x1ff & (status >> 8); |
| 317 | residue *= fsi->chan; |
| 318 | |
| 319 | return residue; |
| 320 | } |
| 321 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 322 | /************************************************************************ |
| 323 | |
| 324 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame^] | 325 | irq function |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 326 | |
| 327 | |
| 328 | ************************************************************************/ |
| 329 | static void fsi_irq_enable(struct fsi_priv *fsi, int is_play) |
| 330 | { |
| 331 | u32 data = fsi_port_ab_io_bit(fsi, is_play); |
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 | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 334 | fsi_master_mask_set(master, IMSK, data, data); |
| 335 | fsi_master_mask_set(master, IEMSK, data, data); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static void fsi_irq_disable(struct fsi_priv *fsi, int is_play) |
| 339 | { |
| 340 | u32 data = fsi_port_ab_io_bit(fsi, is_play); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 341 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 342 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 343 | fsi_master_mask_set(master, IMSK, data, 0); |
| 344 | fsi_master_mask_set(master, IEMSK, data, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 345 | } |
| 346 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame^] | 347 | static u32 fsi_irq_get_status(struct fsi_master *master) |
| 348 | { |
| 349 | return fsi_master_read(master, INT_ST); |
| 350 | } |
| 351 | |
| 352 | static void fsi_irq_clear_all_status(struct fsi_master *master) |
| 353 | { |
| 354 | fsi_master_write(master, INT_ST, 0x0000000); |
| 355 | } |
| 356 | |
| 357 | static void fsi_irq_clear_status(struct fsi_priv *fsi) |
| 358 | { |
| 359 | u32 data = 0; |
| 360 | struct fsi_master *master = fsi_get_master(fsi); |
| 361 | |
| 362 | data |= fsi_port_ab_io_bit(fsi, 0); |
| 363 | data |= fsi_port_ab_io_bit(fsi, 1); |
| 364 | |
| 365 | /* clear interrupt factor */ |
| 366 | fsi_master_mask_set(master, INT_ST, data, 0); |
| 367 | } |
| 368 | |
| 369 | /************************************************************************ |
| 370 | |
| 371 | |
| 372 | ctrl function |
| 373 | |
| 374 | |
| 375 | ************************************************************************/ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 376 | static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable) |
| 377 | { |
| 378 | u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 379 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 380 | |
| 381 | if (enable) |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 382 | fsi_master_mask_set(master, CLK_RST, val, val); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 383 | else |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 384 | fsi_master_mask_set(master, CLK_RST, val, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 385 | } |
| 386 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame^] | 387 | static void fsi_fifo_init(struct fsi_priv *fsi, int is_play) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 388 | { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 389 | u32 ctrl; |
| 390 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 391 | ctrl = is_play ? DOFF_CTL : DIFF_CTL; |
| 392 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 393 | /* set interrupt generation factor */ |
| 394 | fsi_reg_write(fsi, ctrl, IRQ_HALF); |
| 395 | |
| 396 | /* clear FIFO */ |
| 397 | fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 398 | } |
| 399 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 400 | static void fsi_soft_all_reset(struct fsi_master *master) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 401 | { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 402 | /* port AB reset */ |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 403 | fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 404 | mdelay(10); |
| 405 | |
| 406 | /* soft reset */ |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 407 | fsi_master_mask_set(master, SOFT_RST, FSISR, 0); |
| 408 | fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 409 | mdelay(10); |
| 410 | } |
| 411 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 412 | /* playback interrupt */ |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 413 | static int fsi_data_push(struct fsi_priv *fsi, int startup) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 414 | { |
| 415 | struct snd_pcm_runtime *runtime; |
| 416 | struct snd_pcm_substream *substream = NULL; |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 417 | u32 status; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 418 | int send; |
| 419 | int fifo_free; |
| 420 | int width; |
Kuninori Morimoto | 9ddc9aa | 2009-10-30 12:02:39 +0900 | [diff] [blame] | 421 | u8 *start; |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 422 | int i, over_period; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 423 | |
| 424 | if (!fsi || |
| 425 | !fsi->substream || |
| 426 | !fsi->substream->runtime) |
| 427 | return -EINVAL; |
| 428 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 429 | over_period = 0; |
| 430 | substream = fsi->substream; |
| 431 | runtime = substream->runtime; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 432 | |
| 433 | /* FSI FIFO has limit. |
| 434 | * So, this driver can not send periods data at a time |
| 435 | */ |
| 436 | if (fsi->byte_offset >= |
| 437 | fsi->period_len * (fsi->periods + 1)) { |
| 438 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 439 | over_period = 1; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 440 | fsi->periods = (fsi->periods + 1) % runtime->periods; |
| 441 | |
| 442 | if (0 == fsi->periods) |
| 443 | fsi->byte_offset = 0; |
| 444 | } |
| 445 | |
| 446 | /* get 1 channel data width */ |
| 447 | width = frames_to_bytes(runtime, 1) / fsi->chan; |
| 448 | |
| 449 | /* get send size for alsa */ |
| 450 | send = (fsi->buffer_len - fsi->byte_offset) / width; |
| 451 | |
| 452 | /* get FIFO free size */ |
| 453 | fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1); |
| 454 | |
| 455 | /* size check */ |
| 456 | if (fifo_free < send) |
| 457 | send = fifo_free; |
| 458 | |
Kuninori Morimoto | 9ddc9aa | 2009-10-30 12:02:39 +0900 | [diff] [blame] | 459 | start = runtime->dma_area; |
| 460 | start += fsi->byte_offset; |
| 461 | |
| 462 | switch (width) { |
| 463 | case 2: |
| 464 | for (i = 0; i < send; i++) |
| 465 | fsi_reg_write(fsi, DODT, |
| 466 | ((u32)*((u16 *)start + i) << 8)); |
| 467 | break; |
| 468 | case 4: |
| 469 | for (i = 0; i < send; i++) |
| 470 | fsi_reg_write(fsi, DODT, *((u32 *)start + i)); |
| 471 | break; |
| 472 | default: |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 473 | return -EINVAL; |
Kuninori Morimoto | 9ddc9aa | 2009-10-30 12:02:39 +0900 | [diff] [blame] | 474 | } |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 475 | |
| 476 | fsi->byte_offset += send * width; |
| 477 | |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 478 | status = fsi_reg_read(fsi, DOFF_ST); |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 479 | if (!startup) { |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 480 | struct snd_soc_dai *dai = fsi_get_dai(substream); |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 481 | |
| 482 | if (status & ERR_OVER) |
| 483 | dev_err(dai->dev, "over run\n"); |
| 484 | if (status & ERR_UNDER) |
| 485 | dev_err(dai->dev, "under run\n"); |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 486 | } |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 487 | fsi_reg_write(fsi, DOFF_ST, 0); |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 488 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 489 | fsi_irq_enable(fsi, 1); |
| 490 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 491 | if (over_period) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 492 | snd_pcm_period_elapsed(substream); |
| 493 | |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 494 | return 0; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 495 | } |
| 496 | |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 497 | static int fsi_data_pop(struct fsi_priv *fsi, int startup) |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 498 | { |
| 499 | struct snd_pcm_runtime *runtime; |
| 500 | struct snd_pcm_substream *substream = NULL; |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 501 | u32 status; |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 502 | int free; |
| 503 | int fifo_fill; |
| 504 | int width; |
| 505 | u8 *start; |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 506 | int i, over_period; |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 507 | |
| 508 | if (!fsi || |
| 509 | !fsi->substream || |
| 510 | !fsi->substream->runtime) |
| 511 | return -EINVAL; |
| 512 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 513 | over_period = 0; |
| 514 | substream = fsi->substream; |
| 515 | runtime = substream->runtime; |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 516 | |
| 517 | /* FSI FIFO has limit. |
| 518 | * So, this driver can not send periods data at a time |
| 519 | */ |
| 520 | if (fsi->byte_offset >= |
| 521 | fsi->period_len * (fsi->periods + 1)) { |
| 522 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 523 | over_period = 1; |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 524 | fsi->periods = (fsi->periods + 1) % runtime->periods; |
| 525 | |
| 526 | if (0 == fsi->periods) |
| 527 | fsi->byte_offset = 0; |
| 528 | } |
| 529 | |
| 530 | /* get 1 channel data width */ |
| 531 | width = frames_to_bytes(runtime, 1) / fsi->chan; |
| 532 | |
| 533 | /* get free space for alsa */ |
| 534 | free = (fsi->buffer_len - fsi->byte_offset) / width; |
| 535 | |
| 536 | /* get recv size */ |
| 537 | fifo_fill = fsi_get_fifo_residue(fsi, 0); |
| 538 | |
| 539 | if (free < fifo_fill) |
| 540 | fifo_fill = free; |
| 541 | |
| 542 | start = runtime->dma_area; |
| 543 | start += fsi->byte_offset; |
| 544 | |
| 545 | switch (width) { |
| 546 | case 2: |
| 547 | for (i = 0; i < fifo_fill; i++) |
| 548 | *((u16 *)start + i) = |
| 549 | (u16)(fsi_reg_read(fsi, DIDT) >> 8); |
| 550 | break; |
| 551 | case 4: |
| 552 | for (i = 0; i < fifo_fill; i++) |
| 553 | *((u32 *)start + i) = fsi_reg_read(fsi, DIDT); |
| 554 | break; |
| 555 | default: |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | fsi->byte_offset += fifo_fill * width; |
| 560 | |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 561 | status = fsi_reg_read(fsi, DIFF_ST); |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 562 | if (!startup) { |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 563 | struct snd_soc_dai *dai = fsi_get_dai(substream); |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 564 | |
| 565 | if (status & ERR_OVER) |
| 566 | dev_err(dai->dev, "over run\n"); |
| 567 | if (status & ERR_UNDER) |
| 568 | dev_err(dai->dev, "under run\n"); |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 569 | } |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 570 | fsi_reg_write(fsi, DIFF_ST, 0); |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 571 | |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 572 | fsi_irq_enable(fsi, 0); |
| 573 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 574 | if (over_period) |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 575 | snd_pcm_period_elapsed(substream); |
| 576 | |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 577 | return 0; |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 578 | } |
| 579 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 580 | static irqreturn_t fsi_interrupt(int irq, void *data) |
| 581 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 582 | struct fsi_master *master = data; |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame^] | 583 | u32 int_st = fsi_irq_get_status(master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 584 | |
| 585 | /* clear irq status */ |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 586 | fsi_master_mask_set(master, SOFT_RST, IR, 0); |
| 587 | fsi_master_mask_set(master, SOFT_RST, IR, IR); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 588 | |
| 589 | if (int_st & INT_A_OUT) |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 590 | fsi_data_push(&master->fsia, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 591 | if (int_st & INT_B_OUT) |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 592 | fsi_data_push(&master->fsib, 0); |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 593 | if (int_st & INT_A_IN) |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 594 | fsi_data_pop(&master->fsia, 0); |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 595 | if (int_st & INT_B_IN) |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 596 | fsi_data_pop(&master->fsib, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 597 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame^] | 598 | fsi_irq_clear_all_status(master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 599 | |
| 600 | return IRQ_HANDLED; |
| 601 | } |
| 602 | |
| 603 | /************************************************************************ |
| 604 | |
| 605 | |
| 606 | dai ops |
| 607 | |
| 608 | |
| 609 | ************************************************************************/ |
| 610 | static int fsi_dai_startup(struct snd_pcm_substream *substream, |
| 611 | struct snd_soc_dai *dai) |
| 612 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 613 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 614 | const char *msg; |
| 615 | u32 flags = fsi_get_info_flags(fsi); |
| 616 | u32 fmt; |
| 617 | u32 reg; |
| 618 | u32 data; |
| 619 | int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 620 | int is_master; |
| 621 | int ret = 0; |
| 622 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 623 | pm_runtime_get_sync(dai->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 624 | |
| 625 | /* CKG1 */ |
| 626 | data = is_play ? (1 << 0) : (1 << 4); |
| 627 | is_master = fsi_is_master_mode(fsi, is_play); |
| 628 | if (is_master) |
| 629 | fsi_reg_mask_set(fsi, CKG1, data, data); |
| 630 | else |
| 631 | fsi_reg_mask_set(fsi, CKG1, data, 0); |
| 632 | |
| 633 | /* clock inversion (CKG2) */ |
| 634 | data = 0; |
| 635 | switch (SH_FSI_INVERSION_MASK & flags) { |
| 636 | case SH_FSI_LRM_INV: |
| 637 | data = 1 << 12; |
| 638 | break; |
| 639 | case SH_FSI_BRM_INV: |
| 640 | data = 1 << 8; |
| 641 | break; |
| 642 | case SH_FSI_LRS_INV: |
| 643 | data = 1 << 4; |
| 644 | break; |
| 645 | case SH_FSI_BRS_INV: |
| 646 | data = 1 << 0; |
| 647 | break; |
| 648 | } |
| 649 | fsi_reg_write(fsi, CKG2, data); |
| 650 | |
| 651 | /* do fmt, di fmt */ |
| 652 | data = 0; |
| 653 | reg = is_play ? DO_FMT : DI_FMT; |
| 654 | fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags); |
| 655 | switch (fmt) { |
| 656 | case SH_FSI_FMT_MONO: |
| 657 | msg = "MONO"; |
| 658 | data = CR_FMT(CR_MONO); |
| 659 | fsi->chan = 1; |
| 660 | break; |
| 661 | case SH_FSI_FMT_MONO_DELAY: |
| 662 | msg = "MONO Delay"; |
| 663 | data = CR_FMT(CR_MONO_D); |
| 664 | fsi->chan = 1; |
| 665 | break; |
| 666 | case SH_FSI_FMT_PCM: |
| 667 | msg = "PCM"; |
| 668 | data = CR_FMT(CR_PCM); |
| 669 | fsi->chan = 2; |
| 670 | break; |
| 671 | case SH_FSI_FMT_I2S: |
| 672 | msg = "I2S"; |
| 673 | data = CR_FMT(CR_I2S); |
| 674 | fsi->chan = 2; |
| 675 | break; |
| 676 | case SH_FSI_FMT_TDM: |
| 677 | msg = "TDM"; |
| 678 | data = CR_FMT(CR_TDM) | (fsi->chan - 1); |
| 679 | fsi->chan = is_play ? |
| 680 | SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags); |
| 681 | break; |
| 682 | case SH_FSI_FMT_TDM_DELAY: |
| 683 | msg = "TDM Delay"; |
| 684 | data = CR_FMT(CR_TDM_D) | (fsi->chan - 1); |
| 685 | fsi->chan = is_play ? |
| 686 | SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags); |
| 687 | break; |
| 688 | default: |
| 689 | dev_err(dai->dev, "unknown format.\n"); |
| 690 | return -EINVAL; |
| 691 | } |
| 692 | |
| 693 | switch (fsi->chan) { |
| 694 | case 1: |
| 695 | fsi->fifo_max = 256; |
| 696 | break; |
| 697 | case 2: |
| 698 | fsi->fifo_max = 128; |
| 699 | break; |
| 700 | case 3: |
| 701 | case 4: |
| 702 | fsi->fifo_max = 64; |
| 703 | break; |
| 704 | case 5: |
| 705 | case 6: |
| 706 | case 7: |
| 707 | case 8: |
| 708 | fsi->fifo_max = 32; |
| 709 | break; |
| 710 | default: |
| 711 | dev_err(dai->dev, "channel size error.\n"); |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | |
| 715 | fsi_reg_write(fsi, reg, data); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 716 | |
| 717 | /* |
| 718 | * clear clk reset if master mode |
| 719 | */ |
| 720 | if (is_master) |
| 721 | fsi_clk_ctrl(fsi, 1); |
| 722 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame^] | 723 | /* irq clear */ |
| 724 | fsi_irq_disable(fsi, is_play); |
| 725 | fsi_irq_clear_status(fsi); |
| 726 | |
| 727 | /* fifo init */ |
| 728 | fsi_fifo_init(fsi, is_play); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 729 | |
| 730 | return ret; |
| 731 | } |
| 732 | |
| 733 | static void fsi_dai_shutdown(struct snd_pcm_substream *substream, |
| 734 | struct snd_soc_dai *dai) |
| 735 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 736 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 737 | int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 738 | |
| 739 | fsi_irq_disable(fsi, is_play); |
| 740 | fsi_clk_ctrl(fsi, 0); |
| 741 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 742 | pm_runtime_put_sync(dai->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd, |
| 746 | struct snd_soc_dai *dai) |
| 747 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 748 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 749 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 750 | int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 751 | int ret = 0; |
| 752 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 753 | switch (cmd) { |
| 754 | case SNDRV_PCM_TRIGGER_START: |
| 755 | fsi_stream_push(fsi, substream, |
| 756 | frames_to_bytes(runtime, runtime->buffer_size), |
| 757 | frames_to_bytes(runtime, runtime->period_size)); |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 758 | ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 759 | break; |
| 760 | case SNDRV_PCM_TRIGGER_STOP: |
| 761 | fsi_irq_disable(fsi, is_play); |
| 762 | fsi_stream_pop(fsi); |
| 763 | break; |
| 764 | } |
| 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | static struct snd_soc_dai_ops fsi_dai_ops = { |
| 770 | .startup = fsi_dai_startup, |
| 771 | .shutdown = fsi_dai_shutdown, |
| 772 | .trigger = fsi_dai_trigger, |
| 773 | }; |
| 774 | |
| 775 | /************************************************************************ |
| 776 | |
| 777 | |
| 778 | pcm ops |
| 779 | |
| 780 | |
| 781 | ************************************************************************/ |
| 782 | static struct snd_pcm_hardware fsi_pcm_hardware = { |
| 783 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 784 | SNDRV_PCM_INFO_MMAP | |
| 785 | SNDRV_PCM_INFO_MMAP_VALID | |
| 786 | SNDRV_PCM_INFO_PAUSE, |
| 787 | .formats = FSI_FMTS, |
| 788 | .rates = FSI_RATES, |
| 789 | .rate_min = 8000, |
| 790 | .rate_max = 192000, |
| 791 | .channels_min = 1, |
| 792 | .channels_max = 2, |
| 793 | .buffer_bytes_max = 64 * 1024, |
| 794 | .period_bytes_min = 32, |
| 795 | .period_bytes_max = 8192, |
| 796 | .periods_min = 1, |
| 797 | .periods_max = 32, |
| 798 | .fifo_size = 256, |
| 799 | }; |
| 800 | |
| 801 | static int fsi_pcm_open(struct snd_pcm_substream *substream) |
| 802 | { |
| 803 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 804 | int ret = 0; |
| 805 | |
| 806 | snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware); |
| 807 | |
| 808 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 809 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 810 | |
| 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | static int fsi_hw_params(struct snd_pcm_substream *substream, |
| 815 | struct snd_pcm_hw_params *hw_params) |
| 816 | { |
| 817 | return snd_pcm_lib_malloc_pages(substream, |
| 818 | params_buffer_bytes(hw_params)); |
| 819 | } |
| 820 | |
| 821 | static int fsi_hw_free(struct snd_pcm_substream *substream) |
| 822 | { |
| 823 | return snd_pcm_lib_free_pages(substream); |
| 824 | } |
| 825 | |
| 826 | static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream) |
| 827 | { |
| 828 | struct snd_pcm_runtime *runtime = substream->runtime; |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 829 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 830 | long location; |
| 831 | |
Kuninori Morimoto | 9ddc9aa | 2009-10-30 12:02:39 +0900 | [diff] [blame] | 832 | location = (fsi->byte_offset - 1); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 833 | if (location < 0) |
| 834 | location = 0; |
| 835 | |
| 836 | return bytes_to_frames(runtime, location); |
| 837 | } |
| 838 | |
| 839 | static struct snd_pcm_ops fsi_pcm_ops = { |
| 840 | .open = fsi_pcm_open, |
| 841 | .ioctl = snd_pcm_lib_ioctl, |
| 842 | .hw_params = fsi_hw_params, |
| 843 | .hw_free = fsi_hw_free, |
| 844 | .pointer = fsi_pointer, |
| 845 | }; |
| 846 | |
| 847 | /************************************************************************ |
| 848 | |
| 849 | |
| 850 | snd_soc_platform |
| 851 | |
| 852 | |
| 853 | ************************************************************************/ |
| 854 | #define PREALLOC_BUFFER (32 * 1024) |
| 855 | #define PREALLOC_BUFFER_MAX (32 * 1024) |
| 856 | |
| 857 | static void fsi_pcm_free(struct snd_pcm *pcm) |
| 858 | { |
| 859 | snd_pcm_lib_preallocate_free_for_all(pcm); |
| 860 | } |
| 861 | |
| 862 | static int fsi_pcm_new(struct snd_card *card, |
| 863 | struct snd_soc_dai *dai, |
| 864 | struct snd_pcm *pcm) |
| 865 | { |
| 866 | /* |
| 867 | * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel |
| 868 | * in MMAP mode (i.e. aplay -M) |
| 869 | */ |
| 870 | return snd_pcm_lib_preallocate_pages_for_all( |
| 871 | pcm, |
| 872 | SNDRV_DMA_TYPE_CONTINUOUS, |
| 873 | snd_dma_continuous_data(GFP_KERNEL), |
| 874 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); |
| 875 | } |
| 876 | |
| 877 | /************************************************************************ |
| 878 | |
| 879 | |
| 880 | alsa struct |
| 881 | |
| 882 | |
| 883 | ************************************************************************/ |
| 884 | struct snd_soc_dai fsi_soc_dai[] = { |
| 885 | { |
| 886 | .name = "FSIA", |
| 887 | .id = 0, |
| 888 | .playback = { |
| 889 | .rates = FSI_RATES, |
| 890 | .formats = FSI_FMTS, |
| 891 | .channels_min = 1, |
| 892 | .channels_max = 8, |
| 893 | }, |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 894 | .capture = { |
| 895 | .rates = FSI_RATES, |
| 896 | .formats = FSI_FMTS, |
| 897 | .channels_min = 1, |
| 898 | .channels_max = 8, |
| 899 | }, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 900 | .ops = &fsi_dai_ops, |
| 901 | }, |
| 902 | { |
| 903 | .name = "FSIB", |
| 904 | .id = 1, |
| 905 | .playback = { |
| 906 | .rates = FSI_RATES, |
| 907 | .formats = FSI_FMTS, |
| 908 | .channels_min = 1, |
| 909 | .channels_max = 8, |
| 910 | }, |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 911 | .capture = { |
| 912 | .rates = FSI_RATES, |
| 913 | .formats = FSI_FMTS, |
| 914 | .channels_min = 1, |
| 915 | .channels_max = 8, |
| 916 | }, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 917 | .ops = &fsi_dai_ops, |
| 918 | }, |
| 919 | }; |
| 920 | EXPORT_SYMBOL_GPL(fsi_soc_dai); |
| 921 | |
| 922 | struct snd_soc_platform fsi_soc_platform = { |
| 923 | .name = "fsi-pcm", |
| 924 | .pcm_ops = &fsi_pcm_ops, |
| 925 | .pcm_new = fsi_pcm_new, |
| 926 | .pcm_free = fsi_pcm_free, |
| 927 | }; |
| 928 | EXPORT_SYMBOL_GPL(fsi_soc_platform); |
| 929 | |
| 930 | /************************************************************************ |
| 931 | |
| 932 | |
| 933 | platform function |
| 934 | |
| 935 | |
| 936 | ************************************************************************/ |
| 937 | static int fsi_probe(struct platform_device *pdev) |
| 938 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 939 | struct fsi_master *master; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 940 | struct resource *res; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 941 | unsigned int irq; |
| 942 | int ret; |
| 943 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 944 | if (0 != pdev->id) { |
| 945 | dev_err(&pdev->dev, "current fsi support id 0 only now\n"); |
| 946 | return -ENODEV; |
| 947 | } |
| 948 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 949 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 950 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | b6aa179 | 2009-12-16 17:10:09 +0100 | [diff] [blame] | 951 | if (!res || (int)irq <= 0) { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 952 | dev_err(&pdev->dev, "Not enough FSI platform resources.\n"); |
| 953 | ret = -ENODEV; |
| 954 | goto exit; |
| 955 | } |
| 956 | |
| 957 | master = kzalloc(sizeof(*master), GFP_KERNEL); |
| 958 | if (!master) { |
| 959 | dev_err(&pdev->dev, "Could not allocate master\n"); |
| 960 | ret = -ENOMEM; |
| 961 | goto exit; |
| 962 | } |
| 963 | |
| 964 | master->base = ioremap_nocache(res->start, resource_size(res)); |
| 965 | if (!master->base) { |
| 966 | ret = -ENXIO; |
| 967 | dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n"); |
| 968 | goto exit_kfree; |
| 969 | } |
| 970 | |
| 971 | master->irq = irq; |
| 972 | master->info = pdev->dev.platform_data; |
| 973 | master->fsia.base = master->base; |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 974 | master->fsia.master = master; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 975 | master->fsib.base = master->base + 0x40; |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 976 | master->fsib.master = master; |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 977 | spin_lock_init(&master->lock); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 978 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 979 | pm_runtime_enable(&pdev->dev); |
| 980 | pm_runtime_resume(&pdev->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 981 | |
| 982 | fsi_soc_dai[0].dev = &pdev->dev; |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 983 | fsi_soc_dai[0].private_data = &master->fsia; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 984 | fsi_soc_dai[1].dev = &pdev->dev; |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 985 | fsi_soc_dai[1].private_data = &master->fsib; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 986 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 987 | fsi_soft_all_reset(master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 988 | |
| 989 | ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, "fsi", master); |
| 990 | if (ret) { |
| 991 | dev_err(&pdev->dev, "irq request err\n"); |
Kuninori Morimoto | 9ddc9aa | 2009-10-30 12:02:39 +0900 | [diff] [blame] | 992 | goto exit_iounmap; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | ret = snd_soc_register_platform(&fsi_soc_platform); |
| 996 | if (ret < 0) { |
| 997 | dev_err(&pdev->dev, "cannot snd soc register\n"); |
| 998 | goto exit_free_irq; |
| 999 | } |
| 1000 | |
| 1001 | return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai)); |
| 1002 | |
| 1003 | exit_free_irq: |
| 1004 | free_irq(irq, master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1005 | exit_iounmap: |
| 1006 | iounmap(master->base); |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1007 | pm_runtime_disable(&pdev->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1008 | exit_kfree: |
| 1009 | kfree(master); |
| 1010 | master = NULL; |
| 1011 | exit: |
| 1012 | return ret; |
| 1013 | } |
| 1014 | |
| 1015 | static int fsi_remove(struct platform_device *pdev) |
| 1016 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1017 | struct fsi_master *master; |
| 1018 | |
| 1019 | master = fsi_get_master(fsi_soc_dai[0].private_data); |
| 1020 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1021 | snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai)); |
| 1022 | snd_soc_unregister_platform(&fsi_soc_platform); |
| 1023 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1024 | pm_runtime_disable(&pdev->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1025 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1026 | free_irq(master->irq, master); |
| 1027 | |
| 1028 | iounmap(master->base); |
| 1029 | kfree(master); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1030 | |
| 1031 | fsi_soc_dai[0].dev = NULL; |
| 1032 | fsi_soc_dai[0].private_data = NULL; |
| 1033 | fsi_soc_dai[1].dev = NULL; |
| 1034 | fsi_soc_dai[1].private_data = NULL; |
| 1035 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1039 | static int fsi_runtime_nop(struct device *dev) |
| 1040 | { |
| 1041 | /* Runtime PM callback shared between ->runtime_suspend() |
| 1042 | * and ->runtime_resume(). Simply returns success. |
| 1043 | * |
| 1044 | * This driver re-initializes all registers after |
| 1045 | * pm_runtime_get_sync() anyway so there is no need |
| 1046 | * to save and restore registers here. |
| 1047 | */ |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | static struct dev_pm_ops fsi_pm_ops = { |
| 1052 | .runtime_suspend = fsi_runtime_nop, |
| 1053 | .runtime_resume = fsi_runtime_nop, |
| 1054 | }; |
| 1055 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1056 | static struct platform_driver fsi_driver = { |
| 1057 | .driver = { |
| 1058 | .name = "sh_fsi", |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1059 | .pm = &fsi_pm_ops, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1060 | }, |
| 1061 | .probe = fsi_probe, |
| 1062 | .remove = fsi_remove, |
| 1063 | }; |
| 1064 | |
| 1065 | static int __init fsi_mobile_init(void) |
| 1066 | { |
| 1067 | return platform_driver_register(&fsi_driver); |
| 1068 | } |
| 1069 | |
| 1070 | static void __exit fsi_mobile_exit(void) |
| 1071 | { |
| 1072 | platform_driver_unregister(&fsi_driver); |
| 1073 | } |
| 1074 | module_init(fsi_mobile_init); |
| 1075 | module_exit(fsi_mobile_exit); |
| 1076 | |
| 1077 | MODULE_LICENSE("GPL"); |
| 1078 | MODULE_DESCRIPTION("SuperH onchip FSI audio driver"); |
| 1079 | MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>"); |