18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Andrea Venturi
48c2ecf20Sopenharmony_ci * Andrea Venturi <be17068@iperbole.bo.it>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2016 Maxime Ripard
78c2ecf20Sopenharmony_ci * Maxime Ripard <maxime.ripard@free-electrons.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/clk.h>
118c2ecf20Sopenharmony_ci#include <linux/dmaengine.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/of_device.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
168c2ecf20Sopenharmony_ci#include <linux/regmap.h>
178c2ecf20Sopenharmony_ci#include <linux/reset.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <sound/dmaengine_pcm.h>
208c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
218c2ecf20Sopenharmony_ci#include <sound/soc.h>
228c2ecf20Sopenharmony_ci#include <sound/soc-dai.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_REG		0x00
258c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_SDO_EN_MASK		GENMASK(11, 8)
268c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_SDO_EN(sdo)			BIT(8 + (sdo))
278c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_MODE_MASK		BIT(5)
288c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_MODE_SLAVE			(1 << 5)
298c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_MODE_MASTER			(0 << 5)
308c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_TX_EN			BIT(2)
318c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_RX_EN			BIT(1)
328c2ecf20Sopenharmony_ci#define SUN4I_I2S_CTRL_GL_EN			BIT(0)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_REG		0x04
358c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK	BIT(7)
368c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED		(1 << 7)
378c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL		(0 << 7)
388c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_BCLK_POLARITY_MASK	BIT(6)
398c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED		(1 << 6)
408c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL		(0 << 6)
418c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_SR_MASK			GENMASK(5, 4)
428c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_SR(sr)				((sr) << 4)
438c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_WSS_MASK			GENMASK(3, 2)
448c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_WSS(wss)				((wss) << 2)
458c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_FMT_MASK			GENMASK(1, 0)
468c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_FMT_RIGHT_J			(2 << 0)
478c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_FMT_LEFT_J			(1 << 0)
488c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT0_FMT_I2S				(0 << 0)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define SUN4I_I2S_FMT1_REG		0x08
518c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_TX_REG		0x0c
528c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_RX_REG		0x10
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_CTRL_REG		0x14
558c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_CTRL_FLUSH_TX		BIT(25)
568c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_CTRL_FLUSH_RX		BIT(24)
578c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK	BIT(2)
588c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_CTRL_TX_MODE(mode)		((mode) << 2)
598c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK	GENMASK(1, 0)
608c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_CTRL_RX_MODE(mode)		(mode)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define SUN4I_I2S_FIFO_STA_REG		0x18
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define SUN4I_I2S_DMA_INT_CTRL_REG	0x1c
658c2ecf20Sopenharmony_ci#define SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN	BIT(7)
668c2ecf20Sopenharmony_ci#define SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN	BIT(3)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define SUN4I_I2S_INT_STA_REG		0x20
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define SUN4I_I2S_CLK_DIV_REG		0x24
718c2ecf20Sopenharmony_ci#define SUN4I_I2S_CLK_DIV_MCLK_EN		BIT(7)
728c2ecf20Sopenharmony_ci#define SUN4I_I2S_CLK_DIV_BCLK_MASK		GENMASK(6, 4)
738c2ecf20Sopenharmony_ci#define SUN4I_I2S_CLK_DIV_BCLK(bclk)			((bclk) << 4)
748c2ecf20Sopenharmony_ci#define SUN4I_I2S_CLK_DIV_MCLK_MASK		GENMASK(3, 0)
758c2ecf20Sopenharmony_ci#define SUN4I_I2S_CLK_DIV_MCLK(mclk)			((mclk) << 0)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define SUN4I_I2S_TX_CNT_REG		0x28
788c2ecf20Sopenharmony_ci#define SUN4I_I2S_RX_CNT_REG		0x2c
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define SUN4I_I2S_TX_CHAN_SEL_REG	0x30
818c2ecf20Sopenharmony_ci#define SUN4I_I2S_CHAN_SEL_MASK			GENMASK(2, 0)
828c2ecf20Sopenharmony_ci#define SUN4I_I2S_CHAN_SEL(num_chan)		(((num_chan) - 1) << 0)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define SUN4I_I2S_TX_CHAN_MAP_REG	0x34
858c2ecf20Sopenharmony_ci#define SUN4I_I2S_TX_CHAN_MAP(chan, sample)	((sample) << (chan << 2))
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define SUN4I_I2S_RX_CHAN_SEL_REG	0x38
888c2ecf20Sopenharmony_ci#define SUN4I_I2S_RX_CHAN_MAP_REG	0x3c
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* Defines required for sun8i-h3 support */
918c2ecf20Sopenharmony_ci#define SUN8I_I2S_CTRL_BCLK_OUT			BIT(18)
928c2ecf20Sopenharmony_ci#define SUN8I_I2S_CTRL_LRCK_OUT			BIT(17)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define SUN8I_I2S_CTRL_MODE_MASK		GENMASK(5, 4)
958c2ecf20Sopenharmony_ci#define SUN8I_I2S_CTRL_MODE_RIGHT		(2 << 4)
968c2ecf20Sopenharmony_ci#define SUN8I_I2S_CTRL_MODE_LEFT		(1 << 4)
978c2ecf20Sopenharmony_ci#define SUN8I_I2S_CTRL_MODE_PCM			(0 << 4)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK	BIT(19)
1008c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED		(1 << 19)
1018c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_LRCLK_POLARITY_NORMAL		(0 << 19)
1028c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_LRCK_PERIOD_MASK		GENMASK(17, 8)
1038c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_LRCK_PERIOD(period)	((period - 1) << 8)
1048c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_BCLK_POLARITY_MASK	BIT(7)
1058c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED		(1 << 7)
1068c2ecf20Sopenharmony_ci#define SUN8I_I2S_FMT0_BCLK_POLARITY_NORMAL		(0 << 7)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define SUN8I_I2S_INT_STA_REG		0x0c
1098c2ecf20Sopenharmony_ci#define SUN8I_I2S_FIFO_TX_REG		0x20
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define SUN8I_I2S_CHAN_CFG_REG		0x30
1128c2ecf20Sopenharmony_ci#define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK	GENMASK(6, 4)
1138c2ecf20Sopenharmony_ci#define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(chan)	((chan - 1) << 4)
1148c2ecf20Sopenharmony_ci#define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK	GENMASK(2, 0)
1158c2ecf20Sopenharmony_ci#define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(chan)	(chan - 1)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define SUN8I_I2S_TX_CHAN_MAP_REG	0x44
1188c2ecf20Sopenharmony_ci#define SUN8I_I2S_TX_CHAN_SEL_REG	0x34
1198c2ecf20Sopenharmony_ci#define SUN8I_I2S_TX_CHAN_OFFSET_MASK		GENMASK(13, 12)
1208c2ecf20Sopenharmony_ci#define SUN8I_I2S_TX_CHAN_OFFSET(offset)	(offset << 12)
1218c2ecf20Sopenharmony_ci#define SUN8I_I2S_TX_CHAN_EN_MASK		GENMASK(11, 4)
1228c2ecf20Sopenharmony_ci#define SUN8I_I2S_TX_CHAN_EN(num_chan)		(((1 << num_chan) - 1) << 4)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define SUN8I_I2S_RX_CHAN_SEL_REG	0x54
1258c2ecf20Sopenharmony_ci#define SUN8I_I2S_RX_CHAN_MAP_REG	0x58
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistruct sun4i_i2s;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/**
1308c2ecf20Sopenharmony_ci * struct sun4i_i2s_quirks - Differences between SoC variants.
1318c2ecf20Sopenharmony_ci * @has_reset: SoC needs reset deasserted.
1328c2ecf20Sopenharmony_ci * @reg_offset_txdata: offset of the tx fifo.
1338c2ecf20Sopenharmony_ci * @sun4i_i2s_regmap: regmap config to use.
1348c2ecf20Sopenharmony_ci * @field_clkdiv_mclk_en: regmap field to enable mclk output.
1358c2ecf20Sopenharmony_ci * @field_fmt_wss: regmap field to set word select size.
1368c2ecf20Sopenharmony_ci * @field_fmt_sr: regmap field to set sample resolution.
1378c2ecf20Sopenharmony_ci * @bclk_dividers: bit clock dividers array
1388c2ecf20Sopenharmony_ci * @num_bclk_dividers: number of bit clock dividers
1398c2ecf20Sopenharmony_ci * @mclk_dividers: mclk dividers array
1408c2ecf20Sopenharmony_ci * @num_mclk_dividers: number of mclk dividers
1418c2ecf20Sopenharmony_ci * @get_bclk_parent_rate: callback to get bclk parent rate
1428c2ecf20Sopenharmony_ci * @get_sr: callback to get sample resolution
1438c2ecf20Sopenharmony_ci * @get_wss: callback to get word select size
1448c2ecf20Sopenharmony_ci * @set_chan_cfg: callback to set channel configuration
1458c2ecf20Sopenharmony_ci * @set_fmt: callback to set format
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_cistruct sun4i_i2s_quirks {
1488c2ecf20Sopenharmony_ci	bool				has_reset;
1498c2ecf20Sopenharmony_ci	unsigned int			reg_offset_txdata;	/* TX FIFO */
1508c2ecf20Sopenharmony_ci	const struct regmap_config	*sun4i_i2s_regmap;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* Register fields for i2s */
1538c2ecf20Sopenharmony_ci	struct reg_field		field_clkdiv_mclk_en;
1548c2ecf20Sopenharmony_ci	struct reg_field		field_fmt_wss;
1558c2ecf20Sopenharmony_ci	struct reg_field		field_fmt_sr;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	const struct sun4i_i2s_clk_div	*bclk_dividers;
1588c2ecf20Sopenharmony_ci	unsigned int			num_bclk_dividers;
1598c2ecf20Sopenharmony_ci	const struct sun4i_i2s_clk_div	*mclk_dividers;
1608c2ecf20Sopenharmony_ci	unsigned int			num_mclk_dividers;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	unsigned long (*get_bclk_parent_rate)(const struct sun4i_i2s *);
1638c2ecf20Sopenharmony_ci	s8	(*get_sr)(const struct sun4i_i2s *, int);
1648c2ecf20Sopenharmony_ci	s8	(*get_wss)(const struct sun4i_i2s *, int);
1658c2ecf20Sopenharmony_ci	int	(*set_chan_cfg)(const struct sun4i_i2s *,
1668c2ecf20Sopenharmony_ci				const struct snd_pcm_hw_params *);
1678c2ecf20Sopenharmony_ci	int	(*set_fmt)(const struct sun4i_i2s *, unsigned int);
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistruct sun4i_i2s {
1718c2ecf20Sopenharmony_ci	struct clk	*bus_clk;
1728c2ecf20Sopenharmony_ci	struct clk	*mod_clk;
1738c2ecf20Sopenharmony_ci	struct regmap	*regmap;
1748c2ecf20Sopenharmony_ci	struct reset_control *rst;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	unsigned int	format;
1778c2ecf20Sopenharmony_ci	unsigned int	mclk_freq;
1788c2ecf20Sopenharmony_ci	unsigned int	slots;
1798c2ecf20Sopenharmony_ci	unsigned int	slot_width;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	struct snd_dmaengine_dai_dma_data	capture_dma_data;
1828c2ecf20Sopenharmony_ci	struct snd_dmaengine_dai_dma_data	playback_dma_data;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* Register fields for i2s */
1858c2ecf20Sopenharmony_ci	struct regmap_field	*field_clkdiv_mclk_en;
1868c2ecf20Sopenharmony_ci	struct regmap_field	*field_fmt_wss;
1878c2ecf20Sopenharmony_ci	struct regmap_field	*field_fmt_sr;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	const struct sun4i_i2s_quirks	*variant;
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistruct sun4i_i2s_clk_div {
1938c2ecf20Sopenharmony_ci	u8	div;
1948c2ecf20Sopenharmony_ci	u8	val;
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_clk_div sun4i_i2s_bclk_div[] = {
1988c2ecf20Sopenharmony_ci	{ .div = 2, .val = 0 },
1998c2ecf20Sopenharmony_ci	{ .div = 4, .val = 1 },
2008c2ecf20Sopenharmony_ci	{ .div = 6, .val = 2 },
2018c2ecf20Sopenharmony_ci	{ .div = 8, .val = 3 },
2028c2ecf20Sopenharmony_ci	{ .div = 12, .val = 4 },
2038c2ecf20Sopenharmony_ci	{ .div = 16, .val = 5 },
2048c2ecf20Sopenharmony_ci	/* TODO - extend divide ratio supported by newer SoCs */
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_clk_div sun4i_i2s_mclk_div[] = {
2088c2ecf20Sopenharmony_ci	{ .div = 1, .val = 0 },
2098c2ecf20Sopenharmony_ci	{ .div = 2, .val = 1 },
2108c2ecf20Sopenharmony_ci	{ .div = 4, .val = 2 },
2118c2ecf20Sopenharmony_ci	{ .div = 6, .val = 3 },
2128c2ecf20Sopenharmony_ci	{ .div = 8, .val = 4 },
2138c2ecf20Sopenharmony_ci	{ .div = 12, .val = 5 },
2148c2ecf20Sopenharmony_ci	{ .div = 16, .val = 6 },
2158c2ecf20Sopenharmony_ci	{ .div = 24, .val = 7 },
2168c2ecf20Sopenharmony_ci	/* TODO - extend divide ratio supported by newer SoCs */
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_clk_div sun8i_i2s_clk_div[] = {
2208c2ecf20Sopenharmony_ci	{ .div = 1, .val = 1 },
2218c2ecf20Sopenharmony_ci	{ .div = 2, .val = 2 },
2228c2ecf20Sopenharmony_ci	{ .div = 4, .val = 3 },
2238c2ecf20Sopenharmony_ci	{ .div = 6, .val = 4 },
2248c2ecf20Sopenharmony_ci	{ .div = 8, .val = 5 },
2258c2ecf20Sopenharmony_ci	{ .div = 12, .val = 6 },
2268c2ecf20Sopenharmony_ci	{ .div = 16, .val = 7 },
2278c2ecf20Sopenharmony_ci	{ .div = 24, .val = 8 },
2288c2ecf20Sopenharmony_ci	{ .div = 32, .val = 9 },
2298c2ecf20Sopenharmony_ci	{ .div = 48, .val = 10 },
2308c2ecf20Sopenharmony_ci	{ .div = 64, .val = 11 },
2318c2ecf20Sopenharmony_ci	{ .div = 96, .val = 12 },
2328c2ecf20Sopenharmony_ci	{ .div = 128, .val = 13 },
2338c2ecf20Sopenharmony_ci	{ .div = 176, .val = 14 },
2348c2ecf20Sopenharmony_ci	{ .div = 192, .val = 15 },
2358c2ecf20Sopenharmony_ci};
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic unsigned long sun4i_i2s_get_bclk_parent_rate(const struct sun4i_i2s *i2s)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	return i2s->mclk_freq;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic unsigned long sun8i_i2s_get_bclk_parent_rate(const struct sun4i_i2s *i2s)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	return clk_get_rate(i2s->mod_clk);
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic int sun4i_i2s_get_bclk_div(struct sun4i_i2s *i2s,
2488c2ecf20Sopenharmony_ci				  unsigned long parent_rate,
2498c2ecf20Sopenharmony_ci				  unsigned int sampling_rate,
2508c2ecf20Sopenharmony_ci				  unsigned int channels,
2518c2ecf20Sopenharmony_ci				  unsigned int word_size)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	const struct sun4i_i2s_clk_div *dividers = i2s->variant->bclk_dividers;
2548c2ecf20Sopenharmony_ci	int div = parent_rate / sampling_rate / word_size / channels;
2558c2ecf20Sopenharmony_ci	int i;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	for (i = 0; i < i2s->variant->num_bclk_dividers; i++) {
2588c2ecf20Sopenharmony_ci		const struct sun4i_i2s_clk_div *bdiv = &dividers[i];
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci		if (bdiv->div == div)
2618c2ecf20Sopenharmony_ci			return bdiv->val;
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	return -EINVAL;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic int sun4i_i2s_get_mclk_div(struct sun4i_i2s *i2s,
2688c2ecf20Sopenharmony_ci				  unsigned long parent_rate,
2698c2ecf20Sopenharmony_ci				  unsigned long mclk_rate)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	const struct sun4i_i2s_clk_div *dividers = i2s->variant->mclk_dividers;
2728c2ecf20Sopenharmony_ci	int div = parent_rate / mclk_rate;
2738c2ecf20Sopenharmony_ci	int i;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	for (i = 0; i < i2s->variant->num_mclk_dividers; i++) {
2768c2ecf20Sopenharmony_ci		const struct sun4i_i2s_clk_div *mdiv = &dividers[i];
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci		if (mdiv->div == div)
2798c2ecf20Sopenharmony_ci			return mdiv->val;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return -EINVAL;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic int sun4i_i2s_oversample_rates[] = { 128, 192, 256, 384, 512, 768 };
2868c2ecf20Sopenharmony_cistatic bool sun4i_i2s_oversample_is_valid(unsigned int oversample)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	int i;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sun4i_i2s_oversample_rates); i++)
2918c2ecf20Sopenharmony_ci		if (sun4i_i2s_oversample_rates[i] == oversample)
2928c2ecf20Sopenharmony_ci			return true;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return false;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic int sun4i_i2s_set_clk_rate(struct snd_soc_dai *dai,
2988c2ecf20Sopenharmony_ci				  unsigned int rate,
2998c2ecf20Sopenharmony_ci				  unsigned int slots,
3008c2ecf20Sopenharmony_ci				  unsigned int slot_width)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
3038c2ecf20Sopenharmony_ci	unsigned int oversample_rate, clk_rate, bclk_parent_rate;
3048c2ecf20Sopenharmony_ci	int bclk_div, mclk_div;
3058c2ecf20Sopenharmony_ci	int ret;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	switch (rate) {
3088c2ecf20Sopenharmony_ci	case 176400:
3098c2ecf20Sopenharmony_ci	case 88200:
3108c2ecf20Sopenharmony_ci	case 44100:
3118c2ecf20Sopenharmony_ci	case 22050:
3128c2ecf20Sopenharmony_ci	case 11025:
3138c2ecf20Sopenharmony_ci		clk_rate = 22579200;
3148c2ecf20Sopenharmony_ci		break;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	case 192000:
3178c2ecf20Sopenharmony_ci	case 128000:
3188c2ecf20Sopenharmony_ci	case 96000:
3198c2ecf20Sopenharmony_ci	case 64000:
3208c2ecf20Sopenharmony_ci	case 48000:
3218c2ecf20Sopenharmony_ci	case 32000:
3228c2ecf20Sopenharmony_ci	case 24000:
3238c2ecf20Sopenharmony_ci	case 16000:
3248c2ecf20Sopenharmony_ci	case 12000:
3258c2ecf20Sopenharmony_ci	case 8000:
3268c2ecf20Sopenharmony_ci		clk_rate = 24576000;
3278c2ecf20Sopenharmony_ci		break;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	default:
3308c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported sample rate: %u\n", rate);
3318c2ecf20Sopenharmony_ci		return -EINVAL;
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	ret = clk_set_rate(i2s->mod_clk, clk_rate);
3358c2ecf20Sopenharmony_ci	if (ret)
3368c2ecf20Sopenharmony_ci		return ret;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	oversample_rate = i2s->mclk_freq / rate;
3398c2ecf20Sopenharmony_ci	if (!sun4i_i2s_oversample_is_valid(oversample_rate)) {
3408c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported oversample rate: %d\n",
3418c2ecf20Sopenharmony_ci			oversample_rate);
3428c2ecf20Sopenharmony_ci		return -EINVAL;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	bclk_parent_rate = i2s->variant->get_bclk_parent_rate(i2s);
3468c2ecf20Sopenharmony_ci	bclk_div = sun4i_i2s_get_bclk_div(i2s, bclk_parent_rate,
3478c2ecf20Sopenharmony_ci					  rate, slots, slot_width);
3488c2ecf20Sopenharmony_ci	if (bclk_div < 0) {
3498c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported BCLK divider: %d\n", bclk_div);
3508c2ecf20Sopenharmony_ci		return -EINVAL;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	mclk_div = sun4i_i2s_get_mclk_div(i2s, clk_rate, i2s->mclk_freq);
3548c2ecf20Sopenharmony_ci	if (mclk_div < 0) {
3558c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported MCLK divider: %d\n", mclk_div);
3568c2ecf20Sopenharmony_ci		return -EINVAL;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	regmap_write(i2s->regmap, SUN4I_I2S_CLK_DIV_REG,
3608c2ecf20Sopenharmony_ci		     SUN4I_I2S_CLK_DIV_BCLK(bclk_div) |
3618c2ecf20Sopenharmony_ci		     SUN4I_I2S_CLK_DIV_MCLK(mclk_div));
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	regmap_field_write(i2s->field_clkdiv_mclk_en, 1);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	return 0;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic s8 sun4i_i2s_get_sr(const struct sun4i_i2s *i2s, int width)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	if (width < 16 || width > 24)
3718c2ecf20Sopenharmony_ci		return -EINVAL;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (width % 4)
3748c2ecf20Sopenharmony_ci		return -EINVAL;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return (width - 16) / 4;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic s8 sun4i_i2s_get_wss(const struct sun4i_i2s *i2s, int width)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	if (width < 16 || width > 32)
3828c2ecf20Sopenharmony_ci		return -EINVAL;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	if (width % 4)
3858c2ecf20Sopenharmony_ci		return -EINVAL;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	return (width - 16) / 4;
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistatic s8 sun8i_i2s_get_sr_wss(const struct sun4i_i2s *i2s, int width)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	if (width % 4)
3938c2ecf20Sopenharmony_ci		return -EINVAL;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	if (width < 8 || width > 32)
3968c2ecf20Sopenharmony_ci		return -EINVAL;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return (width - 8) / 4 + 1;
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic int sun4i_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
4028c2ecf20Sopenharmony_ci				  const struct snd_pcm_hw_params *params)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	unsigned int channels = params_channels(params);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	/* Map the channels for playback and capture */
4078c2ecf20Sopenharmony_ci	regmap_write(i2s->regmap, SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210);
4088c2ecf20Sopenharmony_ci	regmap_write(i2s->regmap, SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/* Configure the channels */
4118c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_TX_CHAN_SEL_REG,
4128c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL_MASK,
4138c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL(channels));
4148c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_RX_CHAN_SEL_REG,
4158c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL_MASK,
4168c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL(channels));
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	return 0;
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic int sun8i_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
4228c2ecf20Sopenharmony_ci				  const struct snd_pcm_hw_params *params)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	unsigned int channels = params_channels(params);
4258c2ecf20Sopenharmony_ci	unsigned int slots = channels;
4268c2ecf20Sopenharmony_ci	unsigned int lrck_period;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (i2s->slots)
4298c2ecf20Sopenharmony_ci		slots = i2s->slots;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/* Map the channels for playback and capture */
4328c2ecf20Sopenharmony_ci	regmap_write(i2s->regmap, SUN8I_I2S_TX_CHAN_MAP_REG, 0x76543210);
4338c2ecf20Sopenharmony_ci	regmap_write(i2s->regmap, SUN8I_I2S_RX_CHAN_MAP_REG, 0x76543210);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	/* Configure the channels */
4368c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
4378c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL_MASK,
4388c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL(channels));
4398c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN8I_I2S_RX_CHAN_SEL_REG,
4408c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL_MASK,
4418c2ecf20Sopenharmony_ci			   SUN4I_I2S_CHAN_SEL(channels));
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
4448c2ecf20Sopenharmony_ci			   SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK,
4458c2ecf20Sopenharmony_ci			   SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(channels));
4468c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
4478c2ecf20Sopenharmony_ci			   SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK,
4488c2ecf20Sopenharmony_ci			   SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(channels));
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	switch (i2s->format & SND_SOC_DAIFMT_FORMAT_MASK) {
4518c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A:
4528c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_B:
4538c2ecf20Sopenharmony_ci		lrck_period = params_physical_width(params) * slots;
4548c2ecf20Sopenharmony_ci		break;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
4578c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_RIGHT_J:
4588c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
4598c2ecf20Sopenharmony_ci		lrck_period = params_physical_width(params);
4608c2ecf20Sopenharmony_ci		break;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	default:
4638c2ecf20Sopenharmony_ci		return -EINVAL;
4648c2ecf20Sopenharmony_ci	}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
4678c2ecf20Sopenharmony_ci			   SUN8I_I2S_FMT0_LRCK_PERIOD_MASK,
4688c2ecf20Sopenharmony_ci			   SUN8I_I2S_FMT0_LRCK_PERIOD(lrck_period));
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
4718c2ecf20Sopenharmony_ci			   SUN8I_I2S_TX_CHAN_EN_MASK,
4728c2ecf20Sopenharmony_ci			   SUN8I_I2S_TX_CHAN_EN(channels));
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	return 0;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int sun4i_i2s_hw_params(struct snd_pcm_substream *substream,
4788c2ecf20Sopenharmony_ci			       struct snd_pcm_hw_params *params,
4798c2ecf20Sopenharmony_ci			       struct snd_soc_dai *dai)
4808c2ecf20Sopenharmony_ci{
4818c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
4828c2ecf20Sopenharmony_ci	unsigned int word_size = params_width(params);
4838c2ecf20Sopenharmony_ci	unsigned int slot_width = params_physical_width(params);
4848c2ecf20Sopenharmony_ci	unsigned int channels = params_channels(params);
4858c2ecf20Sopenharmony_ci	unsigned int slots = channels;
4868c2ecf20Sopenharmony_ci	int ret, sr, wss;
4878c2ecf20Sopenharmony_ci	u32 width;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	if (i2s->slots)
4908c2ecf20Sopenharmony_ci		slots = i2s->slots;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	if (i2s->slot_width)
4938c2ecf20Sopenharmony_ci		slot_width = i2s->slot_width;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	ret = i2s->variant->set_chan_cfg(i2s, params);
4968c2ecf20Sopenharmony_ci	if (ret < 0) {
4978c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Invalid channel configuration\n");
4988c2ecf20Sopenharmony_ci		return ret;
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	switch (params_physical_width(params)) {
5028c2ecf20Sopenharmony_ci	case 16:
5038c2ecf20Sopenharmony_ci		width = DMA_SLAVE_BUSWIDTH_2_BYTES;
5048c2ecf20Sopenharmony_ci		break;
5058c2ecf20Sopenharmony_ci	default:
5068c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported physical sample width: %d\n",
5078c2ecf20Sopenharmony_ci			params_physical_width(params));
5088c2ecf20Sopenharmony_ci		return -EINVAL;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci	i2s->playback_dma_data.addr_width = width;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	sr = i2s->variant->get_sr(i2s, word_size);
5138c2ecf20Sopenharmony_ci	if (sr < 0)
5148c2ecf20Sopenharmony_ci		return -EINVAL;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	wss = i2s->variant->get_wss(i2s, slot_width);
5178c2ecf20Sopenharmony_ci	if (wss < 0)
5188c2ecf20Sopenharmony_ci		return -EINVAL;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	regmap_field_write(i2s->field_fmt_wss, wss);
5218c2ecf20Sopenharmony_ci	regmap_field_write(i2s->field_fmt_sr, sr);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	return sun4i_i2s_set_clk_rate(dai, params_rate(params),
5248c2ecf20Sopenharmony_ci				      slots, slot_width);
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic int sun4i_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
5288c2ecf20Sopenharmony_ci				 unsigned int fmt)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	u32 val;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* DAI clock polarity */
5338c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
5348c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_IF:
5358c2ecf20Sopenharmony_ci		/* Invert both clocks */
5368c2ecf20Sopenharmony_ci		val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED |
5378c2ecf20Sopenharmony_ci		      SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
5388c2ecf20Sopenharmony_ci		break;
5398c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF:
5408c2ecf20Sopenharmony_ci		/* Invert bit clock */
5418c2ecf20Sopenharmony_ci		val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED;
5428c2ecf20Sopenharmony_ci		break;
5438c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_IF:
5448c2ecf20Sopenharmony_ci		/* Invert frame clock */
5458c2ecf20Sopenharmony_ci		val = SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
5468c2ecf20Sopenharmony_ci		break;
5478c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
5488c2ecf20Sopenharmony_ci		val = 0;
5498c2ecf20Sopenharmony_ci		break;
5508c2ecf20Sopenharmony_ci	default:
5518c2ecf20Sopenharmony_ci		return -EINVAL;
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
5558c2ecf20Sopenharmony_ci			   SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK |
5568c2ecf20Sopenharmony_ci			   SUN4I_I2S_FMT0_BCLK_POLARITY_MASK,
5578c2ecf20Sopenharmony_ci			   val);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	/* DAI Mode */
5608c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
5618c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
5628c2ecf20Sopenharmony_ci		val = SUN4I_I2S_FMT0_FMT_I2S;
5638c2ecf20Sopenharmony_ci		break;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
5668c2ecf20Sopenharmony_ci		val = SUN4I_I2S_FMT0_FMT_LEFT_J;
5678c2ecf20Sopenharmony_ci		break;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_RIGHT_J:
5708c2ecf20Sopenharmony_ci		val = SUN4I_I2S_FMT0_FMT_RIGHT_J;
5718c2ecf20Sopenharmony_ci		break;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	default:
5748c2ecf20Sopenharmony_ci		return -EINVAL;
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
5788c2ecf20Sopenharmony_ci			   SUN4I_I2S_FMT0_FMT_MASK, val);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/* DAI clock master masks */
5818c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
5828c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
5838c2ecf20Sopenharmony_ci		/* BCLK and LRCLK master */
5848c2ecf20Sopenharmony_ci		val = SUN4I_I2S_CTRL_MODE_MASTER;
5858c2ecf20Sopenharmony_ci		break;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
5888c2ecf20Sopenharmony_ci		/* BCLK and LRCLK slave */
5898c2ecf20Sopenharmony_ci		val = SUN4I_I2S_CTRL_MODE_SLAVE;
5908c2ecf20Sopenharmony_ci		break;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	default:
5938c2ecf20Sopenharmony_ci		return -EINVAL;
5948c2ecf20Sopenharmony_ci	}
5958c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
5968c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_MODE_MASK, val);
5978c2ecf20Sopenharmony_ci	return 0;
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic int sun8i_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
6018c2ecf20Sopenharmony_ci				 unsigned int fmt)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	u32 mode, val;
6048c2ecf20Sopenharmony_ci	u8 offset;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	/*
6078c2ecf20Sopenharmony_ci	 * DAI clock polarity
6088c2ecf20Sopenharmony_ci	 *
6098c2ecf20Sopenharmony_ci	 * The setup for LRCK contradicts the datasheet, but under a
6108c2ecf20Sopenharmony_ci	 * scope it's clear that the LRCK polarity is reversed
6118c2ecf20Sopenharmony_ci	 * compared to the expected polarity on the bus.
6128c2ecf20Sopenharmony_ci	 */
6138c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6148c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_IF:
6158c2ecf20Sopenharmony_ci		/* Invert both clocks */
6168c2ecf20Sopenharmony_ci		val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED;
6178c2ecf20Sopenharmony_ci		break;
6188c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF:
6198c2ecf20Sopenharmony_ci		/* Invert bit clock */
6208c2ecf20Sopenharmony_ci		val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED |
6218c2ecf20Sopenharmony_ci		      SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
6228c2ecf20Sopenharmony_ci		break;
6238c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_IF:
6248c2ecf20Sopenharmony_ci		/* Invert frame clock */
6258c2ecf20Sopenharmony_ci		val = 0;
6268c2ecf20Sopenharmony_ci		break;
6278c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
6288c2ecf20Sopenharmony_ci		val = SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
6298c2ecf20Sopenharmony_ci		break;
6308c2ecf20Sopenharmony_ci	default:
6318c2ecf20Sopenharmony_ci		return -EINVAL;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
6358c2ecf20Sopenharmony_ci			   SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK |
6368c2ecf20Sopenharmony_ci			   SUN8I_I2S_FMT0_BCLK_POLARITY_MASK,
6378c2ecf20Sopenharmony_ci			   val);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	/* DAI Mode */
6408c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6418c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A:
6428c2ecf20Sopenharmony_ci		mode = SUN8I_I2S_CTRL_MODE_PCM;
6438c2ecf20Sopenharmony_ci		offset = 1;
6448c2ecf20Sopenharmony_ci		break;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_B:
6478c2ecf20Sopenharmony_ci		mode = SUN8I_I2S_CTRL_MODE_PCM;
6488c2ecf20Sopenharmony_ci		offset = 0;
6498c2ecf20Sopenharmony_ci		break;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
6528c2ecf20Sopenharmony_ci		mode = SUN8I_I2S_CTRL_MODE_LEFT;
6538c2ecf20Sopenharmony_ci		offset = 1;
6548c2ecf20Sopenharmony_ci		break;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
6578c2ecf20Sopenharmony_ci		mode = SUN8I_I2S_CTRL_MODE_LEFT;
6588c2ecf20Sopenharmony_ci		offset = 0;
6598c2ecf20Sopenharmony_ci		break;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_RIGHT_J:
6628c2ecf20Sopenharmony_ci		mode = SUN8I_I2S_CTRL_MODE_RIGHT;
6638c2ecf20Sopenharmony_ci		offset = 0;
6648c2ecf20Sopenharmony_ci		break;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	default:
6678c2ecf20Sopenharmony_ci		return -EINVAL;
6688c2ecf20Sopenharmony_ci	}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
6718c2ecf20Sopenharmony_ci			   SUN8I_I2S_CTRL_MODE_MASK, mode);
6728c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
6738c2ecf20Sopenharmony_ci			   SUN8I_I2S_TX_CHAN_OFFSET_MASK,
6748c2ecf20Sopenharmony_ci			   SUN8I_I2S_TX_CHAN_OFFSET(offset));
6758c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN8I_I2S_RX_CHAN_SEL_REG,
6768c2ecf20Sopenharmony_ci			   SUN8I_I2S_TX_CHAN_OFFSET_MASK,
6778c2ecf20Sopenharmony_ci			   SUN8I_I2S_TX_CHAN_OFFSET(offset));
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	/* DAI clock master masks */
6808c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
6818c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
6828c2ecf20Sopenharmony_ci		/* BCLK and LRCLK master */
6838c2ecf20Sopenharmony_ci		val = SUN8I_I2S_CTRL_BCLK_OUT |	SUN8I_I2S_CTRL_LRCK_OUT;
6848c2ecf20Sopenharmony_ci		break;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
6878c2ecf20Sopenharmony_ci		/* BCLK and LRCLK slave */
6888c2ecf20Sopenharmony_ci		val = 0;
6898c2ecf20Sopenharmony_ci		break;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	default:
6928c2ecf20Sopenharmony_ci		return -EINVAL;
6938c2ecf20Sopenharmony_ci	}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
6968c2ecf20Sopenharmony_ci			   SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT,
6978c2ecf20Sopenharmony_ci			   val);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	return 0;
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic int sun4i_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
7038c2ecf20Sopenharmony_ci{
7048c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
7058c2ecf20Sopenharmony_ci	int ret;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	ret = i2s->variant->set_fmt(i2s, fmt);
7088c2ecf20Sopenharmony_ci	if (ret) {
7098c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported format configuration\n");
7108c2ecf20Sopenharmony_ci		return ret;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	/* Set significant bits in our FIFOs */
7148c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
7158c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK |
7168c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK,
7178c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_TX_MODE(1) |
7188c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_RX_MODE(1));
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	i2s->format = fmt;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	return 0;
7238c2ecf20Sopenharmony_ci}
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_cistatic void sun4i_i2s_start_capture(struct sun4i_i2s *i2s)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci	/* Flush RX FIFO */
7288c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
7298c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_FLUSH_RX,
7308c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_FLUSH_RX);
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	/* Clear RX counter */
7338c2ecf20Sopenharmony_ci	regmap_write(i2s->regmap, SUN4I_I2S_RX_CNT_REG, 0);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	/* Enable RX Block */
7368c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
7378c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_RX_EN,
7388c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_RX_EN);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	/* Enable RX DRQ */
7418c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
7428c2ecf20Sopenharmony_ci			   SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
7438c2ecf20Sopenharmony_ci			   SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN);
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_cistatic void sun4i_i2s_start_playback(struct sun4i_i2s *i2s)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	/* Flush TX FIFO */
7498c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
7508c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_FLUSH_TX,
7518c2ecf20Sopenharmony_ci			   SUN4I_I2S_FIFO_CTRL_FLUSH_TX);
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	/* Clear TX counter */
7548c2ecf20Sopenharmony_ci	regmap_write(i2s->regmap, SUN4I_I2S_TX_CNT_REG, 0);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	/* Enable TX Block */
7578c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
7588c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_TX_EN,
7598c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_TX_EN);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* Enable TX DRQ */
7628c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
7638c2ecf20Sopenharmony_ci			   SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
7648c2ecf20Sopenharmony_ci			   SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN);
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_cistatic void sun4i_i2s_stop_capture(struct sun4i_i2s *i2s)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	/* Disable RX Block */
7708c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
7718c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_RX_EN,
7728c2ecf20Sopenharmony_ci			   0);
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	/* Disable RX DRQ */
7758c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
7768c2ecf20Sopenharmony_ci			   SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
7778c2ecf20Sopenharmony_ci			   0);
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic void sun4i_i2s_stop_playback(struct sun4i_i2s *i2s)
7818c2ecf20Sopenharmony_ci{
7828c2ecf20Sopenharmony_ci	/* Disable TX Block */
7838c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
7848c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_TX_EN,
7858c2ecf20Sopenharmony_ci			   0);
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	/* Disable TX DRQ */
7888c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
7898c2ecf20Sopenharmony_ci			   SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
7908c2ecf20Sopenharmony_ci			   0);
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_cistatic int sun4i_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
7948c2ecf20Sopenharmony_ci			     struct snd_soc_dai *dai)
7958c2ecf20Sopenharmony_ci{
7968c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	switch (cmd) {
7998c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
8008c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
8018c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
8028c2ecf20Sopenharmony_ci		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
8038c2ecf20Sopenharmony_ci			sun4i_i2s_start_playback(i2s);
8048c2ecf20Sopenharmony_ci		else
8058c2ecf20Sopenharmony_ci			sun4i_i2s_start_capture(i2s);
8068c2ecf20Sopenharmony_ci		break;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
8098c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
8108c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
8118c2ecf20Sopenharmony_ci		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
8128c2ecf20Sopenharmony_ci			sun4i_i2s_stop_playback(i2s);
8138c2ecf20Sopenharmony_ci		else
8148c2ecf20Sopenharmony_ci			sun4i_i2s_stop_capture(i2s);
8158c2ecf20Sopenharmony_ci		break;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	default:
8188c2ecf20Sopenharmony_ci		return -EINVAL;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	return 0;
8228c2ecf20Sopenharmony_ci}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic int sun4i_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
8258c2ecf20Sopenharmony_ci				unsigned int freq, int dir)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	if (clk_id != 0)
8308c2ecf20Sopenharmony_ci		return -EINVAL;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	i2s->mclk_freq = freq;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	return 0;
8358c2ecf20Sopenharmony_ci}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_cistatic int sun4i_i2s_set_tdm_slot(struct snd_soc_dai *dai,
8388c2ecf20Sopenharmony_ci				  unsigned int tx_mask, unsigned int rx_mask,
8398c2ecf20Sopenharmony_ci				  int slots, int slot_width)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	if (slots > 8)
8448c2ecf20Sopenharmony_ci		return -EINVAL;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	i2s->slots = slots;
8478c2ecf20Sopenharmony_ci	i2s->slot_width = slot_width;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	return 0;
8508c2ecf20Sopenharmony_ci}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops sun4i_i2s_dai_ops = {
8538c2ecf20Sopenharmony_ci	.hw_params	= sun4i_i2s_hw_params,
8548c2ecf20Sopenharmony_ci	.set_fmt	= sun4i_i2s_set_fmt,
8558c2ecf20Sopenharmony_ci	.set_sysclk	= sun4i_i2s_set_sysclk,
8568c2ecf20Sopenharmony_ci	.set_tdm_slot	= sun4i_i2s_set_tdm_slot,
8578c2ecf20Sopenharmony_ci	.trigger	= sun4i_i2s_trigger,
8588c2ecf20Sopenharmony_ci};
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_cistatic int sun4i_i2s_dai_probe(struct snd_soc_dai *dai)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	snd_soc_dai_init_dma_data(dai,
8658c2ecf20Sopenharmony_ci				  &i2s->playback_dma_data,
8668c2ecf20Sopenharmony_ci				  &i2s->capture_dma_data);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	snd_soc_dai_set_drvdata(dai, i2s);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	return 0;
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver sun4i_i2s_dai = {
8748c2ecf20Sopenharmony_ci	.probe = sun4i_i2s_dai_probe,
8758c2ecf20Sopenharmony_ci	.capture = {
8768c2ecf20Sopenharmony_ci		.stream_name = "Capture",
8778c2ecf20Sopenharmony_ci		.channels_min = 1,
8788c2ecf20Sopenharmony_ci		.channels_max = 8,
8798c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_8000_192000,
8808c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S16_LE,
8818c2ecf20Sopenharmony_ci	},
8828c2ecf20Sopenharmony_ci	.playback = {
8838c2ecf20Sopenharmony_ci		.stream_name = "Playback",
8848c2ecf20Sopenharmony_ci		.channels_min = 1,
8858c2ecf20Sopenharmony_ci		.channels_max = 8,
8868c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_8000_192000,
8878c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S16_LE,
8888c2ecf20Sopenharmony_ci	},
8898c2ecf20Sopenharmony_ci	.ops = &sun4i_i2s_dai_ops,
8908c2ecf20Sopenharmony_ci	.symmetric_rates = 1,
8918c2ecf20Sopenharmony_ci};
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver sun4i_i2s_component = {
8948c2ecf20Sopenharmony_ci	.name	= "sun4i-dai",
8958c2ecf20Sopenharmony_ci};
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_cistatic bool sun4i_i2s_rd_reg(struct device *dev, unsigned int reg)
8988c2ecf20Sopenharmony_ci{
8998c2ecf20Sopenharmony_ci	switch (reg) {
9008c2ecf20Sopenharmony_ci	case SUN4I_I2S_FIFO_TX_REG:
9018c2ecf20Sopenharmony_ci		return false;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	default:
9048c2ecf20Sopenharmony_ci		return true;
9058c2ecf20Sopenharmony_ci	}
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_cistatic bool sun4i_i2s_wr_reg(struct device *dev, unsigned int reg)
9098c2ecf20Sopenharmony_ci{
9108c2ecf20Sopenharmony_ci	switch (reg) {
9118c2ecf20Sopenharmony_ci	case SUN4I_I2S_FIFO_RX_REG:
9128c2ecf20Sopenharmony_ci	case SUN4I_I2S_FIFO_STA_REG:
9138c2ecf20Sopenharmony_ci		return false;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	default:
9168c2ecf20Sopenharmony_ci		return true;
9178c2ecf20Sopenharmony_ci	}
9188c2ecf20Sopenharmony_ci}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_cistatic bool sun4i_i2s_volatile_reg(struct device *dev, unsigned int reg)
9218c2ecf20Sopenharmony_ci{
9228c2ecf20Sopenharmony_ci	switch (reg) {
9238c2ecf20Sopenharmony_ci	case SUN4I_I2S_FIFO_RX_REG:
9248c2ecf20Sopenharmony_ci	case SUN4I_I2S_INT_STA_REG:
9258c2ecf20Sopenharmony_ci	case SUN4I_I2S_RX_CNT_REG:
9268c2ecf20Sopenharmony_ci	case SUN4I_I2S_TX_CNT_REG:
9278c2ecf20Sopenharmony_ci		return true;
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	default:
9308c2ecf20Sopenharmony_ci		return false;
9318c2ecf20Sopenharmony_ci	}
9328c2ecf20Sopenharmony_ci}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_cistatic bool sun8i_i2s_rd_reg(struct device *dev, unsigned int reg)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	switch (reg) {
9378c2ecf20Sopenharmony_ci	case SUN8I_I2S_FIFO_TX_REG:
9388c2ecf20Sopenharmony_ci		return false;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	default:
9418c2ecf20Sopenharmony_ci		return true;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cistatic bool sun8i_i2s_volatile_reg(struct device *dev, unsigned int reg)
9468c2ecf20Sopenharmony_ci{
9478c2ecf20Sopenharmony_ci	if (reg == SUN8I_I2S_INT_STA_REG)
9488c2ecf20Sopenharmony_ci		return true;
9498c2ecf20Sopenharmony_ci	if (reg == SUN8I_I2S_FIFO_TX_REG)
9508c2ecf20Sopenharmony_ci		return false;
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	return sun4i_i2s_volatile_reg(dev, reg);
9538c2ecf20Sopenharmony_ci}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_cistatic const struct reg_default sun4i_i2s_reg_defaults[] = {
9568c2ecf20Sopenharmony_ci	{ SUN4I_I2S_CTRL_REG, 0x00000000 },
9578c2ecf20Sopenharmony_ci	{ SUN4I_I2S_FMT0_REG, 0x0000000c },
9588c2ecf20Sopenharmony_ci	{ SUN4I_I2S_FMT1_REG, 0x00004020 },
9598c2ecf20Sopenharmony_ci	{ SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
9608c2ecf20Sopenharmony_ci	{ SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
9618c2ecf20Sopenharmony_ci	{ SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
9628c2ecf20Sopenharmony_ci	{ SUN4I_I2S_TX_CHAN_SEL_REG, 0x00000001 },
9638c2ecf20Sopenharmony_ci	{ SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210 },
9648c2ecf20Sopenharmony_ci	{ SUN4I_I2S_RX_CHAN_SEL_REG, 0x00000001 },
9658c2ecf20Sopenharmony_ci	{ SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210 },
9668c2ecf20Sopenharmony_ci};
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_cistatic const struct reg_default sun8i_i2s_reg_defaults[] = {
9698c2ecf20Sopenharmony_ci	{ SUN4I_I2S_CTRL_REG, 0x00060000 },
9708c2ecf20Sopenharmony_ci	{ SUN4I_I2S_FMT0_REG, 0x00000033 },
9718c2ecf20Sopenharmony_ci	{ SUN4I_I2S_FMT1_REG, 0x00000030 },
9728c2ecf20Sopenharmony_ci	{ SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
9738c2ecf20Sopenharmony_ci	{ SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
9748c2ecf20Sopenharmony_ci	{ SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
9758c2ecf20Sopenharmony_ci	{ SUN8I_I2S_CHAN_CFG_REG, 0x00000000 },
9768c2ecf20Sopenharmony_ci	{ SUN8I_I2S_TX_CHAN_SEL_REG, 0x00000000 },
9778c2ecf20Sopenharmony_ci	{ SUN8I_I2S_TX_CHAN_MAP_REG, 0x00000000 },
9788c2ecf20Sopenharmony_ci	{ SUN8I_I2S_RX_CHAN_SEL_REG, 0x00000000 },
9798c2ecf20Sopenharmony_ci	{ SUN8I_I2S_RX_CHAN_MAP_REG, 0x00000000 },
9808c2ecf20Sopenharmony_ci};
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_cistatic const struct regmap_config sun4i_i2s_regmap_config = {
9838c2ecf20Sopenharmony_ci	.reg_bits	= 32,
9848c2ecf20Sopenharmony_ci	.reg_stride	= 4,
9858c2ecf20Sopenharmony_ci	.val_bits	= 32,
9868c2ecf20Sopenharmony_ci	.max_register	= SUN4I_I2S_RX_CHAN_MAP_REG,
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	.cache_type	= REGCACHE_FLAT,
9898c2ecf20Sopenharmony_ci	.reg_defaults	= sun4i_i2s_reg_defaults,
9908c2ecf20Sopenharmony_ci	.num_reg_defaults	= ARRAY_SIZE(sun4i_i2s_reg_defaults),
9918c2ecf20Sopenharmony_ci	.writeable_reg	= sun4i_i2s_wr_reg,
9928c2ecf20Sopenharmony_ci	.readable_reg	= sun4i_i2s_rd_reg,
9938c2ecf20Sopenharmony_ci	.volatile_reg	= sun4i_i2s_volatile_reg,
9948c2ecf20Sopenharmony_ci};
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic const struct regmap_config sun8i_i2s_regmap_config = {
9978c2ecf20Sopenharmony_ci	.reg_bits	= 32,
9988c2ecf20Sopenharmony_ci	.reg_stride	= 4,
9998c2ecf20Sopenharmony_ci	.val_bits	= 32,
10008c2ecf20Sopenharmony_ci	.max_register	= SUN8I_I2S_RX_CHAN_MAP_REG,
10018c2ecf20Sopenharmony_ci	.cache_type	= REGCACHE_FLAT,
10028c2ecf20Sopenharmony_ci	.reg_defaults	= sun8i_i2s_reg_defaults,
10038c2ecf20Sopenharmony_ci	.num_reg_defaults	= ARRAY_SIZE(sun8i_i2s_reg_defaults),
10048c2ecf20Sopenharmony_ci	.writeable_reg	= sun4i_i2s_wr_reg,
10058c2ecf20Sopenharmony_ci	.readable_reg	= sun8i_i2s_rd_reg,
10068c2ecf20Sopenharmony_ci	.volatile_reg	= sun8i_i2s_volatile_reg,
10078c2ecf20Sopenharmony_ci};
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_cistatic int sun4i_i2s_runtime_resume(struct device *dev)
10108c2ecf20Sopenharmony_ci{
10118c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = dev_get_drvdata(dev);
10128c2ecf20Sopenharmony_ci	int ret;
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(i2s->bus_clk);
10158c2ecf20Sopenharmony_ci	if (ret) {
10168c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to enable bus clock\n");
10178c2ecf20Sopenharmony_ci		return ret;
10188c2ecf20Sopenharmony_ci	}
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	regcache_cache_only(i2s->regmap, false);
10218c2ecf20Sopenharmony_ci	regcache_mark_dirty(i2s->regmap);
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	ret = regcache_sync(i2s->regmap);
10248c2ecf20Sopenharmony_ci	if (ret) {
10258c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to sync regmap cache\n");
10268c2ecf20Sopenharmony_ci		goto err_disable_clk;
10278c2ecf20Sopenharmony_ci	}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	/* Enable the whole hardware block */
10308c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
10318c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_GL_EN, SUN4I_I2S_CTRL_GL_EN);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	/* Enable the first output line */
10348c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
10358c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_SDO_EN_MASK,
10368c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_SDO_EN(0));
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(i2s->mod_clk);
10398c2ecf20Sopenharmony_ci	if (ret) {
10408c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to enable module clock\n");
10418c2ecf20Sopenharmony_ci		goto err_disable_clk;
10428c2ecf20Sopenharmony_ci	}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	return 0;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_cierr_disable_clk:
10478c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2s->bus_clk);
10488c2ecf20Sopenharmony_ci	return ret;
10498c2ecf20Sopenharmony_ci}
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_cistatic int sun4i_i2s_runtime_suspend(struct device *dev)
10528c2ecf20Sopenharmony_ci{
10538c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = dev_get_drvdata(dev);
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2s->mod_clk);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	/* Disable our output lines */
10588c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
10598c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_SDO_EN_MASK, 0);
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	/* Disable the whole hardware block */
10628c2ecf20Sopenharmony_ci	regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
10638c2ecf20Sopenharmony_ci			   SUN4I_I2S_CTRL_GL_EN, 0);
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	regcache_cache_only(i2s->regmap, true);
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2s->bus_clk);
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	return 0;
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_quirks sun4i_a10_i2s_quirks = {
10738c2ecf20Sopenharmony_ci	.has_reset		= false,
10748c2ecf20Sopenharmony_ci	.reg_offset_txdata	= SUN4I_I2S_FIFO_TX_REG,
10758c2ecf20Sopenharmony_ci	.sun4i_i2s_regmap	= &sun4i_i2s_regmap_config,
10768c2ecf20Sopenharmony_ci	.field_clkdiv_mclk_en	= REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
10778c2ecf20Sopenharmony_ci	.field_fmt_wss		= REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
10788c2ecf20Sopenharmony_ci	.field_fmt_sr		= REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
10798c2ecf20Sopenharmony_ci	.bclk_dividers		= sun4i_i2s_bclk_div,
10808c2ecf20Sopenharmony_ci	.num_bclk_dividers	= ARRAY_SIZE(sun4i_i2s_bclk_div),
10818c2ecf20Sopenharmony_ci	.mclk_dividers		= sun4i_i2s_mclk_div,
10828c2ecf20Sopenharmony_ci	.num_mclk_dividers	= ARRAY_SIZE(sun4i_i2s_mclk_div),
10838c2ecf20Sopenharmony_ci	.get_bclk_parent_rate	= sun4i_i2s_get_bclk_parent_rate,
10848c2ecf20Sopenharmony_ci	.get_sr			= sun4i_i2s_get_sr,
10858c2ecf20Sopenharmony_ci	.get_wss		= sun4i_i2s_get_wss,
10868c2ecf20Sopenharmony_ci	.set_chan_cfg		= sun4i_i2s_set_chan_cfg,
10878c2ecf20Sopenharmony_ci	.set_fmt		= sun4i_i2s_set_soc_fmt,
10888c2ecf20Sopenharmony_ci};
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_quirks sun6i_a31_i2s_quirks = {
10918c2ecf20Sopenharmony_ci	.has_reset		= true,
10928c2ecf20Sopenharmony_ci	.reg_offset_txdata	= SUN4I_I2S_FIFO_TX_REG,
10938c2ecf20Sopenharmony_ci	.sun4i_i2s_regmap	= &sun4i_i2s_regmap_config,
10948c2ecf20Sopenharmony_ci	.field_clkdiv_mclk_en	= REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
10958c2ecf20Sopenharmony_ci	.field_fmt_wss		= REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
10968c2ecf20Sopenharmony_ci	.field_fmt_sr		= REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
10978c2ecf20Sopenharmony_ci	.bclk_dividers		= sun4i_i2s_bclk_div,
10988c2ecf20Sopenharmony_ci	.num_bclk_dividers	= ARRAY_SIZE(sun4i_i2s_bclk_div),
10998c2ecf20Sopenharmony_ci	.mclk_dividers		= sun4i_i2s_mclk_div,
11008c2ecf20Sopenharmony_ci	.num_mclk_dividers	= ARRAY_SIZE(sun4i_i2s_mclk_div),
11018c2ecf20Sopenharmony_ci	.get_bclk_parent_rate	= sun4i_i2s_get_bclk_parent_rate,
11028c2ecf20Sopenharmony_ci	.get_sr			= sun4i_i2s_get_sr,
11038c2ecf20Sopenharmony_ci	.get_wss		= sun4i_i2s_get_wss,
11048c2ecf20Sopenharmony_ci	.set_chan_cfg		= sun4i_i2s_set_chan_cfg,
11058c2ecf20Sopenharmony_ci	.set_fmt		= sun4i_i2s_set_soc_fmt,
11068c2ecf20Sopenharmony_ci};
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci/*
11098c2ecf20Sopenharmony_ci * This doesn't describe the TDM controller documented in the A83t
11108c2ecf20Sopenharmony_ci * datasheet, but the three undocumented I2S controller that use the
11118c2ecf20Sopenharmony_ci * older design.
11128c2ecf20Sopenharmony_ci */
11138c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_quirks sun8i_a83t_i2s_quirks = {
11148c2ecf20Sopenharmony_ci	.has_reset		= true,
11158c2ecf20Sopenharmony_ci	.reg_offset_txdata	= SUN8I_I2S_FIFO_TX_REG,
11168c2ecf20Sopenharmony_ci	.sun4i_i2s_regmap	= &sun4i_i2s_regmap_config,
11178c2ecf20Sopenharmony_ci	.field_clkdiv_mclk_en	= REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
11188c2ecf20Sopenharmony_ci	.field_fmt_wss		= REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
11198c2ecf20Sopenharmony_ci	.field_fmt_sr		= REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
11208c2ecf20Sopenharmony_ci	.bclk_dividers		= sun4i_i2s_bclk_div,
11218c2ecf20Sopenharmony_ci	.num_bclk_dividers	= ARRAY_SIZE(sun4i_i2s_bclk_div),
11228c2ecf20Sopenharmony_ci	.mclk_dividers		= sun4i_i2s_mclk_div,
11238c2ecf20Sopenharmony_ci	.num_mclk_dividers	= ARRAY_SIZE(sun4i_i2s_mclk_div),
11248c2ecf20Sopenharmony_ci	.get_bclk_parent_rate	= sun4i_i2s_get_bclk_parent_rate,
11258c2ecf20Sopenharmony_ci	.get_sr			= sun4i_i2s_get_sr,
11268c2ecf20Sopenharmony_ci	.get_wss		= sun4i_i2s_get_wss,
11278c2ecf20Sopenharmony_ci	.set_chan_cfg		= sun4i_i2s_set_chan_cfg,
11288c2ecf20Sopenharmony_ci	.set_fmt		= sun4i_i2s_set_soc_fmt,
11298c2ecf20Sopenharmony_ci};
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_quirks sun8i_h3_i2s_quirks = {
11328c2ecf20Sopenharmony_ci	.has_reset		= true,
11338c2ecf20Sopenharmony_ci	.reg_offset_txdata	= SUN8I_I2S_FIFO_TX_REG,
11348c2ecf20Sopenharmony_ci	.sun4i_i2s_regmap	= &sun8i_i2s_regmap_config,
11358c2ecf20Sopenharmony_ci	.field_clkdiv_mclk_en	= REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
11368c2ecf20Sopenharmony_ci	.field_fmt_wss		= REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
11378c2ecf20Sopenharmony_ci	.field_fmt_sr		= REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
11388c2ecf20Sopenharmony_ci	.bclk_dividers		= sun8i_i2s_clk_div,
11398c2ecf20Sopenharmony_ci	.num_bclk_dividers	= ARRAY_SIZE(sun8i_i2s_clk_div),
11408c2ecf20Sopenharmony_ci	.mclk_dividers		= sun8i_i2s_clk_div,
11418c2ecf20Sopenharmony_ci	.num_mclk_dividers	= ARRAY_SIZE(sun8i_i2s_clk_div),
11428c2ecf20Sopenharmony_ci	.get_bclk_parent_rate	= sun8i_i2s_get_bclk_parent_rate,
11438c2ecf20Sopenharmony_ci	.get_sr			= sun8i_i2s_get_sr_wss,
11448c2ecf20Sopenharmony_ci	.get_wss		= sun8i_i2s_get_sr_wss,
11458c2ecf20Sopenharmony_ci	.set_chan_cfg		= sun8i_i2s_set_chan_cfg,
11468c2ecf20Sopenharmony_ci	.set_fmt		= sun8i_i2s_set_soc_fmt,
11478c2ecf20Sopenharmony_ci};
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_cistatic const struct sun4i_i2s_quirks sun50i_a64_codec_i2s_quirks = {
11508c2ecf20Sopenharmony_ci	.has_reset		= true,
11518c2ecf20Sopenharmony_ci	.reg_offset_txdata	= SUN8I_I2S_FIFO_TX_REG,
11528c2ecf20Sopenharmony_ci	.sun4i_i2s_regmap	= &sun4i_i2s_regmap_config,
11538c2ecf20Sopenharmony_ci	.field_clkdiv_mclk_en	= REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
11548c2ecf20Sopenharmony_ci	.field_fmt_wss		= REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
11558c2ecf20Sopenharmony_ci	.field_fmt_sr		= REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
11568c2ecf20Sopenharmony_ci	.bclk_dividers		= sun4i_i2s_bclk_div,
11578c2ecf20Sopenharmony_ci	.num_bclk_dividers	= ARRAY_SIZE(sun4i_i2s_bclk_div),
11588c2ecf20Sopenharmony_ci	.mclk_dividers		= sun4i_i2s_mclk_div,
11598c2ecf20Sopenharmony_ci	.num_mclk_dividers	= ARRAY_SIZE(sun4i_i2s_mclk_div),
11608c2ecf20Sopenharmony_ci	.get_bclk_parent_rate	= sun4i_i2s_get_bclk_parent_rate,
11618c2ecf20Sopenharmony_ci	.get_sr			= sun4i_i2s_get_sr,
11628c2ecf20Sopenharmony_ci	.get_wss		= sun4i_i2s_get_wss,
11638c2ecf20Sopenharmony_ci	.set_chan_cfg		= sun4i_i2s_set_chan_cfg,
11648c2ecf20Sopenharmony_ci	.set_fmt		= sun4i_i2s_set_soc_fmt,
11658c2ecf20Sopenharmony_ci};
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_cistatic int sun4i_i2s_init_regmap_fields(struct device *dev,
11688c2ecf20Sopenharmony_ci					struct sun4i_i2s *i2s)
11698c2ecf20Sopenharmony_ci{
11708c2ecf20Sopenharmony_ci	i2s->field_clkdiv_mclk_en =
11718c2ecf20Sopenharmony_ci		devm_regmap_field_alloc(dev, i2s->regmap,
11728c2ecf20Sopenharmony_ci					i2s->variant->field_clkdiv_mclk_en);
11738c2ecf20Sopenharmony_ci	if (IS_ERR(i2s->field_clkdiv_mclk_en))
11748c2ecf20Sopenharmony_ci		return PTR_ERR(i2s->field_clkdiv_mclk_en);
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci	i2s->field_fmt_wss =
11778c2ecf20Sopenharmony_ci			devm_regmap_field_alloc(dev, i2s->regmap,
11788c2ecf20Sopenharmony_ci						i2s->variant->field_fmt_wss);
11798c2ecf20Sopenharmony_ci	if (IS_ERR(i2s->field_fmt_wss))
11808c2ecf20Sopenharmony_ci		return PTR_ERR(i2s->field_fmt_wss);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	i2s->field_fmt_sr =
11838c2ecf20Sopenharmony_ci			devm_regmap_field_alloc(dev, i2s->regmap,
11848c2ecf20Sopenharmony_ci						i2s->variant->field_fmt_sr);
11858c2ecf20Sopenharmony_ci	if (IS_ERR(i2s->field_fmt_sr))
11868c2ecf20Sopenharmony_ci		return PTR_ERR(i2s->field_fmt_sr);
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	return 0;
11898c2ecf20Sopenharmony_ci}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_cistatic int sun4i_i2s_probe(struct platform_device *pdev)
11928c2ecf20Sopenharmony_ci{
11938c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s;
11948c2ecf20Sopenharmony_ci	struct resource *res;
11958c2ecf20Sopenharmony_ci	void __iomem *regs;
11968c2ecf20Sopenharmony_ci	int irq, ret;
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
11998c2ecf20Sopenharmony_ci	if (!i2s)
12008c2ecf20Sopenharmony_ci		return -ENOMEM;
12018c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, i2s);
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12048c2ecf20Sopenharmony_ci	regs = devm_ioremap_resource(&pdev->dev, res);
12058c2ecf20Sopenharmony_ci	if (IS_ERR(regs))
12068c2ecf20Sopenharmony_ci		return PTR_ERR(regs);
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
12098c2ecf20Sopenharmony_ci	if (irq < 0)
12108c2ecf20Sopenharmony_ci		return irq;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	i2s->variant = of_device_get_match_data(&pdev->dev);
12138c2ecf20Sopenharmony_ci	if (!i2s->variant) {
12148c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
12158c2ecf20Sopenharmony_ci		return -ENODEV;
12168c2ecf20Sopenharmony_ci	}
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	i2s->bus_clk = devm_clk_get(&pdev->dev, "apb");
12198c2ecf20Sopenharmony_ci	if (IS_ERR(i2s->bus_clk)) {
12208c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Can't get our bus clock\n");
12218c2ecf20Sopenharmony_ci		return PTR_ERR(i2s->bus_clk);
12228c2ecf20Sopenharmony_ci	}
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
12258c2ecf20Sopenharmony_ci					    i2s->variant->sun4i_i2s_regmap);
12268c2ecf20Sopenharmony_ci	if (IS_ERR(i2s->regmap)) {
12278c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Regmap initialisation failed\n");
12288c2ecf20Sopenharmony_ci		return PTR_ERR(i2s->regmap);
12298c2ecf20Sopenharmony_ci	}
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	i2s->mod_clk = devm_clk_get(&pdev->dev, "mod");
12328c2ecf20Sopenharmony_ci	if (IS_ERR(i2s->mod_clk)) {
12338c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Can't get our mod clock\n");
12348c2ecf20Sopenharmony_ci		return PTR_ERR(i2s->mod_clk);
12358c2ecf20Sopenharmony_ci	}
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	if (i2s->variant->has_reset) {
12388c2ecf20Sopenharmony_ci		i2s->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
12398c2ecf20Sopenharmony_ci		if (IS_ERR(i2s->rst)) {
12408c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "Failed to get reset control\n");
12418c2ecf20Sopenharmony_ci			return PTR_ERR(i2s->rst);
12428c2ecf20Sopenharmony_ci		}
12438c2ecf20Sopenharmony_ci	}
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	if (!IS_ERR(i2s->rst)) {
12468c2ecf20Sopenharmony_ci		ret = reset_control_deassert(i2s->rst);
12478c2ecf20Sopenharmony_ci		if (ret) {
12488c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
12498c2ecf20Sopenharmony_ci				"Failed to deassert the reset control\n");
12508c2ecf20Sopenharmony_ci			return -EINVAL;
12518c2ecf20Sopenharmony_ci		}
12528c2ecf20Sopenharmony_ci	}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	i2s->playback_dma_data.addr = res->start +
12558c2ecf20Sopenharmony_ci					i2s->variant->reg_offset_txdata;
12568c2ecf20Sopenharmony_ci	i2s->playback_dma_data.maxburst = 8;
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG;
12598c2ecf20Sopenharmony_ci	i2s->capture_dma_data.maxburst = 8;
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
12628c2ecf20Sopenharmony_ci	if (!pm_runtime_enabled(&pdev->dev)) {
12638c2ecf20Sopenharmony_ci		ret = sun4i_i2s_runtime_resume(&pdev->dev);
12648c2ecf20Sopenharmony_ci		if (ret)
12658c2ecf20Sopenharmony_ci			goto err_pm_disable;
12668c2ecf20Sopenharmony_ci	}
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	ret = sun4i_i2s_init_regmap_fields(&pdev->dev, i2s);
12698c2ecf20Sopenharmony_ci	if (ret) {
12708c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not initialise regmap fields\n");
12718c2ecf20Sopenharmony_ci		goto err_suspend;
12728c2ecf20Sopenharmony_ci	}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
12758c2ecf20Sopenharmony_ci	if (ret) {
12768c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not register PCM\n");
12778c2ecf20Sopenharmony_ci		goto err_suspend;
12788c2ecf20Sopenharmony_ci	}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	ret = devm_snd_soc_register_component(&pdev->dev,
12818c2ecf20Sopenharmony_ci					      &sun4i_i2s_component,
12828c2ecf20Sopenharmony_ci					      &sun4i_i2s_dai, 1);
12838c2ecf20Sopenharmony_ci	if (ret) {
12848c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not register DAI\n");
12858c2ecf20Sopenharmony_ci		goto err_suspend;
12868c2ecf20Sopenharmony_ci	}
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	return 0;
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_cierr_suspend:
12918c2ecf20Sopenharmony_ci	if (!pm_runtime_status_suspended(&pdev->dev))
12928c2ecf20Sopenharmony_ci		sun4i_i2s_runtime_suspend(&pdev->dev);
12938c2ecf20Sopenharmony_cierr_pm_disable:
12948c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
12958c2ecf20Sopenharmony_ci	if (!IS_ERR(i2s->rst))
12968c2ecf20Sopenharmony_ci		reset_control_assert(i2s->rst);
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	return ret;
12998c2ecf20Sopenharmony_ci}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_cistatic int sun4i_i2s_remove(struct platform_device *pdev)
13028c2ecf20Sopenharmony_ci{
13038c2ecf20Sopenharmony_ci	struct sun4i_i2s *i2s = dev_get_drvdata(&pdev->dev);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
13068c2ecf20Sopenharmony_ci	if (!pm_runtime_status_suspended(&pdev->dev))
13078c2ecf20Sopenharmony_ci		sun4i_i2s_runtime_suspend(&pdev->dev);
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	if (!IS_ERR(i2s->rst))
13108c2ecf20Sopenharmony_ci		reset_control_assert(i2s->rst);
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	return 0;
13138c2ecf20Sopenharmony_ci}
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_cistatic const struct of_device_id sun4i_i2s_match[] = {
13168c2ecf20Sopenharmony_ci	{
13178c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun4i-a10-i2s",
13188c2ecf20Sopenharmony_ci		.data = &sun4i_a10_i2s_quirks,
13198c2ecf20Sopenharmony_ci	},
13208c2ecf20Sopenharmony_ci	{
13218c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun6i-a31-i2s",
13228c2ecf20Sopenharmony_ci		.data = &sun6i_a31_i2s_quirks,
13238c2ecf20Sopenharmony_ci	},
13248c2ecf20Sopenharmony_ci	{
13258c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun8i-a83t-i2s",
13268c2ecf20Sopenharmony_ci		.data = &sun8i_a83t_i2s_quirks,
13278c2ecf20Sopenharmony_ci	},
13288c2ecf20Sopenharmony_ci	{
13298c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun8i-h3-i2s",
13308c2ecf20Sopenharmony_ci		.data = &sun8i_h3_i2s_quirks,
13318c2ecf20Sopenharmony_ci	},
13328c2ecf20Sopenharmony_ci	{
13338c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun50i-a64-codec-i2s",
13348c2ecf20Sopenharmony_ci		.data = &sun50i_a64_codec_i2s_quirks,
13358c2ecf20Sopenharmony_ci	},
13368c2ecf20Sopenharmony_ci	{}
13378c2ecf20Sopenharmony_ci};
13388c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sun4i_i2s_match);
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_cistatic const struct dev_pm_ops sun4i_i2s_pm_ops = {
13418c2ecf20Sopenharmony_ci	.runtime_resume		= sun4i_i2s_runtime_resume,
13428c2ecf20Sopenharmony_ci	.runtime_suspend	= sun4i_i2s_runtime_suspend,
13438c2ecf20Sopenharmony_ci};
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_cistatic struct platform_driver sun4i_i2s_driver = {
13468c2ecf20Sopenharmony_ci	.probe	= sun4i_i2s_probe,
13478c2ecf20Sopenharmony_ci	.remove	= sun4i_i2s_remove,
13488c2ecf20Sopenharmony_ci	.driver	= {
13498c2ecf20Sopenharmony_ci		.name		= "sun4i-i2s",
13508c2ecf20Sopenharmony_ci		.of_match_table	= sun4i_i2s_match,
13518c2ecf20Sopenharmony_ci		.pm		= &sun4i_i2s_pm_ops,
13528c2ecf20Sopenharmony_ci	},
13538c2ecf20Sopenharmony_ci};
13548c2ecf20Sopenharmony_cimodule_platform_driver(sun4i_i2s_driver);
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
13578c2ecf20Sopenharmony_ciMODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
13588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Allwinner A10 I2S driver");
13598c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1360