Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Andrea Venturi |
| 3 | * Andrea Venturi <be17068@iperbole.bo.it> |
| 4 | * |
| 5 | * Copyright (C) 2016 Maxime Ripard |
| 6 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/dmaengine.h> |
| 16 | #include <linux/module.h> |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 17 | #include <linux/of_device.h> |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pm_runtime.h> |
| 20 | #include <linux/regmap.h> |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 21 | #include <linux/reset.h> |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 22 | |
| 23 | #include <sound/dmaengine_pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/soc-dai.h> |
| 27 | |
| 28 | #define SUN4I_I2S_CTRL_REG 0x00 |
| 29 | #define SUN4I_I2S_CTRL_SDO_EN_MASK GENMASK(11, 8) |
| 30 | #define SUN4I_I2S_CTRL_SDO_EN(sdo) BIT(8 + (sdo)) |
| 31 | #define SUN4I_I2S_CTRL_MODE_MASK BIT(5) |
| 32 | #define SUN4I_I2S_CTRL_MODE_SLAVE (1 << 5) |
| 33 | #define SUN4I_I2S_CTRL_MODE_MASTER (0 << 5) |
| 34 | #define SUN4I_I2S_CTRL_TX_EN BIT(2) |
| 35 | #define SUN4I_I2S_CTRL_RX_EN BIT(1) |
| 36 | #define SUN4I_I2S_CTRL_GL_EN BIT(0) |
| 37 | |
| 38 | #define SUN4I_I2S_FMT0_REG 0x04 |
| 39 | #define SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK BIT(7) |
| 40 | #define SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED (1 << 7) |
| 41 | #define SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL (0 << 7) |
| 42 | #define SUN4I_I2S_FMT0_BCLK_POLARITY_MASK BIT(6) |
| 43 | #define SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED (1 << 6) |
| 44 | #define SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL (0 << 6) |
| 45 | #define SUN4I_I2S_FMT0_SR_MASK GENMASK(5, 4) |
| 46 | #define SUN4I_I2S_FMT0_SR(sr) ((sr) << 4) |
| 47 | #define SUN4I_I2S_FMT0_WSS_MASK GENMASK(3, 2) |
| 48 | #define SUN4I_I2S_FMT0_WSS(wss) ((wss) << 2) |
| 49 | #define SUN4I_I2S_FMT0_FMT_MASK GENMASK(1, 0) |
| 50 | #define SUN4I_I2S_FMT0_FMT_RIGHT_J (2 << 0) |
| 51 | #define SUN4I_I2S_FMT0_FMT_LEFT_J (1 << 0) |
| 52 | #define SUN4I_I2S_FMT0_FMT_I2S (0 << 0) |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 53 | #define SUN4I_I2S_FMT0_POLARITY_INVERTED (1) |
| 54 | #define SUN4I_I2S_FMT0_POLARITY_NORMAL (0) |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 55 | |
| 56 | #define SUN4I_I2S_FMT1_REG 0x08 |
| 57 | #define SUN4I_I2S_FIFO_TX_REG 0x0c |
| 58 | #define SUN4I_I2S_FIFO_RX_REG 0x10 |
| 59 | |
| 60 | #define SUN4I_I2S_FIFO_CTRL_REG 0x14 |
| 61 | #define SUN4I_I2S_FIFO_CTRL_FLUSH_TX BIT(25) |
| 62 | #define SUN4I_I2S_FIFO_CTRL_FLUSH_RX BIT(24) |
| 63 | #define SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK BIT(2) |
| 64 | #define SUN4I_I2S_FIFO_CTRL_TX_MODE(mode) ((mode) << 2) |
| 65 | #define SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK GENMASK(1, 0) |
| 66 | #define SUN4I_I2S_FIFO_CTRL_RX_MODE(mode) (mode) |
| 67 | |
| 68 | #define SUN4I_I2S_FIFO_STA_REG 0x18 |
| 69 | |
| 70 | #define SUN4I_I2S_DMA_INT_CTRL_REG 0x1c |
| 71 | #define SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN BIT(7) |
| 72 | #define SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN BIT(3) |
| 73 | |
| 74 | #define SUN4I_I2S_INT_STA_REG 0x20 |
| 75 | |
| 76 | #define SUN4I_I2S_CLK_DIV_REG 0x24 |
| 77 | #define SUN4I_I2S_CLK_DIV_MCLK_EN BIT(7) |
| 78 | #define SUN4I_I2S_CLK_DIV_BCLK_MASK GENMASK(6, 4) |
| 79 | #define SUN4I_I2S_CLK_DIV_BCLK(bclk) ((bclk) << 4) |
| 80 | #define SUN4I_I2S_CLK_DIV_MCLK_MASK GENMASK(3, 0) |
| 81 | #define SUN4I_I2S_CLK_DIV_MCLK(mclk) ((mclk) << 0) |
| 82 | |
| 83 | #define SUN4I_I2S_RX_CNT_REG 0x28 |
| 84 | #define SUN4I_I2S_TX_CNT_REG 0x2c |
| 85 | |
| 86 | #define SUN4I_I2S_TX_CHAN_SEL_REG 0x30 |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 87 | #define SUN4I_I2S_CHAN_SEL(num_chan) (((num_chan) - 1) << 0) |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 88 | |
| 89 | #define SUN4I_I2S_TX_CHAN_MAP_REG 0x34 |
| 90 | #define SUN4I_I2S_TX_CHAN_MAP(chan, sample) ((sample) << (chan << 2)) |
| 91 | |
| 92 | #define SUN4I_I2S_RX_CHAN_SEL_REG 0x38 |
| 93 | #define SUN4I_I2S_RX_CHAN_MAP_REG 0x3c |
| 94 | |
Marcus Cooper | 47bea0c | 2017-07-29 16:17:42 +0200 | [diff] [blame] | 95 | /** |
| 96 | * struct sun4i_i2s_quirks - Differences between SoC variants. |
| 97 | * |
| 98 | * @has_reset: SoC needs reset deasserted. |
Marcus Cooper | d03d273 | 2017-08-19 14:48:37 +0200 | [diff] [blame^] | 99 | * @has_slave_select_bit: SoC has a bit to enable slave mode. |
Marcus Cooper | 3509476 | 2017-08-12 13:00:51 +0200 | [diff] [blame] | 100 | * @reg_offset_txdata: offset of the tx fifo. |
Marcus Cooper | cd1c63d | 2017-08-12 13:00:50 +0200 | [diff] [blame] | 101 | * @sun4i_i2s_regmap: regmap config to use. |
Marcus Cooper | 0aef27c | 2017-08-12 13:00:49 +0200 | [diff] [blame] | 102 | * @mclk_offset: Value by which mclkdiv needs to be adjusted. |
| 103 | * @bclk_offset: Value by which bclkdiv needs to be adjusted. |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 104 | * @fmt_offset: Value by which wss and sr needs to be adjusted. |
Marcus Cooper | 5f93b06 | 2017-08-19 14:48:35 +0200 | [diff] [blame] | 105 | * @field_clkdiv_mclk_en: regmap field to enable mclk output. |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 106 | * @field_fmt_wss: regmap field to set word select size. |
| 107 | * @field_fmt_sr: regmap field to set sample resolution. |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 108 | * @field_fmt_bclk: regmap field to set clk polarity. |
| 109 | * @field_fmt_lrclk: regmap field to set frame polarity. |
Marcus Cooper | dfd2293 | 2017-08-19 14:48:36 +0200 | [diff] [blame] | 110 | * @field_fmt_mode: regmap field to set the operational mode. |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 111 | * @field_txchanmap: location of the tx channel mapping register. |
| 112 | * @field_rxchanmap: location of the rx channel mapping register. |
| 113 | * @field_txchansel: location of the tx channel select bit fields. |
| 114 | * @field_rxchansel: location of the rx channel select bit fields. |
Marcus Cooper | 47bea0c | 2017-07-29 16:17:42 +0200 | [diff] [blame] | 115 | */ |
| 116 | struct sun4i_i2s_quirks { |
| 117 | bool has_reset; |
Marcus Cooper | d03d273 | 2017-08-19 14:48:37 +0200 | [diff] [blame^] | 118 | bool has_slave_select_bit; |
Marcus Cooper | 3509476 | 2017-08-12 13:00:51 +0200 | [diff] [blame] | 119 | unsigned int reg_offset_txdata; /* TX FIFO */ |
Marcus Cooper | cd1c63d | 2017-08-12 13:00:50 +0200 | [diff] [blame] | 120 | const struct regmap_config *sun4i_i2s_regmap; |
Marcus Cooper | 0aef27c | 2017-08-12 13:00:49 +0200 | [diff] [blame] | 121 | unsigned int mclk_offset; |
| 122 | unsigned int bclk_offset; |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 123 | unsigned int fmt_offset; |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 124 | |
| 125 | /* Register fields for i2s */ |
Marcus Cooper | 5f93b06 | 2017-08-19 14:48:35 +0200 | [diff] [blame] | 126 | struct reg_field field_clkdiv_mclk_en; |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 127 | struct reg_field field_fmt_wss; |
| 128 | struct reg_field field_fmt_sr; |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 129 | struct reg_field field_fmt_bclk; |
| 130 | struct reg_field field_fmt_lrclk; |
Marcus Cooper | dfd2293 | 2017-08-19 14:48:36 +0200 | [diff] [blame] | 131 | struct reg_field field_fmt_mode; |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 132 | struct reg_field field_txchanmap; |
| 133 | struct reg_field field_rxchanmap; |
| 134 | struct reg_field field_txchansel; |
| 135 | struct reg_field field_rxchansel; |
Marcus Cooper | 47bea0c | 2017-07-29 16:17:42 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 138 | struct sun4i_i2s { |
| 139 | struct clk *bus_clk; |
| 140 | struct clk *mod_clk; |
| 141 | struct regmap *regmap; |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 142 | struct reset_control *rst; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 143 | |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 144 | unsigned int mclk_freq; |
| 145 | |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 146 | struct snd_dmaengine_dai_dma_data capture_dma_data; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 147 | struct snd_dmaengine_dai_dma_data playback_dma_data; |
Marcus Cooper | 47bea0c | 2017-07-29 16:17:42 +0200 | [diff] [blame] | 148 | |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 149 | /* Register fields for i2s */ |
Marcus Cooper | 5f93b06 | 2017-08-19 14:48:35 +0200 | [diff] [blame] | 150 | struct regmap_field *field_clkdiv_mclk_en; |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 151 | struct regmap_field *field_fmt_wss; |
| 152 | struct regmap_field *field_fmt_sr; |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 153 | struct regmap_field *field_fmt_bclk; |
| 154 | struct regmap_field *field_fmt_lrclk; |
Marcus Cooper | dfd2293 | 2017-08-19 14:48:36 +0200 | [diff] [blame] | 155 | struct regmap_field *field_fmt_mode; |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 156 | struct regmap_field *field_txchanmap; |
| 157 | struct regmap_field *field_rxchanmap; |
| 158 | struct regmap_field *field_txchansel; |
| 159 | struct regmap_field *field_rxchansel; |
| 160 | |
Marcus Cooper | 47bea0c | 2017-07-29 16:17:42 +0200 | [diff] [blame] | 161 | const struct sun4i_i2s_quirks *variant; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | struct sun4i_i2s_clk_div { |
| 165 | u8 div; |
| 166 | u8 val; |
| 167 | }; |
| 168 | |
| 169 | static const struct sun4i_i2s_clk_div sun4i_i2s_bclk_div[] = { |
| 170 | { .div = 2, .val = 0 }, |
| 171 | { .div = 4, .val = 1 }, |
| 172 | { .div = 6, .val = 2 }, |
| 173 | { .div = 8, .val = 3 }, |
| 174 | { .div = 12, .val = 4 }, |
| 175 | { .div = 16, .val = 5 }, |
| 176 | }; |
| 177 | |
| 178 | static const struct sun4i_i2s_clk_div sun4i_i2s_mclk_div[] = { |
| 179 | { .div = 1, .val = 0 }, |
| 180 | { .div = 2, .val = 1 }, |
| 181 | { .div = 4, .val = 2 }, |
| 182 | { .div = 6, .val = 3 }, |
| 183 | { .div = 8, .val = 4 }, |
| 184 | { .div = 12, .val = 5 }, |
| 185 | { .div = 16, .val = 6 }, |
| 186 | { .div = 24, .val = 7 }, |
| 187 | }; |
| 188 | |
| 189 | static int sun4i_i2s_get_bclk_div(struct sun4i_i2s *i2s, |
| 190 | unsigned int oversample_rate, |
| 191 | unsigned int word_size) |
| 192 | { |
| 193 | int div = oversample_rate / word_size / 2; |
| 194 | int i; |
| 195 | |
| 196 | for (i = 0; i < ARRAY_SIZE(sun4i_i2s_bclk_div); i++) { |
| 197 | const struct sun4i_i2s_clk_div *bdiv = &sun4i_i2s_bclk_div[i]; |
| 198 | |
| 199 | if (bdiv->div == div) |
| 200 | return bdiv->val; |
| 201 | } |
| 202 | |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | |
| 206 | static int sun4i_i2s_get_mclk_div(struct sun4i_i2s *i2s, |
| 207 | unsigned int oversample_rate, |
| 208 | unsigned int module_rate, |
| 209 | unsigned int sampling_rate) |
| 210 | { |
| 211 | int div = module_rate / sampling_rate / oversample_rate; |
| 212 | int i; |
| 213 | |
| 214 | for (i = 0; i < ARRAY_SIZE(sun4i_i2s_mclk_div); i++) { |
| 215 | const struct sun4i_i2s_clk_div *mdiv = &sun4i_i2s_mclk_div[i]; |
| 216 | |
| 217 | if (mdiv->div == div) |
| 218 | return mdiv->val; |
| 219 | } |
| 220 | |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | |
| 224 | static int sun4i_i2s_oversample_rates[] = { 128, 192, 256, 384, 512, 768 }; |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 225 | static bool sun4i_i2s_oversample_is_valid(unsigned int oversample) |
| 226 | { |
| 227 | int i; |
| 228 | |
| 229 | for (i = 0; i < ARRAY_SIZE(sun4i_i2s_oversample_rates); i++) |
| 230 | if (sun4i_i2s_oversample_rates[i] == oversample) |
| 231 | return true; |
| 232 | |
| 233 | return false; |
| 234 | } |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 235 | |
| 236 | static int sun4i_i2s_set_clk_rate(struct sun4i_i2s *i2s, |
| 237 | unsigned int rate, |
| 238 | unsigned int word_size) |
| 239 | { |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 240 | unsigned int oversample_rate, clk_rate; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 241 | int bclk_div, mclk_div; |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 242 | int ret; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 243 | |
| 244 | switch (rate) { |
| 245 | case 176400: |
| 246 | case 88200: |
| 247 | case 44100: |
| 248 | case 22050: |
| 249 | case 11025: |
| 250 | clk_rate = 22579200; |
| 251 | break; |
| 252 | |
| 253 | case 192000: |
| 254 | case 128000: |
| 255 | case 96000: |
| 256 | case 64000: |
| 257 | case 48000: |
| 258 | case 32000: |
| 259 | case 24000: |
| 260 | case 16000: |
| 261 | case 12000: |
| 262 | case 8000: |
| 263 | clk_rate = 24576000; |
| 264 | break; |
| 265 | |
| 266 | default: |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | ret = clk_set_rate(i2s->mod_clk, clk_rate); |
| 271 | if (ret) |
| 272 | return ret; |
| 273 | |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 274 | oversample_rate = i2s->mclk_freq / rate; |
| 275 | if (!sun4i_i2s_oversample_is_valid(oversample_rate)) |
| 276 | return -EINVAL; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 277 | |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 278 | bclk_div = sun4i_i2s_get_bclk_div(i2s, oversample_rate, |
| 279 | word_size); |
| 280 | if (bclk_div < 0) |
| 281 | return -EINVAL; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 282 | |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 283 | mclk_div = sun4i_i2s_get_mclk_div(i2s, oversample_rate, |
| 284 | clk_rate, rate); |
| 285 | if (mclk_div < 0) |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 286 | return -EINVAL; |
| 287 | |
Marcus Cooper | 0aef27c | 2017-08-12 13:00:49 +0200 | [diff] [blame] | 288 | /* Adjust the clock division values if needed */ |
| 289 | bclk_div += i2s->variant->bclk_offset; |
| 290 | mclk_div += i2s->variant->mclk_offset; |
| 291 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 292 | regmap_write(i2s->regmap, SUN4I_I2S_CLK_DIV_REG, |
| 293 | SUN4I_I2S_CLK_DIV_BCLK(bclk_div) | |
Marcus Cooper | 5f93b06 | 2017-08-19 14:48:35 +0200 | [diff] [blame] | 294 | SUN4I_I2S_CLK_DIV_MCLK(mclk_div)); |
| 295 | |
| 296 | regmap_field_write(i2s->field_clkdiv_mclk_en, 1); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int sun4i_i2s_hw_params(struct snd_pcm_substream *substream, |
| 302 | struct snd_pcm_hw_params *params, |
| 303 | struct snd_soc_dai *dai) |
| 304 | { |
| 305 | struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
| 306 | int sr, wss; |
| 307 | u32 width; |
| 308 | |
| 309 | if (params_channels(params) != 2) |
| 310 | return -EINVAL; |
| 311 | |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 312 | /* Map the channels for playback and capture */ |
| 313 | regmap_field_write(i2s->field_txchanmap, 0x76543210); |
| 314 | regmap_field_write(i2s->field_rxchanmap, 0x00003210); |
| 315 | |
| 316 | /* Configure the channels */ |
| 317 | regmap_field_write(i2s->field_txchansel, |
| 318 | SUN4I_I2S_CHAN_SEL(params_channels(params))); |
| 319 | |
| 320 | regmap_field_write(i2s->field_rxchansel, |
| 321 | SUN4I_I2S_CHAN_SEL(params_channels(params))); |
| 322 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 323 | switch (params_physical_width(params)) { |
| 324 | case 16: |
| 325 | width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 326 | break; |
| 327 | default: |
| 328 | return -EINVAL; |
| 329 | } |
| 330 | i2s->playback_dma_data.addr_width = width; |
| 331 | |
| 332 | switch (params_width(params)) { |
| 333 | case 16: |
| 334 | sr = 0; |
| 335 | wss = 0; |
| 336 | break; |
| 337 | |
| 338 | default: |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 342 | regmap_field_write(i2s->field_fmt_wss, |
| 343 | wss + i2s->variant->fmt_offset); |
| 344 | regmap_field_write(i2s->field_fmt_sr, |
| 345 | sr + i2s->variant->fmt_offset); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 346 | |
| 347 | return sun4i_i2s_set_clk_rate(i2s, params_rate(params), |
| 348 | params_width(params)); |
| 349 | } |
| 350 | |
| 351 | static int sun4i_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 352 | { |
| 353 | struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
| 354 | u32 val; |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 355 | u32 bclk_polarity = SUN4I_I2S_FMT0_POLARITY_NORMAL; |
| 356 | u32 lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_NORMAL; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 357 | |
| 358 | /* DAI Mode */ |
| 359 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 360 | case SND_SOC_DAIFMT_I2S: |
| 361 | val = SUN4I_I2S_FMT0_FMT_I2S; |
| 362 | break; |
| 363 | case SND_SOC_DAIFMT_LEFT_J: |
| 364 | val = SUN4I_I2S_FMT0_FMT_LEFT_J; |
| 365 | break; |
| 366 | case SND_SOC_DAIFMT_RIGHT_J: |
| 367 | val = SUN4I_I2S_FMT0_FMT_RIGHT_J; |
| 368 | break; |
| 369 | default: |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
Marcus Cooper | dfd2293 | 2017-08-19 14:48:36 +0200 | [diff] [blame] | 373 | regmap_field_write(i2s->field_fmt_mode, val); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 374 | |
| 375 | /* DAI clock polarity */ |
| 376 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 377 | case SND_SOC_DAIFMT_IB_IF: |
| 378 | /* Invert both clocks */ |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 379 | bclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED; |
| 380 | lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 381 | break; |
| 382 | case SND_SOC_DAIFMT_IB_NF: |
| 383 | /* Invert bit clock */ |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 384 | bclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 385 | break; |
| 386 | case SND_SOC_DAIFMT_NB_IF: |
| 387 | /* Invert frame clock */ |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 388 | lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 389 | break; |
| 390 | case SND_SOC_DAIFMT_NB_NF: |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 391 | break; |
| 392 | default: |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 396 | regmap_field_write(i2s->field_fmt_bclk, bclk_polarity); |
| 397 | regmap_field_write(i2s->field_fmt_lrclk, lrclk_polarity); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 398 | |
Marcus Cooper | d03d273 | 2017-08-19 14:48:37 +0200 | [diff] [blame^] | 399 | if (i2s->variant->has_slave_select_bit) { |
| 400 | /* DAI clock master masks */ |
| 401 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 402 | case SND_SOC_DAIFMT_CBS_CFS: |
| 403 | /* BCLK and LRCLK master */ |
| 404 | val = SUN4I_I2S_CTRL_MODE_MASTER; |
| 405 | break; |
| 406 | case SND_SOC_DAIFMT_CBM_CFM: |
| 407 | /* BCLK and LRCLK slave */ |
| 408 | val = SUN4I_I2S_CTRL_MODE_SLAVE; |
| 409 | break; |
| 410 | default: |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 414 | SUN4I_I2S_CTRL_MODE_MASK, |
| 415 | val); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 418 | /* Set significant bits in our FIFOs */ |
| 419 | regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG, |
| 420 | SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK | |
| 421 | SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK, |
| 422 | SUN4I_I2S_FIFO_CTRL_TX_MODE(1) | |
| 423 | SUN4I_I2S_FIFO_CTRL_RX_MODE(1)); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 427 | static void sun4i_i2s_start_capture(struct sun4i_i2s *i2s) |
| 428 | { |
| 429 | /* Flush RX FIFO */ |
| 430 | regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG, |
| 431 | SUN4I_I2S_FIFO_CTRL_FLUSH_RX, |
| 432 | SUN4I_I2S_FIFO_CTRL_FLUSH_RX); |
| 433 | |
| 434 | /* Clear RX counter */ |
| 435 | regmap_write(i2s->regmap, SUN4I_I2S_RX_CNT_REG, 0); |
| 436 | |
| 437 | /* Enable RX Block */ |
| 438 | regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 439 | SUN4I_I2S_CTRL_RX_EN, |
| 440 | SUN4I_I2S_CTRL_RX_EN); |
| 441 | |
| 442 | /* Enable RX DRQ */ |
| 443 | regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG, |
| 444 | SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN, |
| 445 | SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN); |
| 446 | } |
| 447 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 448 | static void sun4i_i2s_start_playback(struct sun4i_i2s *i2s) |
| 449 | { |
| 450 | /* Flush TX FIFO */ |
| 451 | regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG, |
| 452 | SUN4I_I2S_FIFO_CTRL_FLUSH_TX, |
| 453 | SUN4I_I2S_FIFO_CTRL_FLUSH_TX); |
| 454 | |
| 455 | /* Clear TX counter */ |
| 456 | regmap_write(i2s->regmap, SUN4I_I2S_TX_CNT_REG, 0); |
| 457 | |
| 458 | /* Enable TX Block */ |
| 459 | regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 460 | SUN4I_I2S_CTRL_TX_EN, |
| 461 | SUN4I_I2S_CTRL_TX_EN); |
| 462 | |
| 463 | /* Enable TX DRQ */ |
| 464 | regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG, |
| 465 | SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN, |
| 466 | SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN); |
| 467 | } |
| 468 | |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 469 | static void sun4i_i2s_stop_capture(struct sun4i_i2s *i2s) |
| 470 | { |
| 471 | /* Disable RX Block */ |
| 472 | regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 473 | SUN4I_I2S_CTRL_RX_EN, |
| 474 | 0); |
| 475 | |
| 476 | /* Disable RX DRQ */ |
| 477 | regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG, |
| 478 | SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN, |
| 479 | 0); |
| 480 | } |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 481 | |
| 482 | static void sun4i_i2s_stop_playback(struct sun4i_i2s *i2s) |
| 483 | { |
| 484 | /* Disable TX Block */ |
| 485 | regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 486 | SUN4I_I2S_CTRL_TX_EN, |
| 487 | 0); |
| 488 | |
| 489 | /* Disable TX DRQ */ |
| 490 | regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG, |
| 491 | SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN, |
| 492 | 0); |
| 493 | } |
| 494 | |
| 495 | static int sun4i_i2s_trigger(struct snd_pcm_substream *substream, int cmd, |
| 496 | struct snd_soc_dai *dai) |
| 497 | { |
| 498 | struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
| 499 | |
| 500 | switch (cmd) { |
| 501 | case SNDRV_PCM_TRIGGER_START: |
| 502 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 503 | case SNDRV_PCM_TRIGGER_RESUME: |
| 504 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 505 | sun4i_i2s_start_playback(i2s); |
| 506 | else |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 507 | sun4i_i2s_start_capture(i2s); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 508 | break; |
| 509 | |
| 510 | case SNDRV_PCM_TRIGGER_STOP: |
| 511 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 512 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 513 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 514 | sun4i_i2s_stop_playback(i2s); |
| 515 | else |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 516 | sun4i_i2s_stop_capture(i2s); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 517 | break; |
| 518 | |
| 519 | default: |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int sun4i_i2s_startup(struct snd_pcm_substream *substream, |
| 527 | struct snd_soc_dai *dai) |
| 528 | { |
| 529 | struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
| 530 | |
| 531 | /* Enable the whole hardware block */ |
| 532 | regmap_write(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 533 | SUN4I_I2S_CTRL_GL_EN); |
| 534 | |
| 535 | /* Enable the first output line */ |
| 536 | regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 537 | SUN4I_I2S_CTRL_SDO_EN_MASK, |
| 538 | SUN4I_I2S_CTRL_SDO_EN(0)); |
| 539 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 540 | |
| 541 | return clk_prepare_enable(i2s->mod_clk); |
| 542 | } |
| 543 | |
| 544 | static void sun4i_i2s_shutdown(struct snd_pcm_substream *substream, |
| 545 | struct snd_soc_dai *dai) |
| 546 | { |
| 547 | struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
| 548 | |
| 549 | clk_disable_unprepare(i2s->mod_clk); |
| 550 | |
| 551 | /* Disable our output lines */ |
| 552 | regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG, |
| 553 | SUN4I_I2S_CTRL_SDO_EN_MASK, 0); |
| 554 | |
| 555 | /* Disable the whole hardware block */ |
| 556 | regmap_write(i2s->regmap, SUN4I_I2S_CTRL_REG, 0); |
| 557 | } |
| 558 | |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 559 | static int sun4i_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 560 | unsigned int freq, int dir) |
| 561 | { |
| 562 | struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
| 563 | |
| 564 | if (clk_id != 0) |
| 565 | return -EINVAL; |
| 566 | |
| 567 | i2s->mclk_freq = freq; |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 572 | static const struct snd_soc_dai_ops sun4i_i2s_dai_ops = { |
| 573 | .hw_params = sun4i_i2s_hw_params, |
| 574 | .set_fmt = sun4i_i2s_set_fmt, |
Maxime Ripard | b2b7b56 | 2016-11-07 14:08:19 +0100 | [diff] [blame] | 575 | .set_sysclk = sun4i_i2s_set_sysclk, |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 576 | .shutdown = sun4i_i2s_shutdown, |
| 577 | .startup = sun4i_i2s_startup, |
| 578 | .trigger = sun4i_i2s_trigger, |
| 579 | }; |
| 580 | |
| 581 | static int sun4i_i2s_dai_probe(struct snd_soc_dai *dai) |
| 582 | { |
| 583 | struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
| 584 | |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 585 | snd_soc_dai_init_dma_data(dai, |
| 586 | &i2s->playback_dma_data, |
| 587 | &i2s->capture_dma_data); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 588 | |
| 589 | snd_soc_dai_set_drvdata(dai, i2s); |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static struct snd_soc_dai_driver sun4i_i2s_dai = { |
| 595 | .probe = sun4i_i2s_dai_probe, |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 596 | .capture = { |
| 597 | .stream_name = "Capture", |
| 598 | .channels_min = 2, |
| 599 | .channels_max = 2, |
| 600 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 601 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 602 | }, |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 603 | .playback = { |
| 604 | .stream_name = "Playback", |
| 605 | .channels_min = 2, |
| 606 | .channels_max = 2, |
| 607 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 608 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 609 | }, |
| 610 | .ops = &sun4i_i2s_dai_ops, |
| 611 | .symmetric_rates = 1, |
| 612 | }; |
| 613 | |
| 614 | static const struct snd_soc_component_driver sun4i_i2s_component = { |
| 615 | .name = "sun4i-dai", |
| 616 | }; |
| 617 | |
| 618 | static bool sun4i_i2s_rd_reg(struct device *dev, unsigned int reg) |
| 619 | { |
| 620 | switch (reg) { |
| 621 | case SUN4I_I2S_FIFO_TX_REG: |
| 622 | return false; |
| 623 | |
| 624 | default: |
| 625 | return true; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static bool sun4i_i2s_wr_reg(struct device *dev, unsigned int reg) |
| 630 | { |
| 631 | switch (reg) { |
| 632 | case SUN4I_I2S_FIFO_RX_REG: |
| 633 | case SUN4I_I2S_FIFO_STA_REG: |
| 634 | return false; |
| 635 | |
| 636 | default: |
| 637 | return true; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static bool sun4i_i2s_volatile_reg(struct device *dev, unsigned int reg) |
| 642 | { |
| 643 | switch (reg) { |
| 644 | case SUN4I_I2S_FIFO_RX_REG: |
| 645 | case SUN4I_I2S_INT_STA_REG: |
| 646 | case SUN4I_I2S_RX_CNT_REG: |
| 647 | case SUN4I_I2S_TX_CNT_REG: |
| 648 | return true; |
| 649 | |
| 650 | default: |
| 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static const struct reg_default sun4i_i2s_reg_defaults[] = { |
| 656 | { SUN4I_I2S_CTRL_REG, 0x00000000 }, |
| 657 | { SUN4I_I2S_FMT0_REG, 0x0000000c }, |
| 658 | { SUN4I_I2S_FMT1_REG, 0x00004020 }, |
| 659 | { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 }, |
| 660 | { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 }, |
| 661 | { SUN4I_I2S_CLK_DIV_REG, 0x00000000 }, |
| 662 | { SUN4I_I2S_TX_CHAN_SEL_REG, 0x00000001 }, |
| 663 | { SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210 }, |
| 664 | { SUN4I_I2S_RX_CHAN_SEL_REG, 0x00000001 }, |
| 665 | { SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210 }, |
| 666 | }; |
| 667 | |
| 668 | static const struct regmap_config sun4i_i2s_regmap_config = { |
| 669 | .reg_bits = 32, |
| 670 | .reg_stride = 4, |
| 671 | .val_bits = 32, |
| 672 | .max_register = SUN4I_I2S_RX_CHAN_MAP_REG, |
| 673 | |
| 674 | .cache_type = REGCACHE_FLAT, |
| 675 | .reg_defaults = sun4i_i2s_reg_defaults, |
| 676 | .num_reg_defaults = ARRAY_SIZE(sun4i_i2s_reg_defaults), |
| 677 | .writeable_reg = sun4i_i2s_wr_reg, |
| 678 | .readable_reg = sun4i_i2s_rd_reg, |
| 679 | .volatile_reg = sun4i_i2s_volatile_reg, |
| 680 | }; |
| 681 | |
| 682 | static int sun4i_i2s_runtime_resume(struct device *dev) |
| 683 | { |
| 684 | struct sun4i_i2s *i2s = dev_get_drvdata(dev); |
| 685 | int ret; |
| 686 | |
| 687 | ret = clk_prepare_enable(i2s->bus_clk); |
| 688 | if (ret) { |
| 689 | dev_err(dev, "Failed to enable bus clock\n"); |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | regcache_cache_only(i2s->regmap, false); |
| 694 | regcache_mark_dirty(i2s->regmap); |
| 695 | |
| 696 | ret = regcache_sync(i2s->regmap); |
| 697 | if (ret) { |
| 698 | dev_err(dev, "Failed to sync regmap cache\n"); |
| 699 | goto err_disable_clk; |
| 700 | } |
| 701 | |
| 702 | return 0; |
| 703 | |
| 704 | err_disable_clk: |
| 705 | clk_disable_unprepare(i2s->bus_clk); |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | static int sun4i_i2s_runtime_suspend(struct device *dev) |
| 710 | { |
| 711 | struct sun4i_i2s *i2s = dev_get_drvdata(dev); |
| 712 | |
| 713 | regcache_cache_only(i2s->regmap, true); |
| 714 | |
| 715 | clk_disable_unprepare(i2s->bus_clk); |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 720 | static const struct sun4i_i2s_quirks sun4i_a10_i2s_quirks = { |
Marcus Cooper | cd1c63d | 2017-08-12 13:00:50 +0200 | [diff] [blame] | 721 | .has_reset = false, |
Marcus Cooper | 3509476 | 2017-08-12 13:00:51 +0200 | [diff] [blame] | 722 | .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG, |
Marcus Cooper | cd1c63d | 2017-08-12 13:00:50 +0200 | [diff] [blame] | 723 | .sun4i_i2s_regmap = &sun4i_i2s_regmap_config, |
Marcus Cooper | 5f93b06 | 2017-08-19 14:48:35 +0200 | [diff] [blame] | 724 | .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7), |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 725 | .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3), |
| 726 | .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5), |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 727 | .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6), |
| 728 | .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7), |
Marcus Cooper | d03d273 | 2017-08-19 14:48:37 +0200 | [diff] [blame^] | 729 | .has_slave_select_bit = true, |
Marcus Cooper | dfd2293 | 2017-08-19 14:48:36 +0200 | [diff] [blame] | 730 | .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1), |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 731 | .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31), |
| 732 | .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31), |
| 733 | .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2), |
| 734 | .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2), |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | static const struct sun4i_i2s_quirks sun6i_a31_i2s_quirks = { |
Marcus Cooper | cd1c63d | 2017-08-12 13:00:50 +0200 | [diff] [blame] | 738 | .has_reset = true, |
Marcus Cooper | 3509476 | 2017-08-12 13:00:51 +0200 | [diff] [blame] | 739 | .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG, |
Marcus Cooper | cd1c63d | 2017-08-12 13:00:50 +0200 | [diff] [blame] | 740 | .sun4i_i2s_regmap = &sun4i_i2s_regmap_config, |
Marcus Cooper | 5f93b06 | 2017-08-19 14:48:35 +0200 | [diff] [blame] | 741 | .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7), |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 742 | .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3), |
| 743 | .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5), |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 744 | .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6), |
| 745 | .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7), |
Marcus Cooper | d03d273 | 2017-08-19 14:48:37 +0200 | [diff] [blame^] | 746 | .has_slave_select_bit = true, |
Marcus Cooper | dfd2293 | 2017-08-19 14:48:36 +0200 | [diff] [blame] | 747 | .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1), |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 748 | .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31), |
| 749 | .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31), |
| 750 | .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2), |
| 751 | .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2), |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 752 | }; |
| 753 | |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 754 | static int sun4i_i2s_init_regmap_fields(struct device *dev, |
| 755 | struct sun4i_i2s *i2s) |
| 756 | { |
Marcus Cooper | 5f93b06 | 2017-08-19 14:48:35 +0200 | [diff] [blame] | 757 | i2s->field_clkdiv_mclk_en = |
| 758 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 759 | i2s->variant->field_clkdiv_mclk_en); |
| 760 | if (IS_ERR(i2s->field_clkdiv_mclk_en)) |
| 761 | return PTR_ERR(i2s->field_clkdiv_mclk_en); |
| 762 | |
Marcus Cooper | 7716471 | 2017-08-19 14:48:33 +0200 | [diff] [blame] | 763 | i2s->field_fmt_wss = |
| 764 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 765 | i2s->variant->field_fmt_wss); |
| 766 | if (IS_ERR(i2s->field_fmt_wss)) |
| 767 | return PTR_ERR(i2s->field_fmt_wss); |
| 768 | |
| 769 | i2s->field_fmt_sr = |
| 770 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 771 | i2s->variant->field_fmt_sr); |
| 772 | if (IS_ERR(i2s->field_fmt_sr)) |
| 773 | return PTR_ERR(i2s->field_fmt_sr); |
| 774 | |
Marcus Cooper | 2969352 | 2017-08-19 14:48:34 +0200 | [diff] [blame] | 775 | i2s->field_fmt_bclk = |
| 776 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 777 | i2s->variant->field_fmt_bclk); |
| 778 | if (IS_ERR(i2s->field_fmt_bclk)) |
| 779 | return PTR_ERR(i2s->field_fmt_bclk); |
| 780 | |
| 781 | i2s->field_fmt_lrclk = |
| 782 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 783 | i2s->variant->field_fmt_lrclk); |
| 784 | if (IS_ERR(i2s->field_fmt_lrclk)) |
| 785 | return PTR_ERR(i2s->field_fmt_lrclk); |
| 786 | |
Marcus Cooper | dfd2293 | 2017-08-19 14:48:36 +0200 | [diff] [blame] | 787 | i2s->field_fmt_mode = |
| 788 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 789 | i2s->variant->field_fmt_mode); |
| 790 | if (IS_ERR(i2s->field_fmt_mode)) |
| 791 | return PTR_ERR(i2s->field_fmt_mode); |
| 792 | |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 793 | i2s->field_txchanmap = |
| 794 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 795 | i2s->variant->field_txchanmap); |
| 796 | if (IS_ERR(i2s->field_txchanmap)) |
| 797 | return PTR_ERR(i2s->field_txchanmap); |
| 798 | |
| 799 | i2s->field_rxchanmap = |
| 800 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 801 | i2s->variant->field_rxchanmap); |
| 802 | if (IS_ERR(i2s->field_rxchanmap)) |
| 803 | return PTR_ERR(i2s->field_rxchanmap); |
| 804 | |
| 805 | i2s->field_txchansel = |
| 806 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 807 | i2s->variant->field_txchansel); |
| 808 | if (IS_ERR(i2s->field_txchansel)) |
| 809 | return PTR_ERR(i2s->field_txchansel); |
| 810 | |
| 811 | i2s->field_rxchansel = |
| 812 | devm_regmap_field_alloc(dev, i2s->regmap, |
| 813 | i2s->variant->field_rxchansel); |
| 814 | return PTR_ERR_OR_ZERO(i2s->field_rxchansel); |
| 815 | } |
| 816 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 817 | static int sun4i_i2s_probe(struct platform_device *pdev) |
| 818 | { |
| 819 | struct sun4i_i2s *i2s; |
| 820 | struct resource *res; |
| 821 | void __iomem *regs; |
| 822 | int irq, ret; |
| 823 | |
| 824 | i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL); |
| 825 | if (!i2s) |
| 826 | return -ENOMEM; |
| 827 | platform_set_drvdata(pdev, i2s); |
| 828 | |
| 829 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 830 | regs = devm_ioremap_resource(&pdev->dev, res); |
Wei Yongjun | 62ee4ec | 2016-07-04 15:08:07 +0000 | [diff] [blame] | 831 | if (IS_ERR(regs)) |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 832 | return PTR_ERR(regs); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 833 | |
| 834 | irq = platform_get_irq(pdev, 0); |
| 835 | if (irq < 0) { |
| 836 | dev_err(&pdev->dev, "Can't retrieve our interrupt\n"); |
| 837 | return irq; |
| 838 | } |
| 839 | |
Marcus Cooper | 47bea0c | 2017-07-29 16:17:42 +0200 | [diff] [blame] | 840 | i2s->variant = of_device_get_match_data(&pdev->dev); |
| 841 | if (!i2s->variant) { |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 842 | dev_err(&pdev->dev, "Failed to determine the quirks to use\n"); |
| 843 | return -ENODEV; |
| 844 | } |
| 845 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 846 | i2s->bus_clk = devm_clk_get(&pdev->dev, "apb"); |
| 847 | if (IS_ERR(i2s->bus_clk)) { |
| 848 | dev_err(&pdev->dev, "Can't get our bus clock\n"); |
| 849 | return PTR_ERR(i2s->bus_clk); |
| 850 | } |
| 851 | |
| 852 | i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs, |
Marcus Cooper | cd1c63d | 2017-08-12 13:00:50 +0200 | [diff] [blame] | 853 | i2s->variant->sun4i_i2s_regmap); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 854 | if (IS_ERR(i2s->regmap)) { |
| 855 | dev_err(&pdev->dev, "Regmap initialisation failed\n"); |
| 856 | return PTR_ERR(i2s->regmap); |
kbuild test robot | dcf7d19 | 2016-06-30 22:28:10 +0800 | [diff] [blame] | 857 | } |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 858 | |
| 859 | i2s->mod_clk = devm_clk_get(&pdev->dev, "mod"); |
| 860 | if (IS_ERR(i2s->mod_clk)) { |
| 861 | dev_err(&pdev->dev, "Can't get our mod clock\n"); |
| 862 | return PTR_ERR(i2s->mod_clk); |
| 863 | } |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 864 | |
Marcus Cooper | 47bea0c | 2017-07-29 16:17:42 +0200 | [diff] [blame] | 865 | if (i2s->variant->has_reset) { |
Philipp Zabel | 72bfa21 | 2017-07-19 17:26:43 +0200 | [diff] [blame] | 866 | i2s->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL); |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 867 | if (IS_ERR(i2s->rst)) { |
| 868 | dev_err(&pdev->dev, "Failed to get reset control\n"); |
| 869 | return PTR_ERR(i2s->rst); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | if (!IS_ERR(i2s->rst)) { |
| 874 | ret = reset_control_deassert(i2s->rst); |
| 875 | if (ret) { |
| 876 | dev_err(&pdev->dev, |
| 877 | "Failed to deassert the reset control\n"); |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | } |
| 881 | |
Marcus Cooper | 3509476 | 2017-08-12 13:00:51 +0200 | [diff] [blame] | 882 | i2s->playback_dma_data.addr = res->start + |
| 883 | i2s->variant->reg_offset_txdata; |
Mylène Josserand | ebad64d | 2017-01-17 15:02:21 +0100 | [diff] [blame] | 884 | i2s->playback_dma_data.maxburst = 8; |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 885 | |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 886 | i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG; |
Mylène Josserand | ebad64d | 2017-01-17 15:02:21 +0100 | [diff] [blame] | 887 | i2s->capture_dma_data.maxburst = 8; |
Maxime Ripard | ae73b34 | 2016-11-03 17:27:05 +0100 | [diff] [blame] | 888 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 889 | pm_runtime_enable(&pdev->dev); |
| 890 | if (!pm_runtime_enabled(&pdev->dev)) { |
| 891 | ret = sun4i_i2s_runtime_resume(&pdev->dev); |
| 892 | if (ret) |
| 893 | goto err_pm_disable; |
| 894 | } |
| 895 | |
| 896 | ret = devm_snd_soc_register_component(&pdev->dev, |
| 897 | &sun4i_i2s_component, |
| 898 | &sun4i_i2s_dai, 1); |
| 899 | if (ret) { |
| 900 | dev_err(&pdev->dev, "Could not register DAI\n"); |
| 901 | goto err_suspend; |
| 902 | } |
| 903 | |
| 904 | ret = snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); |
| 905 | if (ret) { |
| 906 | dev_err(&pdev->dev, "Could not register PCM\n"); |
| 907 | goto err_suspend; |
| 908 | } |
| 909 | |
Marcus Cooper | 6eb4f27 | 2017-08-19 14:48:32 +0200 | [diff] [blame] | 910 | ret = sun4i_i2s_init_regmap_fields(&pdev->dev, i2s); |
| 911 | if (ret) { |
| 912 | dev_err(&pdev->dev, "Could not initialise regmap fields\n"); |
| 913 | goto err_suspend; |
| 914 | } |
| 915 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 916 | return 0; |
| 917 | |
| 918 | err_suspend: |
| 919 | if (!pm_runtime_status_suspended(&pdev->dev)) |
| 920 | sun4i_i2s_runtime_suspend(&pdev->dev); |
| 921 | err_pm_disable: |
| 922 | pm_runtime_disable(&pdev->dev); |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 923 | if (!IS_ERR(i2s->rst)) |
| 924 | reset_control_assert(i2s->rst); |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 925 | |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | static int sun4i_i2s_remove(struct platform_device *pdev) |
| 930 | { |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 931 | struct sun4i_i2s *i2s = dev_get_drvdata(&pdev->dev); |
| 932 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 933 | snd_dmaengine_pcm_unregister(&pdev->dev); |
| 934 | |
| 935 | pm_runtime_disable(&pdev->dev); |
| 936 | if (!pm_runtime_status_suspended(&pdev->dev)) |
| 937 | sun4i_i2s_runtime_suspend(&pdev->dev); |
| 938 | |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 939 | if (!IS_ERR(i2s->rst)) |
| 940 | reset_control_assert(i2s->rst); |
| 941 | |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static const struct of_device_id sun4i_i2s_match[] = { |
Mylène Josserand | 2ad6f30 | 2017-02-02 10:24:16 +0100 | [diff] [blame] | 946 | { |
| 947 | .compatible = "allwinner,sun4i-a10-i2s", |
| 948 | .data = &sun4i_a10_i2s_quirks, |
| 949 | }, |
| 950 | { |
| 951 | .compatible = "allwinner,sun6i-a31-i2s", |
| 952 | .data = &sun6i_a31_i2s_quirks, |
| 953 | }, |
Maxime Ripard | fa7c0d1 | 2016-06-15 23:11:21 +0200 | [diff] [blame] | 954 | {} |
| 955 | }; |
| 956 | MODULE_DEVICE_TABLE(of, sun4i_i2s_match); |
| 957 | |
| 958 | static const struct dev_pm_ops sun4i_i2s_pm_ops = { |
| 959 | .runtime_resume = sun4i_i2s_runtime_resume, |
| 960 | .runtime_suspend = sun4i_i2s_runtime_suspend, |
| 961 | }; |
| 962 | |
| 963 | static struct platform_driver sun4i_i2s_driver = { |
| 964 | .probe = sun4i_i2s_probe, |
| 965 | .remove = sun4i_i2s_remove, |
| 966 | .driver = { |
| 967 | .name = "sun4i-i2s", |
| 968 | .of_match_table = sun4i_i2s_match, |
| 969 | .pm = &sun4i_i2s_pm_ops, |
| 970 | }, |
| 971 | }; |
| 972 | module_platform_driver(sun4i_i2s_driver); |
| 973 | |
| 974 | MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>"); |
| 975 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 976 | MODULE_DESCRIPTION("Allwinner A10 I2S driver"); |
| 977 | MODULE_LICENSE("GPL"); |