blob: 573acca24a2c6f9f95256d3ab4300539abc737c3 [file] [log] [blame]
Maxime Ripardfa7c0d12016-06-15 23:11:21 +02001/*
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 Josserand2ad6f302017-02-02 10:24:16 +010017#include <linux/of_device.h>
Maxime Ripardfa7c0d12016-06-15 23:11:21 +020018#include <linux/platform_device.h>
19#include <linux/pm_runtime.h>
20#include <linux/regmap.h>
Mylène Josserand2ad6f302017-02-02 10:24:16 +010021#include <linux/reset.h>
Maxime Ripardfa7c0d12016-06-15 23:11:21 +020022
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 Cooper29693522017-08-19 14:48:34 +020053#define SUN4I_I2S_FMT0_POLARITY_INVERTED (1)
54#define SUN4I_I2S_FMT0_POLARITY_NORMAL (0)
Maxime Ripardfa7c0d12016-06-15 23:11:21 +020055
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 Cooper6eb4f272017-08-19 14:48:32 +020087#define SUN4I_I2S_CHAN_SEL(num_chan) (((num_chan) - 1) << 0)
Maxime Ripardfa7c0d12016-06-15 23:11:21 +020088
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 Cooper47bea0c2017-07-29 16:17:42 +020095/**
96 * struct sun4i_i2s_quirks - Differences between SoC variants.
97 *
98 * @has_reset: SoC needs reset deasserted.
Marcus Cooperd03d2732017-08-19 14:48:37 +020099 * @has_slave_select_bit: SoC has a bit to enable slave mode.
Marcus Cooper35094762017-08-12 13:00:51 +0200100 * @reg_offset_txdata: offset of the tx fifo.
Marcus Coopercd1c63d2017-08-12 13:00:50 +0200101 * @sun4i_i2s_regmap: regmap config to use.
Marcus Cooper0aef27c2017-08-12 13:00:49 +0200102 * @mclk_offset: Value by which mclkdiv needs to be adjusted.
103 * @bclk_offset: Value by which bclkdiv needs to be adjusted.
Marcus Cooper77164712017-08-19 14:48:33 +0200104 * @fmt_offset: Value by which wss and sr needs to be adjusted.
Marcus Cooper5f93b062017-08-19 14:48:35 +0200105 * @field_clkdiv_mclk_en: regmap field to enable mclk output.
Marcus Cooper77164712017-08-19 14:48:33 +0200106 * @field_fmt_wss: regmap field to set word select size.
107 * @field_fmt_sr: regmap field to set sample resolution.
Marcus Cooper29693522017-08-19 14:48:34 +0200108 * @field_fmt_bclk: regmap field to set clk polarity.
109 * @field_fmt_lrclk: regmap field to set frame polarity.
Marcus Cooperdfd22932017-08-19 14:48:36 +0200110 * @field_fmt_mode: regmap field to set the operational mode.
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200111 * @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 Cooper47bea0c2017-07-29 16:17:42 +0200115 */
116struct sun4i_i2s_quirks {
117 bool has_reset;
Marcus Cooperd03d2732017-08-19 14:48:37 +0200118 bool has_slave_select_bit;
Marcus Cooper35094762017-08-12 13:00:51 +0200119 unsigned int reg_offset_txdata; /* TX FIFO */
Marcus Coopercd1c63d2017-08-12 13:00:50 +0200120 const struct regmap_config *sun4i_i2s_regmap;
Marcus Cooper0aef27c2017-08-12 13:00:49 +0200121 unsigned int mclk_offset;
122 unsigned int bclk_offset;
Marcus Cooper77164712017-08-19 14:48:33 +0200123 unsigned int fmt_offset;
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200124
125 /* Register fields for i2s */
Marcus Cooper5f93b062017-08-19 14:48:35 +0200126 struct reg_field field_clkdiv_mclk_en;
Marcus Cooper77164712017-08-19 14:48:33 +0200127 struct reg_field field_fmt_wss;
128 struct reg_field field_fmt_sr;
Marcus Cooper29693522017-08-19 14:48:34 +0200129 struct reg_field field_fmt_bclk;
130 struct reg_field field_fmt_lrclk;
Marcus Cooperdfd22932017-08-19 14:48:36 +0200131 struct reg_field field_fmt_mode;
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200132 struct reg_field field_txchanmap;
133 struct reg_field field_rxchanmap;
134 struct reg_field field_txchansel;
135 struct reg_field field_rxchansel;
Marcus Cooper47bea0c2017-07-29 16:17:42 +0200136};
137
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200138struct sun4i_i2s {
139 struct clk *bus_clk;
140 struct clk *mod_clk;
141 struct regmap *regmap;
Mylène Josserand2ad6f302017-02-02 10:24:16 +0100142 struct reset_control *rst;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200143
Maxime Ripardb2b7b562016-11-07 14:08:19 +0100144 unsigned int mclk_freq;
145
Maxime Ripardae73b342016-11-03 17:27:05 +0100146 struct snd_dmaengine_dai_dma_data capture_dma_data;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200147 struct snd_dmaengine_dai_dma_data playback_dma_data;
Marcus Cooper47bea0c2017-07-29 16:17:42 +0200148
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200149 /* Register fields for i2s */
Marcus Cooper5f93b062017-08-19 14:48:35 +0200150 struct regmap_field *field_clkdiv_mclk_en;
Marcus Cooper77164712017-08-19 14:48:33 +0200151 struct regmap_field *field_fmt_wss;
152 struct regmap_field *field_fmt_sr;
Marcus Cooper29693522017-08-19 14:48:34 +0200153 struct regmap_field *field_fmt_bclk;
154 struct regmap_field *field_fmt_lrclk;
Marcus Cooperdfd22932017-08-19 14:48:36 +0200155 struct regmap_field *field_fmt_mode;
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200156 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 Cooper47bea0c2017-07-29 16:17:42 +0200161 const struct sun4i_i2s_quirks *variant;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200162};
163
164struct sun4i_i2s_clk_div {
165 u8 div;
166 u8 val;
167};
168
169static 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
178static 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
189static 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
206static 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
224static int sun4i_i2s_oversample_rates[] = { 128, 192, 256, 384, 512, 768 };
Maxime Ripardb2b7b562016-11-07 14:08:19 +0100225static 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 Ripardfa7c0d12016-06-15 23:11:21 +0200235
236static int sun4i_i2s_set_clk_rate(struct sun4i_i2s *i2s,
237 unsigned int rate,
238 unsigned int word_size)
239{
Maxime Ripardb2b7b562016-11-07 14:08:19 +0100240 unsigned int oversample_rate, clk_rate;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200241 int bclk_div, mclk_div;
Maxime Ripardb2b7b562016-11-07 14:08:19 +0100242 int ret;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200243
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 Ripardb2b7b562016-11-07 14:08:19 +0100274 oversample_rate = i2s->mclk_freq / rate;
275 if (!sun4i_i2s_oversample_is_valid(oversample_rate))
276 return -EINVAL;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200277
Maxime Ripardb2b7b562016-11-07 14:08:19 +0100278 bclk_div = sun4i_i2s_get_bclk_div(i2s, oversample_rate,
279 word_size);
280 if (bclk_div < 0)
281 return -EINVAL;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200282
Maxime Ripardb2b7b562016-11-07 14:08:19 +0100283 mclk_div = sun4i_i2s_get_mclk_div(i2s, oversample_rate,
284 clk_rate, rate);
285 if (mclk_div < 0)
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200286 return -EINVAL;
287
Marcus Cooper0aef27c2017-08-12 13:00:49 +0200288 /* Adjust the clock division values if needed */
289 bclk_div += i2s->variant->bclk_offset;
290 mclk_div += i2s->variant->mclk_offset;
291
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200292 regmap_write(i2s->regmap, SUN4I_I2S_CLK_DIV_REG,
293 SUN4I_I2S_CLK_DIV_BCLK(bclk_div) |
Marcus Cooper5f93b062017-08-19 14:48:35 +0200294 SUN4I_I2S_CLK_DIV_MCLK(mclk_div));
295
296 regmap_field_write(i2s->field_clkdiv_mclk_en, 1);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200297
298 return 0;
299}
300
301static 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 Cooper6eb4f272017-08-19 14:48:32 +0200312 /* 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 Ripardfa7c0d12016-06-15 23:11:21 +0200323 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 Cooper77164712017-08-19 14:48:33 +0200342 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 Ripardfa7c0d12016-06-15 23:11:21 +0200346
347 return sun4i_i2s_set_clk_rate(i2s, params_rate(params),
348 params_width(params));
349}
350
351static 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 Cooper29693522017-08-19 14:48:34 +0200355 u32 bclk_polarity = SUN4I_I2S_FMT0_POLARITY_NORMAL;
356 u32 lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_NORMAL;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200357
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 Cooperdfd22932017-08-19 14:48:36 +0200373 regmap_field_write(i2s->field_fmt_mode, val);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200374
375 /* DAI clock polarity */
376 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
377 case SND_SOC_DAIFMT_IB_IF:
378 /* Invert both clocks */
Marcus Cooper29693522017-08-19 14:48:34 +0200379 bclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
380 lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200381 break;
382 case SND_SOC_DAIFMT_IB_NF:
383 /* Invert bit clock */
Marcus Cooper29693522017-08-19 14:48:34 +0200384 bclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200385 break;
386 case SND_SOC_DAIFMT_NB_IF:
387 /* Invert frame clock */
Marcus Cooper29693522017-08-19 14:48:34 +0200388 lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200389 break;
390 case SND_SOC_DAIFMT_NB_NF:
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200391 break;
392 default:
393 return -EINVAL;
394 }
395
Marcus Cooper29693522017-08-19 14:48:34 +0200396 regmap_field_write(i2s->field_fmt_bclk, bclk_polarity);
397 regmap_field_write(i2s->field_fmt_lrclk, lrclk_polarity);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200398
Marcus Cooperd03d2732017-08-19 14:48:37 +0200399 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 Ripardfa7c0d12016-06-15 23:11:21 +0200416 }
417
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200418 /* 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 Ripardae73b342016-11-03 17:27:05 +0100427static 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 Ripardfa7c0d12016-06-15 23:11:21 +0200448static 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 Ripardae73b342016-11-03 17:27:05 +0100469static 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 Ripardfa7c0d12016-06-15 23:11:21 +0200481
482static 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
495static 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 Ripardae73b342016-11-03 17:27:05 +0100507 sun4i_i2s_start_capture(i2s);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200508 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 Ripardae73b342016-11-03 17:27:05 +0100516 sun4i_i2s_stop_capture(i2s);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200517 break;
518
519 default:
520 return -EINVAL;
521 }
522
523 return 0;
524}
525
526static 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 Ripardfa7c0d12016-06-15 23:11:21 +0200540
541 return clk_prepare_enable(i2s->mod_clk);
542}
543
544static 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 Ripardb2b7b562016-11-07 14:08:19 +0100559static 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 Ripardfa7c0d12016-06-15 23:11:21 +0200572static 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 Ripardb2b7b562016-11-07 14:08:19 +0100575 .set_sysclk = sun4i_i2s_set_sysclk,
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200576 .shutdown = sun4i_i2s_shutdown,
577 .startup = sun4i_i2s_startup,
578 .trigger = sun4i_i2s_trigger,
579};
580
581static int sun4i_i2s_dai_probe(struct snd_soc_dai *dai)
582{
583 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
584
Maxime Ripardae73b342016-11-03 17:27:05 +0100585 snd_soc_dai_init_dma_data(dai,
586 &i2s->playback_dma_data,
587 &i2s->capture_dma_data);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200588
589 snd_soc_dai_set_drvdata(dai, i2s);
590
591 return 0;
592}
593
594static struct snd_soc_dai_driver sun4i_i2s_dai = {
595 .probe = sun4i_i2s_dai_probe,
Maxime Ripardae73b342016-11-03 17:27:05 +0100596 .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 Ripardfa7c0d12016-06-15 23:11:21 +0200603 .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
614static const struct snd_soc_component_driver sun4i_i2s_component = {
615 .name = "sun4i-dai",
616};
617
618static 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
629static 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
641static 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
655static 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
668static 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
682static 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
704err_disable_clk:
705 clk_disable_unprepare(i2s->bus_clk);
706 return ret;
707}
708
709static 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 Josserand2ad6f302017-02-02 10:24:16 +0100720static const struct sun4i_i2s_quirks sun4i_a10_i2s_quirks = {
Marcus Coopercd1c63d2017-08-12 13:00:50 +0200721 .has_reset = false,
Marcus Cooper35094762017-08-12 13:00:51 +0200722 .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
Marcus Coopercd1c63d2017-08-12 13:00:50 +0200723 .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
Marcus Cooper5f93b062017-08-19 14:48:35 +0200724 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
Marcus Cooper77164712017-08-19 14:48:33 +0200725 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
726 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
Marcus Cooper29693522017-08-19 14:48:34 +0200727 .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
728 .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
Marcus Cooperd03d2732017-08-19 14:48:37 +0200729 .has_slave_select_bit = true,
Marcus Cooperdfd22932017-08-19 14:48:36 +0200730 .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200731 .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 Josserand2ad6f302017-02-02 10:24:16 +0100735};
736
737static const struct sun4i_i2s_quirks sun6i_a31_i2s_quirks = {
Marcus Coopercd1c63d2017-08-12 13:00:50 +0200738 .has_reset = true,
Marcus Cooper35094762017-08-12 13:00:51 +0200739 .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
Marcus Coopercd1c63d2017-08-12 13:00:50 +0200740 .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
Marcus Cooper5f93b062017-08-19 14:48:35 +0200741 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
Marcus Cooper77164712017-08-19 14:48:33 +0200742 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
743 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
Marcus Cooper29693522017-08-19 14:48:34 +0200744 .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
745 .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
Marcus Cooperd03d2732017-08-19 14:48:37 +0200746 .has_slave_select_bit = true,
Marcus Cooperdfd22932017-08-19 14:48:36 +0200747 .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200748 .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 Josserand2ad6f302017-02-02 10:24:16 +0100752};
753
Marcus Cooper6eb4f272017-08-19 14:48:32 +0200754static int sun4i_i2s_init_regmap_fields(struct device *dev,
755 struct sun4i_i2s *i2s)
756{
Marcus Cooper5f93b062017-08-19 14:48:35 +0200757 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 Cooper77164712017-08-19 14:48:33 +0200763 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 Cooper29693522017-08-19 14:48:34 +0200775 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 Cooperdfd22932017-08-19 14:48:36 +0200787 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 Cooper6eb4f272017-08-19 14:48:32 +0200793 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 Ripardfa7c0d12016-06-15 23:11:21 +0200817static 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 Yongjun62ee4ec2016-07-04 15:08:07 +0000831 if (IS_ERR(regs))
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200832 return PTR_ERR(regs);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200833
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 Cooper47bea0c2017-07-29 16:17:42 +0200840 i2s->variant = of_device_get_match_data(&pdev->dev);
841 if (!i2s->variant) {
Mylène Josserand2ad6f302017-02-02 10:24:16 +0100842 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
843 return -ENODEV;
844 }
845
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200846 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 Coopercd1c63d2017-08-12 13:00:50 +0200853 i2s->variant->sun4i_i2s_regmap);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200854 if (IS_ERR(i2s->regmap)) {
855 dev_err(&pdev->dev, "Regmap initialisation failed\n");
856 return PTR_ERR(i2s->regmap);
kbuild test robotdcf7d192016-06-30 22:28:10 +0800857 }
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200858
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 Josserand2ad6f302017-02-02 10:24:16 +0100864
Marcus Cooper47bea0c2017-07-29 16:17:42 +0200865 if (i2s->variant->has_reset) {
Philipp Zabel72bfa212017-07-19 17:26:43 +0200866 i2s->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
Mylène Josserand2ad6f302017-02-02 10:24:16 +0100867 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 Cooper35094762017-08-12 13:00:51 +0200882 i2s->playback_dma_data.addr = res->start +
883 i2s->variant->reg_offset_txdata;
Mylène Josserandebad64d2017-01-17 15:02:21 +0100884 i2s->playback_dma_data.maxburst = 8;
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200885
Maxime Ripardae73b342016-11-03 17:27:05 +0100886 i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG;
Mylène Josserandebad64d2017-01-17 15:02:21 +0100887 i2s->capture_dma_data.maxburst = 8;
Maxime Ripardae73b342016-11-03 17:27:05 +0100888
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200889 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 Cooper6eb4f272017-08-19 14:48:32 +0200910 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 Ripardfa7c0d12016-06-15 23:11:21 +0200916 return 0;
917
918err_suspend:
919 if (!pm_runtime_status_suspended(&pdev->dev))
920 sun4i_i2s_runtime_suspend(&pdev->dev);
921err_pm_disable:
922 pm_runtime_disable(&pdev->dev);
Mylène Josserand2ad6f302017-02-02 10:24:16 +0100923 if (!IS_ERR(i2s->rst))
924 reset_control_assert(i2s->rst);
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200925
926 return ret;
927}
928
929static int sun4i_i2s_remove(struct platform_device *pdev)
930{
Mylène Josserand2ad6f302017-02-02 10:24:16 +0100931 struct sun4i_i2s *i2s = dev_get_drvdata(&pdev->dev);
932
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200933 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 Josserand2ad6f302017-02-02 10:24:16 +0100939 if (!IS_ERR(i2s->rst))
940 reset_control_assert(i2s->rst);
941
Maxime Ripardfa7c0d12016-06-15 23:11:21 +0200942 return 0;
943}
944
945static const struct of_device_id sun4i_i2s_match[] = {
Mylène Josserand2ad6f302017-02-02 10:24:16 +0100946 {
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 Ripardfa7c0d12016-06-15 23:11:21 +0200954 {}
955};
956MODULE_DEVICE_TABLE(of, sun4i_i2s_match);
957
958static 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
963static 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};
972module_platform_driver(sun4i_i2s_driver);
973
974MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
975MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
976MODULE_DESCRIPTION("Allwinner A10 I2S driver");
977MODULE_LICENSE("GPL");