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