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> |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 19 | #include <sound/soc.h> |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 20 | #include <sound/sh_fsi.h> |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 21 | |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 22 | /* PortA/PortB register */ |
| 23 | #define REG_DO_FMT 0x0000 |
| 24 | #define REG_DOFF_CTL 0x0004 |
| 25 | #define REG_DOFF_ST 0x0008 |
| 26 | #define REG_DI_FMT 0x000C |
| 27 | #define REG_DIFF_CTL 0x0010 |
| 28 | #define REG_DIFF_ST 0x0014 |
| 29 | #define REG_CKG1 0x0018 |
| 30 | #define REG_CKG2 0x001C |
| 31 | #define REG_DIDT 0x0020 |
| 32 | #define REG_DODT 0x0024 |
| 33 | #define REG_MUTE_ST 0x0028 |
| 34 | #define REG_OUT_SEL 0x0030 |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 35 | |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 36 | /* master register */ |
| 37 | #define MST_CLK_RST 0x0210 |
| 38 | #define MST_SOFT_RST 0x0214 |
| 39 | #define MST_FIFO_SZ 0x0218 |
| 40 | |
| 41 | /* core register (depend on FSI version) */ |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 42 | #define A_MST_CTLR 0x0180 |
| 43 | #define B_MST_CTLR 0x01A0 |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 44 | #define CPU_INT_ST 0x01F4 |
| 45 | #define CPU_IEMSK 0x01F8 |
| 46 | #define CPU_IMSK 0x01FC |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 47 | #define INT_ST 0x0200 |
| 48 | #define IEMSK 0x0204 |
| 49 | #define IMSK 0x0208 |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 50 | |
| 51 | /* DO_FMT */ |
| 52 | /* DI_FMT */ |
Kuninori Morimoto | f7d711e | 2010-12-03 17:36:24 +0900 | [diff] [blame] | 53 | #define CR_BWS_24 (0x0 << 20) /* FSI2 */ |
| 54 | #define CR_BWS_16 (0x1 << 20) /* FSI2 */ |
| 55 | #define CR_BWS_20 (0x2 << 20) /* FSI2 */ |
| 56 | |
| 57 | #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */ |
| 58 | #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */ |
| 59 | #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */ |
| 60 | |
Kuninori Morimoto | a7ffb52 | 2010-07-13 12:13:00 +0900 | [diff] [blame] | 61 | #define CR_MONO (0x0 << 4) |
| 62 | #define CR_MONO_D (0x1 << 4) |
| 63 | #define CR_PCM (0x2 << 4) |
| 64 | #define CR_I2S (0x3 << 4) |
| 65 | #define CR_TDM (0x4 << 4) |
| 66 | #define CR_TDM_D (0x5 << 4) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 67 | |
| 68 | /* DOFF_CTL */ |
| 69 | /* DIFF_CTL */ |
| 70 | #define IRQ_HALF 0x00100000 |
| 71 | #define FIFO_CLR 0x00000001 |
| 72 | |
| 73 | /* DOFF_ST */ |
| 74 | #define ERR_OVER 0x00000010 |
| 75 | #define ERR_UNDER 0x00000001 |
Kuninori Morimoto | 59c3b00 | 2009-12-28 14:09:16 +0900 | [diff] [blame] | 76 | #define ST_ERR (ERR_OVER | ERR_UNDER) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 77 | |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 78 | /* CKG1 */ |
| 79 | #define ACKMD_MASK 0x00007000 |
| 80 | #define BPFMD_MASK 0x00000700 |
Kuninori Morimoto | 4d805f7 | 2011-01-20 11:46:02 +0900 | [diff] [blame] | 81 | #define DIMD (1 << 4) |
| 82 | #define DOMD (1 << 0) |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 83 | |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 84 | /* A/B MST_CTLR */ |
| 85 | #define BP (1 << 4) /* Fix the signal of Biphase output */ |
| 86 | #define SE (1 << 0) /* Fix the master clock */ |
| 87 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 88 | /* CLK_RST */ |
| 89 | #define B_CLK 0x00000010 |
| 90 | #define A_CLK 0x00000001 |
| 91 | |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 92 | /* IO SHIFT / MACRO */ |
| 93 | #define BI_SHIFT 12 |
| 94 | #define BO_SHIFT 8 |
| 95 | #define AI_SHIFT 4 |
| 96 | #define AO_SHIFT 0 |
| 97 | #define AB_IO(param, shift) (param << shift) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 98 | |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 99 | /* SOFT_RST */ |
| 100 | #define PBSR (1 << 12) /* Port B Software Reset */ |
| 101 | #define PASR (1 << 8) /* Port A Software Reset */ |
| 102 | #define IR (1 << 4) /* Interrupt Reset */ |
| 103 | #define FSISR (1 << 0) /* Software Reset */ |
| 104 | |
Kuninori Morimoto | f7d711e | 2010-12-03 17:36:24 +0900 | [diff] [blame] | 105 | /* OUT_SEL (FSI2) */ |
| 106 | #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */ |
| 107 | /* 1: Biphase and serial */ |
| 108 | |
Kuninori Morimoto | 4a942b4 | 2010-03-25 19:15:51 +0900 | [diff] [blame] | 109 | /* FIFO_SZ */ |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 110 | #define FIFO_SZ_MASK 0x7 |
Kuninori Morimoto | 4a942b4 | 2010-03-25 19:15:51 +0900 | [diff] [blame] | 111 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 112 | #define FSI_RATES SNDRV_PCM_RATE_8000_96000 |
| 113 | |
| 114 | #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE) |
| 115 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 116 | /* |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 117 | * FSI driver use below type name for variable |
| 118 | * |
| 119 | * xxx_len : data length |
| 120 | * xxx_width : data width |
| 121 | * xxx_offset : data offset |
| 122 | * xxx_num : number of data |
| 123 | */ |
| 124 | |
| 125 | /* |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 126 | * struct |
| 127 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 128 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 129 | struct fsi_stream { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 130 | struct snd_pcm_substream *substream; |
| 131 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 132 | int fifo_max_num; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 133 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 134 | int buff_offset; |
| 135 | int buff_len; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 136 | int period_len; |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 137 | int period_num; |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 138 | |
| 139 | int uerr_num; |
| 140 | int oerr_num; |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | struct fsi_priv { |
| 144 | void __iomem *base; |
| 145 | struct fsi_master *master; |
| 146 | |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 147 | int chan_num; |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 148 | struct fsi_stream playback; |
| 149 | struct fsi_stream capture; |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 150 | |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 151 | long rate; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 152 | }; |
| 153 | |
Kuninori Morimoto | 73b92c1 | 2010-07-13 12:13:04 +0900 | [diff] [blame] | 154 | struct fsi_core { |
| 155 | int ver; |
| 156 | |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 157 | u32 int_st; |
| 158 | u32 iemsk; |
| 159 | u32 imsk; |
Kuninori Morimoto | 2b0e730 | 2010-12-03 17:37:44 +0900 | [diff] [blame] | 160 | u32 a_mclk; |
| 161 | u32 b_mclk; |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 162 | }; |
| 163 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 164 | struct fsi_master { |
| 165 | void __iomem *base; |
| 166 | int irq; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 167 | struct fsi_priv fsia; |
| 168 | struct fsi_priv fsib; |
Kuninori Morimoto | 73b92c1 | 2010-07-13 12:13:04 +0900 | [diff] [blame] | 169 | struct fsi_core *core; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 170 | struct sh_fsi_platform_info *info; |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 171 | spinlock_t lock; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 172 | }; |
| 173 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 174 | /* |
| 175 | * basic read write function |
| 176 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 177 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 178 | static void __fsi_reg_write(u32 reg, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 179 | { |
| 180 | /* valid data area is 24bit */ |
| 181 | data &= 0x00ffffff; |
| 182 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 183 | __raw_writel(data, reg); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static u32 __fsi_reg_read(u32 reg) |
| 187 | { |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 188 | return __raw_readl(reg); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 189 | } |
| 190 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 191 | static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 192 | { |
| 193 | u32 val = __fsi_reg_read(reg); |
| 194 | |
| 195 | val &= ~mask; |
| 196 | val |= data & mask; |
| 197 | |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 198 | __fsi_reg_write(reg, val); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 199 | } |
| 200 | |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 201 | #define fsi_reg_write(p, r, d)\ |
| 202 | __fsi_reg_write((u32)(p->base + REG_##r), d) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 203 | |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 204 | #define fsi_reg_read(p, r)\ |
| 205 | __fsi_reg_read((u32)(p->base + REG_##r)) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 206 | |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 207 | #define fsi_reg_mask_set(p, r, m, d)\ |
| 208 | __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 209 | |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 210 | #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r) |
| 211 | #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r) |
| 212 | static u32 _fsi_master_read(struct fsi_master *master, u32 reg) |
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 | u32 ret; |
| 215 | unsigned long flags; |
| 216 | |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 217 | spin_lock_irqsave(&master->lock, flags); |
| 218 | ret = __fsi_reg_read((u32)(master->base + reg)); |
| 219 | spin_unlock_irqrestore(&master->lock, flags); |
| 220 | |
| 221 | return ret; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 224 | #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d) |
| 225 | #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d) |
| 226 | static void _fsi_master_mask_set(struct fsi_master *master, |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 227 | u32 reg, u32 mask, u32 data) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 228 | { |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 229 | unsigned long flags; |
| 230 | |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 231 | spin_lock_irqsave(&master->lock, flags); |
Guennadi Liakhovetski | 0f69d97 | 2010-02-03 17:37:23 +0100 | [diff] [blame] | 232 | __fsi_reg_mask_set((u32)(master->base + reg), mask, data); |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 233 | spin_unlock_irqrestore(&master->lock, flags); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 234 | } |
| 235 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 236 | /* |
| 237 | * basic function |
| 238 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 239 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 240 | static struct fsi_master *fsi_get_master(struct fsi_priv *fsi) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 241 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 242 | return fsi->master; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static int fsi_is_port_a(struct fsi_priv *fsi) |
| 246 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 247 | return fsi->master->base == fsi->base; |
| 248 | } |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 249 | |
Kuninori Morimoto | 142e817 | 2009-12-28 14:09:11 +0900 | [diff] [blame] | 250 | 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] | 251 | { |
| 252 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 142e817 | 2009-12-28 14:09:11 +0900 | [diff] [blame] | 253 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 254 | return rtd->cpu_dai; |
Kuninori Morimoto | 142e817 | 2009-12-28 14:09:11 +0900 | [diff] [blame] | 255 | } |
| 256 | |
Kuninori Morimoto | 0d032c1 | 2011-01-20 11:45:51 +0900 | [diff] [blame] | 257 | 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] | 258 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 259 | struct fsi_master *master = snd_soc_dai_get_drvdata(dai); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 260 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 261 | if (dai->id == 0) |
| 262 | return &master->fsia; |
| 263 | else |
| 264 | return &master->fsib; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 265 | } |
| 266 | |
Kuninori Morimoto | 0d032c1 | 2011-01-20 11:45:51 +0900 | [diff] [blame] | 267 | static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream) |
| 268 | { |
| 269 | return fsi_get_priv_frm_dai(fsi_get_dai(substream)); |
| 270 | } |
| 271 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 272 | static u32 fsi_get_info_flags(struct fsi_priv *fsi) |
| 273 | { |
| 274 | int is_porta = fsi_is_port_a(fsi); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 275 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 276 | |
| 277 | return is_porta ? master->info->porta_flags : |
| 278 | master->info->portb_flags; |
| 279 | } |
| 280 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 281 | static inline int fsi_stream_is_play(int stream) |
| 282 | { |
| 283 | return stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 284 | } |
| 285 | |
Kuninori Morimoto | 0054578 | 2010-10-12 18:30:14 +0900 | [diff] [blame] | 286 | static inline int fsi_is_play(struct snd_pcm_substream *substream) |
| 287 | { |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 288 | return fsi_stream_is_play(substream->stream); |
| 289 | } |
| 290 | |
| 291 | static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi, |
| 292 | int is_play) |
| 293 | { |
| 294 | return is_play ? &fsi->playback : &fsi->capture; |
Kuninori Morimoto | 0054578 | 2010-10-12 18:30:14 +0900 | [diff] [blame] | 295 | } |
| 296 | |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 297 | 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] | 298 | { |
| 299 | int is_porta = fsi_is_port_a(fsi); |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 300 | u32 shift; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 301 | |
| 302 | if (is_porta) |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 303 | shift = is_play ? AO_SHIFT : AI_SHIFT; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 304 | else |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 305 | shift = is_play ? BO_SHIFT : BI_SHIFT; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 306 | |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 307 | return shift; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void fsi_stream_push(struct fsi_priv *fsi, |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 311 | int is_play, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 312 | struct snd_pcm_substream *substream, |
| 313 | u32 buffer_len, |
| 314 | u32 period_len) |
| 315 | { |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 316 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
| 317 | |
| 318 | io->substream = substream; |
| 319 | io->buff_len = buffer_len; |
| 320 | io->buff_offset = 0; |
| 321 | io->period_len = period_len; |
| 322 | io->period_num = 0; |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 323 | io->oerr_num = -1; /* ignore 1st err */ |
| 324 | io->uerr_num = -1; /* ignore 1st err */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 325 | } |
| 326 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 327 | static void fsi_stream_pop(struct fsi_priv *fsi, int is_play) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 328 | { |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 329 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 330 | struct snd_soc_dai *dai = fsi_get_dai(io->substream); |
| 331 | |
| 332 | |
| 333 | if (io->oerr_num > 0) |
| 334 | dev_err(dai->dev, "over_run = %d\n", io->oerr_num); |
| 335 | |
| 336 | if (io->uerr_num > 0) |
| 337 | dev_err(dai->dev, "under_run = %d\n", io->uerr_num); |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 338 | |
| 339 | io->substream = NULL; |
| 340 | io->buff_len = 0; |
| 341 | io->buff_offset = 0; |
| 342 | io->period_len = 0; |
| 343 | io->period_num = 0; |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 344 | io->oerr_num = 0; |
| 345 | io->uerr_num = 0; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 346 | } |
| 347 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 348 | static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 349 | { |
| 350 | u32 status; |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 351 | int data_num; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 352 | |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 353 | status = is_play ? |
| 354 | fsi_reg_read(fsi, DOFF_ST) : |
| 355 | fsi_reg_read(fsi, DIFF_ST); |
| 356 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 357 | data_num = 0x1ff & (status >> 8); |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 358 | data_num *= fsi->chan_num; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 359 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 360 | return data_num; |
| 361 | } |
| 362 | |
| 363 | static int fsi_len2num(int len, int width) |
| 364 | { |
| 365 | return len / width; |
| 366 | } |
| 367 | |
| 368 | #define fsi_num2offset(a, b) fsi_num2len(a, b) |
| 369 | static int fsi_num2len(int num, int width) |
| 370 | { |
| 371 | return num * width; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 372 | } |
| 373 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 374 | static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play) |
Kuninori Morimoto | cca1b23 | 2010-10-12 11:39:25 +0900 | [diff] [blame] | 375 | { |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 376 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
| 377 | struct snd_pcm_substream *substream = io->substream; |
Kuninori Morimoto | cca1b23 | 2010-10-12 11:39:25 +0900 | [diff] [blame] | 378 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 379 | |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 380 | return frames_to_bytes(runtime, 1) / fsi->chan_num; |
Kuninori Morimoto | cca1b23 | 2010-10-12 11:39:25 +0900 | [diff] [blame] | 381 | } |
| 382 | |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 383 | static void fsi_count_fifo_err(struct fsi_priv *fsi) |
| 384 | { |
| 385 | u32 ostatus = fsi_reg_read(fsi, DOFF_ST); |
| 386 | u32 istatus = fsi_reg_read(fsi, DIFF_ST); |
| 387 | |
| 388 | if (ostatus & ERR_OVER) |
| 389 | fsi->playback.oerr_num++; |
| 390 | |
| 391 | if (ostatus & ERR_UNDER) |
| 392 | fsi->playback.uerr_num++; |
| 393 | |
| 394 | if (istatus & ERR_OVER) |
| 395 | fsi->capture.oerr_num++; |
| 396 | |
| 397 | if (istatus & ERR_UNDER) |
| 398 | fsi->capture.uerr_num++; |
| 399 | |
| 400 | fsi_reg_write(fsi, DOFF_ST, 0); |
| 401 | fsi_reg_write(fsi, DIFF_ST, 0); |
| 402 | } |
| 403 | |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 404 | /* |
| 405 | * dma function |
| 406 | */ |
| 407 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 408 | static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream) |
Kuninori Morimoto | c79eab3 | 2010-09-17 13:48:05 +0900 | [diff] [blame] | 409 | { |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 410 | int is_play = fsi_stream_is_play(stream); |
| 411 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
| 412 | |
| 413 | return io->substream->runtime->dma_area + io->buff_offset; |
Kuninori Morimoto | c79eab3 | 2010-09-17 13:48:05 +0900 | [diff] [blame] | 414 | } |
| 415 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 416 | static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 417 | { |
| 418 | u16 *start; |
| 419 | int i; |
| 420 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 421 | start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK); |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 422 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 423 | for (i = 0; i < num; i++) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 424 | fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8)); |
| 425 | } |
| 426 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 427 | static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 428 | { |
| 429 | u16 *start; |
| 430 | int i; |
| 431 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 432 | start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE); |
| 433 | |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 434 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 435 | for (i = 0; i < num; i++) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 436 | *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8); |
| 437 | } |
| 438 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 439 | static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 440 | { |
| 441 | u32 *start; |
| 442 | int i; |
| 443 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 444 | start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK); |
| 445 | |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 446 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 447 | for (i = 0; i < num; i++) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 448 | fsi_reg_write(fsi, DODT, *(start + i)); |
| 449 | } |
| 450 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 451 | static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 452 | { |
| 453 | u32 *start; |
| 454 | int i; |
| 455 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 456 | start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE); |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 457 | |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 458 | for (i = 0; i < num; i++) |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 459 | *(start + i) = fsi_reg_read(fsi, DIDT); |
| 460 | } |
| 461 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 462 | /* |
| 463 | * irq function |
| 464 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 465 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 466 | static void fsi_irq_enable(struct fsi_priv *fsi, int is_play) |
| 467 | { |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 468 | u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play)); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 469 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 470 | |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 471 | fsi_core_mask_set(master, imsk, data, data); |
| 472 | fsi_core_mask_set(master, iemsk, data, data); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static void fsi_irq_disable(struct fsi_priv *fsi, int is_play) |
| 476 | { |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 477 | u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play)); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 478 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 479 | |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 480 | fsi_core_mask_set(master, imsk, data, 0); |
| 481 | fsi_core_mask_set(master, iemsk, data, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 482 | } |
| 483 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame] | 484 | static u32 fsi_irq_get_status(struct fsi_master *master) |
| 485 | { |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 486 | return fsi_core_read(master, int_st); |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame] | 487 | } |
| 488 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame] | 489 | static void fsi_irq_clear_status(struct fsi_priv *fsi) |
| 490 | { |
| 491 | u32 data = 0; |
| 492 | struct fsi_master *master = fsi_get_master(fsi); |
| 493 | |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 494 | data |= AB_IO(1, fsi_get_port_shift(fsi, 0)); |
| 495 | data |= AB_IO(1, fsi_get_port_shift(fsi, 1)); |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame] | 496 | |
| 497 | /* clear interrupt factor */ |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 498 | fsi_core_mask_set(master, int_st, data, 0); |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame] | 499 | } |
| 500 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 501 | /* |
| 502 | * SPDIF master clock function |
| 503 | * |
| 504 | * These functions are used later FSI2 |
| 505 | */ |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 506 | static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable) |
| 507 | { |
| 508 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | 2b0e730 | 2010-12-03 17:37:44 +0900 | [diff] [blame] | 509 | u32 mask, val; |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 510 | |
| 511 | if (master->core->ver < 2) { |
| 512 | pr_err("fsi: register access err (%s)\n", __func__); |
| 513 | return; |
| 514 | } |
| 515 | |
Kuninori Morimoto | 2b0e730 | 2010-12-03 17:37:44 +0900 | [diff] [blame] | 516 | mask = BP | SE; |
| 517 | val = enable ? mask : 0; |
| 518 | |
| 519 | fsi_is_port_a(fsi) ? |
Kuninori Morimoto | 43fa95c | 2010-12-03 17:38:03 +0900 | [diff] [blame] | 520 | fsi_core_mask_set(master, a_mclk, mask, val) : |
| 521 | fsi_core_mask_set(master, b_mclk, mask, val); |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 522 | } |
| 523 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 524 | /* |
| 525 | * ctrl function |
| 526 | */ |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 527 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 528 | static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable) |
| 529 | { |
| 530 | u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 531 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 532 | |
| 533 | if (enable) |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 534 | fsi_master_mask_set(master, CLK_RST, val, val); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 535 | else |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 536 | fsi_master_mask_set(master, CLK_RST, val, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 537 | } |
| 538 | |
Kuninori Morimoto | 4a942b4 | 2010-03-25 19:15:51 +0900 | [diff] [blame] | 539 | static void fsi_fifo_init(struct fsi_priv *fsi, |
| 540 | int is_play, |
| 541 | struct snd_soc_dai *dai) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 542 | { |
Kuninori Morimoto | 4a942b4 | 2010-03-25 19:15:51 +0900 | [diff] [blame] | 543 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 544 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 545 | u32 shift, i; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 546 | |
Kuninori Morimoto | 4a942b4 | 2010-03-25 19:15:51 +0900 | [diff] [blame] | 547 | /* get on-chip RAM capacity */ |
| 548 | shift = fsi_master_read(master, FIFO_SZ); |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 549 | shift >>= fsi_get_port_shift(fsi, is_play); |
| 550 | shift &= FIFO_SZ_MASK; |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 551 | io->fifo_max_num = 256 << shift; |
| 552 | dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num); |
Kuninori Morimoto | 4a942b4 | 2010-03-25 19:15:51 +0900 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * The maximum number of sample data varies depending |
| 556 | * on the number of channels selected for the format. |
| 557 | * |
| 558 | * FIFOs are used in 4-channel units in 3-channel mode |
| 559 | * and in 8-channel units in 5- to 7-channel mode |
| 560 | * meaning that more FIFOs than the required size of DPRAM |
| 561 | * are used. |
| 562 | * |
| 563 | * ex) if 256 words of DP-RAM is connected |
| 564 | * 1 channel: 256 (256 x 1 = 256) |
| 565 | * 2 channels: 128 (128 x 2 = 256) |
| 566 | * 3 channels: 64 ( 64 x 3 = 192) |
| 567 | * 4 channels: 64 ( 64 x 4 = 256) |
| 568 | * 5 channels: 32 ( 32 x 5 = 160) |
| 569 | * 6 channels: 32 ( 32 x 6 = 192) |
| 570 | * 7 channels: 32 ( 32 x 7 = 224) |
| 571 | * 8 channels: 32 ( 32 x 8 = 256) |
| 572 | */ |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 573 | for (i = 1; i < fsi->chan_num; i <<= 1) |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 574 | io->fifo_max_num >>= 1; |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 575 | dev_dbg(dai->dev, "%d channel %d store\n", |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 576 | fsi->chan_num, io->fifo_max_num); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 577 | |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 578 | /* |
| 579 | * set interrupt generation factor |
| 580 | * clear FIFO |
| 581 | */ |
| 582 | if (is_play) { |
| 583 | fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF); |
| 584 | fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR); |
| 585 | } else { |
| 586 | fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF); |
| 587 | fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR); |
| 588 | } |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 589 | } |
| 590 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 591 | static void fsi_soft_all_reset(struct fsi_master *master) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 592 | { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 593 | /* port AB reset */ |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 594 | fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 595 | mdelay(10); |
| 596 | |
| 597 | /* soft reset */ |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 598 | fsi_master_mask_set(master, SOFT_RST, FSISR, 0); |
| 599 | fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 600 | mdelay(10); |
| 601 | } |
| 602 | |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 603 | static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 604 | { |
| 605 | struct snd_pcm_runtime *runtime; |
| 606 | struct snd_pcm_substream *substream = NULL; |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 607 | int is_play = fsi_stream_is_play(stream); |
| 608 | struct fsi_stream *io = fsi_get_stream(fsi, is_play); |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 609 | int data_residue_num; |
| 610 | int data_num; |
| 611 | int data_num_max; |
Kuninori Morimoto | 5bfb9ad | 2010-09-17 13:48:45 +0900 | [diff] [blame] | 612 | int ch_width; |
Kuninori Morimoto | b9fde18 | 2010-09-17 13:48:32 +0900 | [diff] [blame] | 613 | int over_period; |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 614 | void (*fn)(struct fsi_priv *fsi, int size); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 615 | |
| 616 | if (!fsi || |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 617 | !io->substream || |
| 618 | !io->substream->runtime) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 619 | return -EINVAL; |
| 620 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 621 | over_period = 0; |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 622 | substream = io->substream; |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 623 | runtime = substream->runtime; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 624 | |
| 625 | /* FSI FIFO has limit. |
| 626 | * So, this driver can not send periods data at a time |
| 627 | */ |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 628 | if (io->buff_offset >= |
| 629 | fsi_num2offset(io->period_num + 1, io->period_len)) { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 630 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 631 | over_period = 1; |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 632 | io->period_num = (io->period_num + 1) % runtime->periods; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 633 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 634 | if (0 == io->period_num) |
| 635 | io->buff_offset = 0; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* get 1 channel data width */ |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 639 | ch_width = fsi_get_frame_width(fsi, is_play); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 640 | |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 641 | /* get residue data number of alsa */ |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 642 | data_residue_num = fsi_len2num(io->buff_len - io->buff_offset, |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 643 | ch_width); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 644 | |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 645 | if (is_play) { |
| 646 | /* |
| 647 | * for play-back |
| 648 | * |
| 649 | * data_num_max : number of FSI fifo free space |
| 650 | * data_num : number of ALSA residue data |
| 651 | */ |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 652 | data_num_max = io->fifo_max_num * fsi->chan_num; |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 653 | data_num_max -= fsi_get_fifo_data_num(fsi, is_play); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 654 | |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 655 | data_num = data_residue_num; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 656 | |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 657 | switch (ch_width) { |
| 658 | case 2: |
| 659 | fn = fsi_dma_soft_push16; |
| 660 | break; |
| 661 | case 4: |
| 662 | fn = fsi_dma_soft_push32; |
| 663 | break; |
| 664 | default: |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | } else { |
| 668 | /* |
| 669 | * for capture |
| 670 | * |
| 671 | * data_num_max : number of ALSA free space |
| 672 | * data_num : number of data in FSI fifo |
| 673 | */ |
| 674 | data_num_max = data_residue_num; |
| 675 | data_num = fsi_get_fifo_data_num(fsi, is_play); |
| 676 | |
| 677 | switch (ch_width) { |
| 678 | case 2: |
| 679 | fn = fsi_dma_soft_pop16; |
| 680 | break; |
| 681 | case 4: |
| 682 | fn = fsi_dma_soft_pop32; |
| 683 | break; |
| 684 | default: |
| 685 | return -EINVAL; |
| 686 | } |
Kuninori Morimoto | 9ddc9aa | 2009-10-30 12:02:39 +0900 | [diff] [blame] | 687 | } |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 688 | |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 689 | data_num = min(data_num, data_num_max); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 690 | |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 691 | fn(fsi, data_num); |
| 692 | |
| 693 | /* update buff_offset */ |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 694 | io->buff_offset += fsi_num2offset(data_num, ch_width); |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 695 | |
Kuninori Morimoto | 1c418d1 | 2009-12-28 14:09:05 +0900 | [diff] [blame] | 696 | if (over_period) |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 697 | snd_pcm_period_elapsed(substream); |
| 698 | |
Kuninori Morimoto | 47fc9a0 | 2010-02-22 16:41:57 +0900 | [diff] [blame] | 699 | return 0; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 700 | } |
| 701 | |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 702 | static int fsi_data_pop(struct fsi_priv *fsi) |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 703 | { |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 704 | return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE); |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 705 | } |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 706 | |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 707 | static int fsi_data_push(struct fsi_priv *fsi) |
Kuninori Morimoto | d8b3353 | 2010-09-17 13:49:05 +0900 | [diff] [blame] | 708 | { |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 709 | return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK); |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 710 | } |
| 711 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 712 | static irqreturn_t fsi_interrupt(int irq, void *data) |
| 713 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 714 | struct fsi_master *master = data; |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame] | 715 | u32 int_st = fsi_irq_get_status(master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 716 | |
| 717 | /* clear irq status */ |
Kuninori Morimoto | feb58cf | 2010-03-24 15:27:24 +0900 | [diff] [blame] | 718 | fsi_master_mask_set(master, SOFT_RST, IR, 0); |
| 719 | fsi_master_mask_set(master, SOFT_RST, IR, IR); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 720 | |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 721 | if (int_st & AB_IO(1, AO_SHIFT)) |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 722 | fsi_data_push(&master->fsia); |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 723 | if (int_st & AB_IO(1, BO_SHIFT)) |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 724 | fsi_data_push(&master->fsib); |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 725 | if (int_st & AB_IO(1, AI_SHIFT)) |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 726 | fsi_data_pop(&master->fsia); |
Kuninori Morimoto | cf6edd0 | 2010-10-12 11:40:53 +0900 | [diff] [blame] | 727 | if (int_st & AB_IO(1, BI_SHIFT)) |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 728 | fsi_data_pop(&master->fsib); |
| 729 | |
| 730 | fsi_count_fifo_err(&master->fsia); |
| 731 | fsi_count_fifo_err(&master->fsib); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 732 | |
Kuninori Morimoto | 48d78e5 | 2010-12-03 17:37:31 +0900 | [diff] [blame] | 733 | fsi_irq_clear_status(&master->fsia); |
| 734 | fsi_irq_clear_status(&master->fsib); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 735 | |
| 736 | return IRQ_HANDLED; |
| 737 | } |
| 738 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 739 | /* |
| 740 | * dai ops |
| 741 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 742 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 743 | static int fsi_dai_startup(struct snd_pcm_substream *substream, |
| 744 | struct snd_soc_dai *dai) |
| 745 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 746 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 747 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 748 | u32 flags = fsi_get_info_flags(fsi); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 749 | u32 fmt; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 750 | u32 data; |
Kuninori Morimoto | 0054578 | 2010-10-12 18:30:14 +0900 | [diff] [blame] | 751 | int is_play = fsi_is_play(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 752 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 753 | pm_runtime_get_sync(dai->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 754 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 755 | |
| 756 | /* clock inversion (CKG2) */ |
| 757 | data = 0; |
Kuninori Morimoto | b427b44 | 2010-07-13 12:01:15 +0900 | [diff] [blame] | 758 | if (SH_FSI_LRM_INV & flags) |
| 759 | data |= 1 << 12; |
| 760 | if (SH_FSI_BRM_INV & flags) |
| 761 | data |= 1 << 8; |
| 762 | if (SH_FSI_LRS_INV & flags) |
| 763 | data |= 1 << 4; |
| 764 | if (SH_FSI_BRS_INV & flags) |
| 765 | data |= 1 << 0; |
| 766 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 767 | fsi_reg_write(fsi, CKG2, data); |
| 768 | |
| 769 | /* do fmt, di fmt */ |
| 770 | data = 0; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 771 | fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags); |
| 772 | switch (fmt) { |
| 773 | case SH_FSI_FMT_MONO: |
Kuninori Morimoto | a7ffb52 | 2010-07-13 12:13:00 +0900 | [diff] [blame] | 774 | data = CR_MONO; |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 775 | fsi->chan_num = 1; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 776 | break; |
| 777 | case SH_FSI_FMT_MONO_DELAY: |
Kuninori Morimoto | a7ffb52 | 2010-07-13 12:13:00 +0900 | [diff] [blame] | 778 | data = CR_MONO_D; |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 779 | fsi->chan_num = 1; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 780 | break; |
| 781 | case SH_FSI_FMT_PCM: |
Kuninori Morimoto | a7ffb52 | 2010-07-13 12:13:00 +0900 | [diff] [blame] | 782 | data = CR_PCM; |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 783 | fsi->chan_num = 2; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 784 | break; |
| 785 | case SH_FSI_FMT_I2S: |
Kuninori Morimoto | a7ffb52 | 2010-07-13 12:13:00 +0900 | [diff] [blame] | 786 | data = CR_I2S; |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 787 | fsi->chan_num = 2; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 788 | break; |
| 789 | case SH_FSI_FMT_TDM: |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 790 | fsi->chan_num = is_play ? |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 791 | SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags); |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 792 | data = CR_TDM | (fsi->chan_num - 1); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 793 | break; |
| 794 | case SH_FSI_FMT_TDM_DELAY: |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 795 | fsi->chan_num = is_play ? |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 796 | SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags); |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 797 | data = CR_TDM_D | (fsi->chan_num - 1); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 798 | break; |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 799 | case SH_FSI_FMT_SPDIF: |
| 800 | if (master->core->ver < 2) { |
| 801 | dev_err(dai->dev, "This FSI can not use SPDIF\n"); |
| 802 | return -EINVAL; |
| 803 | } |
Kuninori Morimoto | f7d711e | 2010-12-03 17:36:24 +0900 | [diff] [blame] | 804 | data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM; |
Kuninori Morimoto | 160afa7 | 2011-01-24 10:42:08 +0900 | [diff] [blame^] | 805 | fsi->chan_num = 2; |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 806 | fsi_spdif_clk_ctrl(fsi, 1); |
Kuninori Morimoto | f7d711e | 2010-12-03 17:36:24 +0900 | [diff] [blame] | 807 | fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD); |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 808 | break; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 809 | default: |
| 810 | dev_err(dai->dev, "unknown format.\n"); |
| 811 | return -EINVAL; |
| 812 | } |
Kuninori Morimoto | e8c8b63 | 2010-12-03 17:37:55 +0900 | [diff] [blame] | 813 | is_play ? |
| 814 | fsi_reg_write(fsi, DO_FMT, data) : |
| 815 | fsi_reg_write(fsi, DI_FMT, data); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 816 | |
Kuninori Morimoto | 10ea76c | 2010-03-23 11:47:54 +0900 | [diff] [blame] | 817 | /* irq clear */ |
| 818 | fsi_irq_disable(fsi, is_play); |
| 819 | fsi_irq_clear_status(fsi); |
| 820 | |
| 821 | /* fifo init */ |
Kuninori Morimoto | 4a942b4 | 2010-03-25 19:15:51 +0900 | [diff] [blame] | 822 | fsi_fifo_init(fsi, is_play, dai); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 823 | |
Kuninori Morimoto | a68a3b4 | 2010-10-12 11:39:50 +0900 | [diff] [blame] | 824 | return 0; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static void fsi_dai_shutdown(struct snd_pcm_substream *substream, |
| 828 | struct snd_soc_dai *dai) |
| 829 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 830 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | 0054578 | 2010-10-12 18:30:14 +0900 | [diff] [blame] | 831 | int is_play = fsi_is_play(substream); |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 832 | struct fsi_master *master = fsi_get_master(fsi); |
| 833 | int (*set_rate)(struct device *dev, int is_porta, int rate, int enable); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 834 | |
| 835 | fsi_irq_disable(fsi, is_play); |
| 836 | fsi_clk_ctrl(fsi, 0); |
| 837 | |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 838 | set_rate = master->info->set_rate; |
| 839 | if (set_rate && fsi->rate) |
| 840 | set_rate(dai->dev, fsi_is_port_a(fsi), fsi->rate, 0); |
| 841 | fsi->rate = 0; |
| 842 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 843 | pm_runtime_put_sync(dai->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd, |
| 847 | struct snd_soc_dai *dai) |
| 848 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 849 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 850 | struct snd_pcm_runtime *runtime = substream->runtime; |
Kuninori Morimoto | 0054578 | 2010-10-12 18:30:14 +0900 | [diff] [blame] | 851 | int is_play = fsi_is_play(substream); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 852 | int ret = 0; |
| 853 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 854 | switch (cmd) { |
| 855 | case SNDRV_PCM_TRIGGER_START: |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 856 | fsi_stream_push(fsi, is_play, substream, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 857 | frames_to_bytes(runtime, runtime->buffer_size), |
| 858 | frames_to_bytes(runtime, runtime->period_size)); |
Kuninori Morimoto | 1ec9bc3 | 2010-12-17 12:55:22 +0900 | [diff] [blame] | 859 | ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi); |
Kuninori Morimoto | 9e261bb | 2010-12-17 12:52:56 +0900 | [diff] [blame] | 860 | fsi_irq_enable(fsi, is_play); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 861 | break; |
| 862 | case SNDRV_PCM_TRIGGER_STOP: |
| 863 | fsi_irq_disable(fsi, is_play); |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 864 | fsi_stream_pop(fsi, is_play); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | |
| 868 | return ret; |
| 869 | } |
| 870 | |
Kuninori Morimoto | 4d805f7 | 2011-01-20 11:46:02 +0900 | [diff] [blame] | 871 | static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 872 | { |
| 873 | struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai); |
| 874 | u32 data = 0; |
| 875 | int ret; |
| 876 | |
| 877 | pm_runtime_get_sync(dai->dev); |
| 878 | |
| 879 | /* set master/slave audio interface */ |
| 880 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 881 | case SND_SOC_DAIFMT_CBM_CFM: |
| 882 | data = DIMD | DOMD; |
| 883 | break; |
| 884 | case SND_SOC_DAIFMT_CBS_CFS: |
| 885 | break; |
| 886 | default: |
| 887 | ret = -EINVAL; |
| 888 | goto set_fmt_exit; |
| 889 | } |
| 890 | fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data); |
| 891 | ret = 0; |
| 892 | |
| 893 | set_fmt_exit: |
| 894 | pm_runtime_put_sync(dai->dev); |
| 895 | |
| 896 | return ret; |
| 897 | } |
| 898 | |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 899 | static int fsi_dai_hw_params(struct snd_pcm_substream *substream, |
| 900 | struct snd_pcm_hw_params *params, |
| 901 | struct snd_soc_dai *dai) |
| 902 | { |
| 903 | struct fsi_priv *fsi = fsi_get_priv(substream); |
| 904 | struct fsi_master *master = fsi_get_master(fsi); |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 905 | int (*set_rate)(struct device *dev, int is_porta, int rate, int enable); |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 906 | int fsi_ver = master->core->ver; |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 907 | long rate = params_rate(params); |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 908 | int ret; |
| 909 | |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 910 | set_rate = master->info->set_rate; |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 911 | if (!set_rate) |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 912 | return 0; |
| 913 | |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 914 | ret = set_rate(dai->dev, fsi_is_port_a(fsi), rate, 1); |
| 915 | if (ret < 0) /* error */ |
| 916 | return ret; |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 917 | |
Kuninori Morimoto | d4bc99b | 2010-11-24 02:44:06 +0000 | [diff] [blame] | 918 | fsi->rate = rate; |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 919 | if (ret > 0) { |
| 920 | u32 data = 0; |
| 921 | |
| 922 | switch (ret & SH_FSI_ACKMD_MASK) { |
| 923 | default: |
| 924 | /* FALL THROUGH */ |
| 925 | case SH_FSI_ACKMD_512: |
| 926 | data |= (0x0 << 12); |
| 927 | break; |
| 928 | case SH_FSI_ACKMD_256: |
| 929 | data |= (0x1 << 12); |
| 930 | break; |
| 931 | case SH_FSI_ACKMD_128: |
| 932 | data |= (0x2 << 12); |
| 933 | break; |
| 934 | case SH_FSI_ACKMD_64: |
| 935 | data |= (0x3 << 12); |
| 936 | break; |
| 937 | case SH_FSI_ACKMD_32: |
| 938 | if (fsi_ver < 2) |
| 939 | dev_err(dai->dev, "unsupported ACKMD\n"); |
| 940 | else |
| 941 | data |= (0x4 << 12); |
| 942 | break; |
| 943 | } |
| 944 | |
| 945 | switch (ret & SH_FSI_BPFMD_MASK) { |
| 946 | default: |
| 947 | /* FALL THROUGH */ |
| 948 | case SH_FSI_BPFMD_32: |
| 949 | data |= (0x0 << 8); |
| 950 | break; |
| 951 | case SH_FSI_BPFMD_64: |
| 952 | data |= (0x1 << 8); |
| 953 | break; |
| 954 | case SH_FSI_BPFMD_128: |
| 955 | data |= (0x2 << 8); |
| 956 | break; |
| 957 | case SH_FSI_BPFMD_256: |
| 958 | data |= (0x3 << 8); |
| 959 | break; |
| 960 | case SH_FSI_BPFMD_512: |
| 961 | data |= (0x4 << 8); |
| 962 | break; |
| 963 | case SH_FSI_BPFMD_16: |
| 964 | if (fsi_ver < 2) |
| 965 | dev_err(dai->dev, "unsupported ACKMD\n"); |
| 966 | else |
| 967 | data |= (0x7 << 8); |
| 968 | break; |
| 969 | } |
| 970 | |
| 971 | fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data); |
| 972 | udelay(10); |
| 973 | fsi_clk_ctrl(fsi, 1); |
| 974 | ret = 0; |
| 975 | } |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 976 | |
| 977 | return ret; |
| 978 | |
| 979 | } |
| 980 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 981 | static struct snd_soc_dai_ops fsi_dai_ops = { |
| 982 | .startup = fsi_dai_startup, |
| 983 | .shutdown = fsi_dai_shutdown, |
| 984 | .trigger = fsi_dai_trigger, |
Kuninori Morimoto | 4d805f7 | 2011-01-20 11:46:02 +0900 | [diff] [blame] | 985 | .set_fmt = fsi_dai_set_fmt, |
Kuninori Morimoto | ccad7b4 | 2010-07-13 12:13:14 +0900 | [diff] [blame] | 986 | .hw_params = fsi_dai_hw_params, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 987 | }; |
| 988 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 989 | /* |
| 990 | * pcm ops |
| 991 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 992 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 993 | static struct snd_pcm_hardware fsi_pcm_hardware = { |
| 994 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 995 | SNDRV_PCM_INFO_MMAP | |
| 996 | SNDRV_PCM_INFO_MMAP_VALID | |
| 997 | SNDRV_PCM_INFO_PAUSE, |
| 998 | .formats = FSI_FMTS, |
| 999 | .rates = FSI_RATES, |
| 1000 | .rate_min = 8000, |
| 1001 | .rate_max = 192000, |
| 1002 | .channels_min = 1, |
| 1003 | .channels_max = 2, |
| 1004 | .buffer_bytes_max = 64 * 1024, |
| 1005 | .period_bytes_min = 32, |
| 1006 | .period_bytes_max = 8192, |
| 1007 | .periods_min = 1, |
| 1008 | .periods_max = 32, |
| 1009 | .fifo_size = 256, |
| 1010 | }; |
| 1011 | |
| 1012 | static int fsi_pcm_open(struct snd_pcm_substream *substream) |
| 1013 | { |
| 1014 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1015 | int ret = 0; |
| 1016 | |
| 1017 | snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware); |
| 1018 | |
| 1019 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 1020 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 1021 | |
| 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | static int fsi_hw_params(struct snd_pcm_substream *substream, |
| 1026 | struct snd_pcm_hw_params *hw_params) |
| 1027 | { |
| 1028 | return snd_pcm_lib_malloc_pages(substream, |
| 1029 | params_buffer_bytes(hw_params)); |
| 1030 | } |
| 1031 | |
| 1032 | static int fsi_hw_free(struct snd_pcm_substream *substream) |
| 1033 | { |
| 1034 | return snd_pcm_lib_free_pages(substream); |
| 1035 | } |
| 1036 | |
| 1037 | static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream) |
| 1038 | { |
| 1039 | struct snd_pcm_runtime *runtime = substream->runtime; |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1040 | struct fsi_priv *fsi = fsi_get_priv(substream); |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 1041 | struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream)); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1042 | long location; |
| 1043 | |
Kuninori Morimoto | 93193c2 | 2010-10-12 19:19:28 +0900 | [diff] [blame] | 1044 | location = (io->buff_offset - 1); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1045 | if (location < 0) |
| 1046 | location = 0; |
| 1047 | |
| 1048 | return bytes_to_frames(runtime, location); |
| 1049 | } |
| 1050 | |
| 1051 | static struct snd_pcm_ops fsi_pcm_ops = { |
| 1052 | .open = fsi_pcm_open, |
| 1053 | .ioctl = snd_pcm_lib_ioctl, |
| 1054 | .hw_params = fsi_hw_params, |
| 1055 | .hw_free = fsi_hw_free, |
| 1056 | .pointer = fsi_pointer, |
| 1057 | }; |
| 1058 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 1059 | /* |
| 1060 | * snd_soc_platform |
| 1061 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1062 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1063 | #define PREALLOC_BUFFER (32 * 1024) |
| 1064 | #define PREALLOC_BUFFER_MAX (32 * 1024) |
| 1065 | |
| 1066 | static void fsi_pcm_free(struct snd_pcm *pcm) |
| 1067 | { |
| 1068 | snd_pcm_lib_preallocate_free_for_all(pcm); |
| 1069 | } |
| 1070 | |
| 1071 | static int fsi_pcm_new(struct snd_card *card, |
| 1072 | struct snd_soc_dai *dai, |
| 1073 | struct snd_pcm *pcm) |
| 1074 | { |
| 1075 | /* |
| 1076 | * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel |
| 1077 | * in MMAP mode (i.e. aplay -M) |
| 1078 | */ |
| 1079 | return snd_pcm_lib_preallocate_pages_for_all( |
| 1080 | pcm, |
| 1081 | SNDRV_DMA_TYPE_CONTINUOUS, |
| 1082 | snd_dma_continuous_data(GFP_KERNEL), |
| 1083 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); |
| 1084 | } |
| 1085 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 1086 | /* |
| 1087 | * alsa struct |
| 1088 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1089 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1090 | static struct snd_soc_dai_driver fsi_soc_dai[] = { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1091 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1092 | .name = "fsia-dai", |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1093 | .playback = { |
| 1094 | .rates = FSI_RATES, |
| 1095 | .formats = FSI_FMTS, |
| 1096 | .channels_min = 1, |
| 1097 | .channels_max = 8, |
| 1098 | }, |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 1099 | .capture = { |
| 1100 | .rates = FSI_RATES, |
| 1101 | .formats = FSI_FMTS, |
| 1102 | .channels_min = 1, |
| 1103 | .channels_max = 8, |
| 1104 | }, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1105 | .ops = &fsi_dai_ops, |
| 1106 | }, |
| 1107 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1108 | .name = "fsib-dai", |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1109 | .playback = { |
| 1110 | .rates = FSI_RATES, |
| 1111 | .formats = FSI_FMTS, |
| 1112 | .channels_min = 1, |
| 1113 | .channels_max = 8, |
| 1114 | }, |
Kuninori Morimoto | 07102f3 | 2009-10-30 12:02:44 +0900 | [diff] [blame] | 1115 | .capture = { |
| 1116 | .rates = FSI_RATES, |
| 1117 | .formats = FSI_FMTS, |
| 1118 | .channels_min = 1, |
| 1119 | .channels_max = 8, |
| 1120 | }, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1121 | .ops = &fsi_dai_ops, |
| 1122 | }, |
| 1123 | }; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1124 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1125 | static struct snd_soc_platform_driver fsi_soc_platform = { |
| 1126 | .ops = &fsi_pcm_ops, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1127 | .pcm_new = fsi_pcm_new, |
| 1128 | .pcm_free = fsi_pcm_free, |
| 1129 | }; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1130 | |
Kuninori Morimoto | c8fe257 | 2010-09-17 13:48:17 +0900 | [diff] [blame] | 1131 | /* |
| 1132 | * platform function |
| 1133 | */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1134 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1135 | static int fsi_probe(struct platform_device *pdev) |
| 1136 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1137 | struct fsi_master *master; |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1138 | const struct platform_device_id *id_entry; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1139 | struct resource *res; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1140 | unsigned int irq; |
| 1141 | int ret; |
| 1142 | |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1143 | id_entry = pdev->id_entry; |
| 1144 | if (!id_entry) { |
| 1145 | dev_err(&pdev->dev, "unknown fsi device\n"); |
| 1146 | return -ENODEV; |
| 1147 | } |
| 1148 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1149 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1150 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | b6aa179 | 2009-12-16 17:10:09 +0100 | [diff] [blame] | 1151 | if (!res || (int)irq <= 0) { |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1152 | dev_err(&pdev->dev, "Not enough FSI platform resources.\n"); |
| 1153 | ret = -ENODEV; |
| 1154 | goto exit; |
| 1155 | } |
| 1156 | |
| 1157 | master = kzalloc(sizeof(*master), GFP_KERNEL); |
| 1158 | if (!master) { |
| 1159 | dev_err(&pdev->dev, "Could not allocate master\n"); |
| 1160 | ret = -ENOMEM; |
| 1161 | goto exit; |
| 1162 | } |
| 1163 | |
| 1164 | master->base = ioremap_nocache(res->start, resource_size(res)); |
| 1165 | if (!master->base) { |
| 1166 | ret = -ENXIO; |
| 1167 | dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n"); |
| 1168 | goto exit_kfree; |
| 1169 | } |
| 1170 | |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 1171 | /* master setting */ |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1172 | master->irq = irq; |
| 1173 | master->info = pdev->dev.platform_data; |
Kuninori Morimoto | 73b92c1 | 2010-07-13 12:13:04 +0900 | [diff] [blame] | 1174 | master->core = (struct fsi_core *)id_entry->driver_data; |
Kuninori Morimoto | 8fc176d | 2010-01-28 13:46:16 +0900 | [diff] [blame] | 1175 | spin_lock_init(&master->lock); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1176 | |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 1177 | /* FSI A setting */ |
| 1178 | master->fsia.base = master->base; |
| 1179 | master->fsia.master = master; |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 1180 | |
| 1181 | /* FSI B setting */ |
| 1182 | master->fsib.base = master->base + 0x40; |
| 1183 | master->fsib.master = master; |
Kuninori Morimoto | 3bc2807 | 2010-07-29 16:48:32 +0900 | [diff] [blame] | 1184 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1185 | pm_runtime_enable(&pdev->dev); |
| 1186 | pm_runtime_resume(&pdev->dev); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1187 | dev_set_drvdata(&pdev->dev, master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1188 | |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1189 | fsi_soft_all_reset(master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1190 | |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1191 | ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, |
| 1192 | id_entry->name, master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1193 | if (ret) { |
| 1194 | dev_err(&pdev->dev, "irq request err\n"); |
Kuninori Morimoto | 9ddc9aa | 2009-10-30 12:02:39 +0900 | [diff] [blame] | 1195 | goto exit_iounmap; |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1196 | } |
| 1197 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1198 | ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1199 | if (ret < 0) { |
| 1200 | dev_err(&pdev->dev, "cannot snd soc register\n"); |
| 1201 | goto exit_free_irq; |
| 1202 | } |
| 1203 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1204 | return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai)); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1205 | |
| 1206 | exit_free_irq: |
| 1207 | free_irq(irq, master); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1208 | exit_iounmap: |
| 1209 | iounmap(master->base); |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1210 | pm_runtime_disable(&pdev->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1211 | exit_kfree: |
| 1212 | kfree(master); |
| 1213 | master = NULL; |
| 1214 | exit: |
| 1215 | return ret; |
| 1216 | } |
| 1217 | |
| 1218 | static int fsi_remove(struct platform_device *pdev) |
| 1219 | { |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1220 | struct fsi_master *master; |
| 1221 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1222 | master = dev_get_drvdata(&pdev->dev); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1223 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1224 | snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai)); |
| 1225 | snd_soc_unregister_platform(&pdev->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1226 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1227 | pm_runtime_disable(&pdev->dev); |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1228 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1229 | free_irq(master->irq, master); |
| 1230 | |
| 1231 | iounmap(master->base); |
| 1232 | kfree(master); |
Kuninori Morimoto | 71f6e06 | 2009-12-02 15:11:08 +0900 | [diff] [blame] | 1233 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1234 | return 0; |
| 1235 | } |
| 1236 | |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1237 | static int fsi_runtime_nop(struct device *dev) |
| 1238 | { |
| 1239 | /* Runtime PM callback shared between ->runtime_suspend() |
| 1240 | * and ->runtime_resume(). Simply returns success. |
| 1241 | * |
| 1242 | * This driver re-initializes all registers after |
| 1243 | * pm_runtime_get_sync() anyway so there is no need |
| 1244 | * to save and restore registers here. |
| 1245 | */ |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | static struct dev_pm_ops fsi_pm_ops = { |
| 1250 | .runtime_suspend = fsi_runtime_nop, |
| 1251 | .runtime_resume = fsi_runtime_nop, |
| 1252 | }; |
| 1253 | |
Kuninori Morimoto | 73b92c1 | 2010-07-13 12:13:04 +0900 | [diff] [blame] | 1254 | static struct fsi_core fsi1_core = { |
| 1255 | .ver = 1, |
| 1256 | |
| 1257 | /* Interrupt */ |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1258 | .int_st = INT_ST, |
| 1259 | .iemsk = IEMSK, |
| 1260 | .imsk = IMSK, |
| 1261 | }; |
| 1262 | |
Kuninori Morimoto | 73b92c1 | 2010-07-13 12:13:04 +0900 | [diff] [blame] | 1263 | static struct fsi_core fsi2_core = { |
| 1264 | .ver = 2, |
| 1265 | |
| 1266 | /* Interrupt */ |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1267 | .int_st = CPU_INT_ST, |
| 1268 | .iemsk = CPU_IEMSK, |
| 1269 | .imsk = CPU_IMSK, |
Kuninori Morimoto | 2b0e730 | 2010-12-03 17:37:44 +0900 | [diff] [blame] | 1270 | .a_mclk = A_MST_CTLR, |
| 1271 | .b_mclk = B_MST_CTLR, |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1272 | }; |
| 1273 | |
| 1274 | static struct platform_device_id fsi_id_table[] = { |
Kuninori Morimoto | 73b92c1 | 2010-07-13 12:13:04 +0900 | [diff] [blame] | 1275 | { "sh_fsi", (kernel_ulong_t)&fsi1_core }, |
| 1276 | { "sh_fsi2", (kernel_ulong_t)&fsi2_core }, |
Guennadi Liakhovetski | 05c6945 | 2010-10-05 17:54:28 +0200 | [diff] [blame] | 1277 | {}, |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1278 | }; |
Dzianis Kahanovich | d85a6d7 | 2010-09-17 16:42:05 +0300 | [diff] [blame] | 1279 | MODULE_DEVICE_TABLE(platform, fsi_id_table); |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1280 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1281 | static struct platform_driver fsi_driver = { |
| 1282 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1283 | .name = "fsi-pcm-audio", |
Kuninori Morimoto | 785d1c4 | 2009-11-30 20:24:48 +0900 | [diff] [blame] | 1284 | .pm = &fsi_pm_ops, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1285 | }, |
| 1286 | .probe = fsi_probe, |
| 1287 | .remove = fsi_remove, |
Kuninori Morimoto | cc780d3 | 2010-03-25 19:15:53 +0900 | [diff] [blame] | 1288 | .id_table = fsi_id_table, |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1289 | }; |
| 1290 | |
| 1291 | static int __init fsi_mobile_init(void) |
| 1292 | { |
| 1293 | return platform_driver_register(&fsi_driver); |
| 1294 | } |
| 1295 | |
| 1296 | static void __exit fsi_mobile_exit(void) |
| 1297 | { |
| 1298 | platform_driver_unregister(&fsi_driver); |
| 1299 | } |
Dzianis Kahanovich | d85a6d7 | 2010-09-17 16:42:05 +0300 | [diff] [blame] | 1300 | |
Kuninori Morimoto | a4d7d55 | 2009-08-20 21:01:05 +0900 | [diff] [blame] | 1301 | module_init(fsi_mobile_init); |
| 1302 | module_exit(fsi_mobile_exit); |
| 1303 | |
| 1304 | MODULE_LICENSE("GPL"); |
| 1305 | MODULE_DESCRIPTION("SuperH onchip FSI audio driver"); |
| 1306 | MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>"); |