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