1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (c) 2020 BayLibre, SAS.
4// Author: Jerome Brunet <jbrunet@baylibre.com>
5
6#include <linux/bitfield.h>
7#include <linux/clk.h>
8#include <linux/module.h>
9#include <sound/pcm_params.h>
10#include <linux/regmap.h>
11#include <linux/regulator/consumer.h>
12#include <linux/reset.h>
13#include <sound/soc.h>
14#include <sound/soc-dai.h>
15
16#include <dt-bindings/sound/meson-g12a-toacodec.h>
17#include "axg-tdm.h"
18#include "meson-codec-glue.h"
19
20#define G12A_TOACODEC_DRV_NAME "g12a-toacodec"
21
22#define TOACODEC_CTRL0			0x0
23#define  CTRL0_ENABLE_SHIFT		31
24#define  CTRL0_DAT_SEL_SHIFT		14
25#define  CTRL0_DAT_SEL			(0x3 << CTRL0_DAT_SEL_SHIFT)
26#define  CTRL0_LANE_SEL			12
27#define  CTRL0_LRCLK_SEL		GENMASK(9, 8)
28#define  CTRL0_BLK_CAP_INV		BIT(7)
29#define  CTRL0_BCLK_O_INV		BIT(6)
30#define  CTRL0_BCLK_SEL			GENMASK(5, 4)
31#define  CTRL0_MCLK_SEL			GENMASK(2, 0)
32
33#define TOACODEC_OUT_CHMAX		2
34
35static const char * const g12a_toacodec_mux_texts[] = {
36	"I2S A", "I2S B", "I2S C",
37};
38
39static int g12a_toacodec_mux_put_enum(struct snd_kcontrol *kcontrol,
40				      struct snd_ctl_elem_value *ucontrol)
41{
42	struct snd_soc_component *component =
43		snd_soc_dapm_kcontrol_component(kcontrol);
44	struct snd_soc_dapm_context *dapm =
45		snd_soc_dapm_kcontrol_dapm(kcontrol);
46	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
47	unsigned int mux, changed;
48
49	if (ucontrol->value.enumerated.item[0] >= e->items)
50		return -EINVAL;
51
52	mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
53	changed = snd_soc_component_test_bits(component, e->reg,
54					      CTRL0_DAT_SEL,
55					      FIELD_PREP(CTRL0_DAT_SEL, mux));
56
57	if (!changed)
58		return 0;
59
60	/* Force disconnect of the mux while updating */
61	snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
62
63	snd_soc_component_update_bits(component, e->reg,
64				      CTRL0_DAT_SEL |
65				      CTRL0_LRCLK_SEL |
66				      CTRL0_BCLK_SEL,
67				      FIELD_PREP(CTRL0_DAT_SEL, mux) |
68				      FIELD_PREP(CTRL0_LRCLK_SEL, mux) |
69				      FIELD_PREP(CTRL0_BCLK_SEL, mux));
70
71	/*
72	 * FIXME:
73	 * On this soc, the glue gets the MCLK directly from the clock
74	 * controller instead of going the through the TDM interface.
75	 *
76	 * Here we assume interface A uses clock A, etc ... While it is
77	 * true for now, it could be different. Instead the glue should
78	 * find out the clock used by the interface and select the same
79	 * source. For that, we will need regmap backed clock mux which
80	 * is a work in progress
81	 */
82	snd_soc_component_update_bits(component, e->reg,
83				      CTRL0_MCLK_SEL,
84				      FIELD_PREP(CTRL0_MCLK_SEL, mux));
85
86	snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
87
88	return 1;
89}
90
91static SOC_ENUM_SINGLE_DECL(g12a_toacodec_mux_enum, TOACODEC_CTRL0,
92			    CTRL0_DAT_SEL_SHIFT,
93			    g12a_toacodec_mux_texts);
94
95static const struct snd_kcontrol_new g12a_toacodec_mux =
96	SOC_DAPM_ENUM_EXT("Source", g12a_toacodec_mux_enum,
97			  snd_soc_dapm_get_enum_double,
98			  g12a_toacodec_mux_put_enum);
99
100static const struct snd_kcontrol_new g12a_toacodec_out_enable =
101	SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOACODEC_CTRL0,
102				    CTRL0_ENABLE_SHIFT, 1, 0);
103
104static const struct snd_soc_dapm_widget g12a_toacodec_widgets[] = {
105	SND_SOC_DAPM_MUX("SRC", SND_SOC_NOPM, 0, 0,
106			 &g12a_toacodec_mux),
107	SND_SOC_DAPM_SWITCH("OUT EN", SND_SOC_NOPM, 0, 0,
108			    &g12a_toacodec_out_enable),
109};
110
111static int g12a_toacodec_input_hw_params(struct snd_pcm_substream *substream,
112					 struct snd_pcm_hw_params *params,
113					 struct snd_soc_dai *dai)
114{
115	struct meson_codec_glue_input *data;
116	int ret;
117
118	ret = meson_codec_glue_input_hw_params(substream, params, dai);
119	if (ret)
120		return ret;
121
122	/* The glue will provide 1 lane out of the 4 to the output */
123	data = meson_codec_glue_input_get_data(dai);
124	data->params.channels_min = min_t(unsigned int, TOACODEC_OUT_CHMAX,
125					data->params.channels_min);
126	data->params.channels_max = min_t(unsigned int, TOACODEC_OUT_CHMAX,
127					data->params.channels_max);
128
129	return 0;
130}
131
132static const struct snd_soc_dai_ops g12a_toacodec_input_ops = {
133	.hw_params	= g12a_toacodec_input_hw_params,
134	.set_fmt	= meson_codec_glue_input_set_fmt,
135};
136
137static const struct snd_soc_dai_ops g12a_toacodec_output_ops = {
138	.startup	= meson_codec_glue_output_startup,
139};
140
141#define TOACODEC_STREAM(xname, xsuffix, xchmax)			\
142{								\
143	.stream_name	= xname " " xsuffix,			\
144	.channels_min	= 1,					\
145	.channels_max	= (xchmax),				\
146	.rate_min       = 5512,					\
147	.rate_max	= 192000,				\
148	.formats	= AXG_TDM_FORMATS,			\
149}
150
151#define TOACODEC_INPUT(xname, xid) {					\
152	.name = xname,							\
153	.id = (xid),							\
154	.playback = TOACODEC_STREAM(xname, "Playback", 8),		\
155	.ops = &g12a_toacodec_input_ops,				\
156	.probe = meson_codec_glue_input_dai_probe,			\
157	.remove = meson_codec_glue_input_dai_remove,			\
158}
159
160#define TOACODEC_OUTPUT(xname, xid) {					\
161	.name = xname,							\
162	.id = (xid),							\
163	.capture = TOACODEC_STREAM(xname, "Capture", TOACODEC_OUT_CHMAX), \
164	.ops = &g12a_toacodec_output_ops,				\
165}
166
167static struct snd_soc_dai_driver g12a_toacodec_dai_drv[] = {
168	TOACODEC_INPUT("IN A", TOACODEC_IN_A),
169	TOACODEC_INPUT("IN B", TOACODEC_IN_B),
170	TOACODEC_INPUT("IN C", TOACODEC_IN_C),
171	TOACODEC_OUTPUT("OUT", TOACODEC_OUT),
172};
173
174static int g12a_toacodec_component_probe(struct snd_soc_component *c)
175{
176	/* Initialize the static clock parameters */
177	return snd_soc_component_write(c, TOACODEC_CTRL0,
178				       CTRL0_BLK_CAP_INV);
179}
180
181static const struct snd_soc_dapm_route g12a_toacodec_routes[] = {
182	{ "SRC", "I2S A", "IN A Playback" },
183	{ "SRC", "I2S B", "IN B Playback" },
184	{ "SRC", "I2S C", "IN C Playback" },
185	{ "OUT EN", "Switch", "SRC" },
186	{ "OUT Capture", NULL, "OUT EN" },
187};
188
189static const struct snd_kcontrol_new g12a_toacodec_controls[] = {
190	SOC_SINGLE("Lane Select", TOACODEC_CTRL0, CTRL0_LANE_SEL, 3, 0),
191};
192
193static const struct snd_soc_component_driver g12a_toacodec_component_drv = {
194	.probe			= g12a_toacodec_component_probe,
195	.controls		= g12a_toacodec_controls,
196	.num_controls		= ARRAY_SIZE(g12a_toacodec_controls),
197	.dapm_widgets		= g12a_toacodec_widgets,
198	.num_dapm_widgets	= ARRAY_SIZE(g12a_toacodec_widgets),
199	.dapm_routes		= g12a_toacodec_routes,
200	.num_dapm_routes	= ARRAY_SIZE(g12a_toacodec_routes),
201	.endianness		= 1,
202	.non_legacy_dai_naming	= 1,
203};
204
205static const struct regmap_config g12a_toacodec_regmap_cfg = {
206	.reg_bits	= 32,
207	.val_bits	= 32,
208	.reg_stride	= 4,
209};
210
211static const struct of_device_id g12a_toacodec_of_match[] = {
212	{ .compatible = "amlogic,g12a-toacodec", },
213	{}
214};
215MODULE_DEVICE_TABLE(of, g12a_toacodec_of_match);
216
217static int g12a_toacodec_probe(struct platform_device *pdev)
218{
219	struct device *dev = &pdev->dev;
220	void __iomem *regs;
221	struct regmap *map;
222	int ret;
223
224	ret = device_reset(dev);
225	if (ret)
226		return ret;
227
228	regs = devm_platform_ioremap_resource(pdev, 0);
229	if (IS_ERR(regs))
230		return PTR_ERR(regs);
231
232	map = devm_regmap_init_mmio(dev, regs, &g12a_toacodec_regmap_cfg);
233	if (IS_ERR(map)) {
234		dev_err(dev, "failed to init regmap: %ld\n",
235			PTR_ERR(map));
236		return PTR_ERR(map);
237	}
238
239	return devm_snd_soc_register_component(dev,
240			&g12a_toacodec_component_drv, g12a_toacodec_dai_drv,
241			ARRAY_SIZE(g12a_toacodec_dai_drv));
242}
243
244static struct platform_driver g12a_toacodec_pdrv = {
245	.driver = {
246		.name = G12A_TOACODEC_DRV_NAME,
247		.of_match_table = g12a_toacodec_of_match,
248	},
249	.probe = g12a_toacodec_probe,
250};
251module_platform_driver(g12a_toacodec_pdrv);
252
253MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
254MODULE_DESCRIPTION("Amlogic G12a To Internal DAC Codec Driver");
255MODULE_LICENSE("GPL v2");
256