18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Copyright (c) 2020 BayLibre, SAS.
48c2ecf20Sopenharmony_ci// Author: Jerome Brunet <jbrunet@baylibre.com>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
108c2ecf20Sopenharmony_ci#include <linux/regmap.h>
118c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
128c2ecf20Sopenharmony_ci#include <linux/reset.h>
138c2ecf20Sopenharmony_ci#include <sound/soc.h>
148c2ecf20Sopenharmony_ci#include <sound/soc-dai.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <dt-bindings/sound/meson-g12a-toacodec.h>
178c2ecf20Sopenharmony_ci#include "axg-tdm.h"
188c2ecf20Sopenharmony_ci#include "meson-codec-glue.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define G12A_TOACODEC_DRV_NAME "g12a-toacodec"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define TOACODEC_CTRL0			0x0
238c2ecf20Sopenharmony_ci#define  CTRL0_ENABLE_SHIFT		31
248c2ecf20Sopenharmony_ci#define  CTRL0_DAT_SEL_SHIFT		14
258c2ecf20Sopenharmony_ci#define  CTRL0_DAT_SEL			(0x3 << CTRL0_DAT_SEL_SHIFT)
268c2ecf20Sopenharmony_ci#define  CTRL0_LANE_SEL			12
278c2ecf20Sopenharmony_ci#define  CTRL0_LRCLK_SEL		GENMASK(9, 8)
288c2ecf20Sopenharmony_ci#define  CTRL0_BLK_CAP_INV		BIT(7)
298c2ecf20Sopenharmony_ci#define  CTRL0_BCLK_O_INV		BIT(6)
308c2ecf20Sopenharmony_ci#define  CTRL0_BCLK_SEL			GENMASK(5, 4)
318c2ecf20Sopenharmony_ci#define  CTRL0_MCLK_SEL			GENMASK(2, 0)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define TOACODEC_OUT_CHMAX		2
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const char * const g12a_toacodec_mux_texts[] = {
368c2ecf20Sopenharmony_ci	"I2S A", "I2S B", "I2S C",
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int g12a_toacodec_mux_put_enum(struct snd_kcontrol *kcontrol,
408c2ecf20Sopenharmony_ci				      struct snd_ctl_elem_value *ucontrol)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct snd_soc_component *component =
438c2ecf20Sopenharmony_ci		snd_soc_dapm_kcontrol_component(kcontrol);
448c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm =
458c2ecf20Sopenharmony_ci		snd_soc_dapm_kcontrol_dapm(kcontrol);
468c2ecf20Sopenharmony_ci	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
478c2ecf20Sopenharmony_ci	unsigned int mux, changed;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (ucontrol->value.enumerated.item[0] >= e->items)
508c2ecf20Sopenharmony_ci		return -EINVAL;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
538c2ecf20Sopenharmony_ci	changed = snd_soc_component_test_bits(component, e->reg,
548c2ecf20Sopenharmony_ci					      CTRL0_DAT_SEL,
558c2ecf20Sopenharmony_ci					      FIELD_PREP(CTRL0_DAT_SEL, mux));
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (!changed)
588c2ecf20Sopenharmony_ci		return 0;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* Force disconnect of the mux while updating */
618c2ecf20Sopenharmony_ci	snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, e->reg,
648c2ecf20Sopenharmony_ci				      CTRL0_DAT_SEL |
658c2ecf20Sopenharmony_ci				      CTRL0_LRCLK_SEL |
668c2ecf20Sopenharmony_ci				      CTRL0_BCLK_SEL,
678c2ecf20Sopenharmony_ci				      FIELD_PREP(CTRL0_DAT_SEL, mux) |
688c2ecf20Sopenharmony_ci				      FIELD_PREP(CTRL0_LRCLK_SEL, mux) |
698c2ecf20Sopenharmony_ci				      FIELD_PREP(CTRL0_BCLK_SEL, mux));
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * FIXME:
738c2ecf20Sopenharmony_ci	 * On this soc, the glue gets the MCLK directly from the clock
748c2ecf20Sopenharmony_ci	 * controller instead of going the through the TDM interface.
758c2ecf20Sopenharmony_ci	 *
768c2ecf20Sopenharmony_ci	 * Here we assume interface A uses clock A, etc ... While it is
778c2ecf20Sopenharmony_ci	 * true for now, it could be different. Instead the glue should
788c2ecf20Sopenharmony_ci	 * find out the clock used by the interface and select the same
798c2ecf20Sopenharmony_ci	 * source. For that, we will need regmap backed clock mux which
808c2ecf20Sopenharmony_ci	 * is a work in progress
818c2ecf20Sopenharmony_ci	 */
828c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, e->reg,
838c2ecf20Sopenharmony_ci				      CTRL0_MCLK_SEL,
848c2ecf20Sopenharmony_ci				      FIELD_PREP(CTRL0_MCLK_SEL, mux));
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return 1;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(g12a_toacodec_mux_enum, TOACODEC_CTRL0,
928c2ecf20Sopenharmony_ci			    CTRL0_DAT_SEL_SHIFT,
938c2ecf20Sopenharmony_ci			    g12a_toacodec_mux_texts);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new g12a_toacodec_mux =
968c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM_EXT("Source", g12a_toacodec_mux_enum,
978c2ecf20Sopenharmony_ci			  snd_soc_dapm_get_enum_double,
988c2ecf20Sopenharmony_ci			  g12a_toacodec_mux_put_enum);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new g12a_toacodec_out_enable =
1018c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOACODEC_CTRL0,
1028c2ecf20Sopenharmony_ci				    CTRL0_ENABLE_SHIFT, 1, 0);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget g12a_toacodec_widgets[] = {
1058c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("SRC", SND_SOC_NOPM, 0, 0,
1068c2ecf20Sopenharmony_ci			 &g12a_toacodec_mux),
1078c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SWITCH("OUT EN", SND_SOC_NOPM, 0, 0,
1088c2ecf20Sopenharmony_ci			    &g12a_toacodec_out_enable),
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int g12a_toacodec_input_hw_params(struct snd_pcm_substream *substream,
1128c2ecf20Sopenharmony_ci					 struct snd_pcm_hw_params *params,
1138c2ecf20Sopenharmony_ci					 struct snd_soc_dai *dai)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct meson_codec_glue_input *data;
1168c2ecf20Sopenharmony_ci	int ret;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	ret = meson_codec_glue_input_hw_params(substream, params, dai);
1198c2ecf20Sopenharmony_ci	if (ret)
1208c2ecf20Sopenharmony_ci		return ret;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* The glue will provide 1 lane out of the 4 to the output */
1238c2ecf20Sopenharmony_ci	data = meson_codec_glue_input_get_data(dai);
1248c2ecf20Sopenharmony_ci	data->params.channels_min = min_t(unsigned int, TOACODEC_OUT_CHMAX,
1258c2ecf20Sopenharmony_ci					data->params.channels_min);
1268c2ecf20Sopenharmony_ci	data->params.channels_max = min_t(unsigned int, TOACODEC_OUT_CHMAX,
1278c2ecf20Sopenharmony_ci					data->params.channels_max);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return 0;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops g12a_toacodec_input_ops = {
1338c2ecf20Sopenharmony_ci	.hw_params	= g12a_toacodec_input_hw_params,
1348c2ecf20Sopenharmony_ci	.set_fmt	= meson_codec_glue_input_set_fmt,
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops g12a_toacodec_output_ops = {
1388c2ecf20Sopenharmony_ci	.startup	= meson_codec_glue_output_startup,
1398c2ecf20Sopenharmony_ci};
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define TOACODEC_STREAM(xname, xsuffix, xchmax)			\
1428c2ecf20Sopenharmony_ci{								\
1438c2ecf20Sopenharmony_ci	.stream_name	= xname " " xsuffix,			\
1448c2ecf20Sopenharmony_ci	.channels_min	= 1,					\
1458c2ecf20Sopenharmony_ci	.channels_max	= (xchmax),				\
1468c2ecf20Sopenharmony_ci	.rate_min       = 5512,					\
1478c2ecf20Sopenharmony_ci	.rate_max	= 192000,				\
1488c2ecf20Sopenharmony_ci	.formats	= AXG_TDM_FORMATS,			\
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci#define TOACODEC_INPUT(xname, xid) {					\
1528c2ecf20Sopenharmony_ci	.name = xname,							\
1538c2ecf20Sopenharmony_ci	.id = (xid),							\
1548c2ecf20Sopenharmony_ci	.playback = TOACODEC_STREAM(xname, "Playback", 8),		\
1558c2ecf20Sopenharmony_ci	.ops = &g12a_toacodec_input_ops,				\
1568c2ecf20Sopenharmony_ci	.probe = meson_codec_glue_input_dai_probe,			\
1578c2ecf20Sopenharmony_ci	.remove = meson_codec_glue_input_dai_remove,			\
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define TOACODEC_OUTPUT(xname, xid) {					\
1618c2ecf20Sopenharmony_ci	.name = xname,							\
1628c2ecf20Sopenharmony_ci	.id = (xid),							\
1638c2ecf20Sopenharmony_ci	.capture = TOACODEC_STREAM(xname, "Capture", TOACODEC_OUT_CHMAX), \
1648c2ecf20Sopenharmony_ci	.ops = &g12a_toacodec_output_ops,				\
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver g12a_toacodec_dai_drv[] = {
1688c2ecf20Sopenharmony_ci	TOACODEC_INPUT("IN A", TOACODEC_IN_A),
1698c2ecf20Sopenharmony_ci	TOACODEC_INPUT("IN B", TOACODEC_IN_B),
1708c2ecf20Sopenharmony_ci	TOACODEC_INPUT("IN C", TOACODEC_IN_C),
1718c2ecf20Sopenharmony_ci	TOACODEC_OUTPUT("OUT", TOACODEC_OUT),
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic int g12a_toacodec_component_probe(struct snd_soc_component *c)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	/* Initialize the static clock parameters */
1778c2ecf20Sopenharmony_ci	return snd_soc_component_write(c, TOACODEC_CTRL0,
1788c2ecf20Sopenharmony_ci				       CTRL0_BLK_CAP_INV);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route g12a_toacodec_routes[] = {
1828c2ecf20Sopenharmony_ci	{ "SRC", "I2S A", "IN A Playback" },
1838c2ecf20Sopenharmony_ci	{ "SRC", "I2S B", "IN B Playback" },
1848c2ecf20Sopenharmony_ci	{ "SRC", "I2S C", "IN C Playback" },
1858c2ecf20Sopenharmony_ci	{ "OUT EN", "Switch", "SRC" },
1868c2ecf20Sopenharmony_ci	{ "OUT Capture", NULL, "OUT EN" },
1878c2ecf20Sopenharmony_ci};
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new g12a_toacodec_controls[] = {
1908c2ecf20Sopenharmony_ci	SOC_SINGLE("Lane Select", TOACODEC_CTRL0, CTRL0_LANE_SEL, 3, 0),
1918c2ecf20Sopenharmony_ci};
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver g12a_toacodec_component_drv = {
1948c2ecf20Sopenharmony_ci	.probe			= g12a_toacodec_component_probe,
1958c2ecf20Sopenharmony_ci	.controls		= g12a_toacodec_controls,
1968c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(g12a_toacodec_controls),
1978c2ecf20Sopenharmony_ci	.dapm_widgets		= g12a_toacodec_widgets,
1988c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(g12a_toacodec_widgets),
1998c2ecf20Sopenharmony_ci	.dapm_routes		= g12a_toacodec_routes,
2008c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(g12a_toacodec_routes),
2018c2ecf20Sopenharmony_ci	.endianness		= 1,
2028c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic const struct regmap_config g12a_toacodec_regmap_cfg = {
2068c2ecf20Sopenharmony_ci	.reg_bits	= 32,
2078c2ecf20Sopenharmony_ci	.val_bits	= 32,
2088c2ecf20Sopenharmony_ci	.reg_stride	= 4,
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic const struct of_device_id g12a_toacodec_of_match[] = {
2128c2ecf20Sopenharmony_ci	{ .compatible = "amlogic,g12a-toacodec", },
2138c2ecf20Sopenharmony_ci	{}
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, g12a_toacodec_of_match);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic int g12a_toacodec_probe(struct platform_device *pdev)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2208c2ecf20Sopenharmony_ci	void __iomem *regs;
2218c2ecf20Sopenharmony_ci	struct regmap *map;
2228c2ecf20Sopenharmony_ci	int ret;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	ret = device_reset(dev);
2258c2ecf20Sopenharmony_ci	if (ret)
2268c2ecf20Sopenharmony_ci		return ret;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	regs = devm_platform_ioremap_resource(pdev, 0);
2298c2ecf20Sopenharmony_ci	if (IS_ERR(regs))
2308c2ecf20Sopenharmony_ci		return PTR_ERR(regs);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	map = devm_regmap_init_mmio(dev, regs, &g12a_toacodec_regmap_cfg);
2338c2ecf20Sopenharmony_ci	if (IS_ERR(map)) {
2348c2ecf20Sopenharmony_ci		dev_err(dev, "failed to init regmap: %ld\n",
2358c2ecf20Sopenharmony_ci			PTR_ERR(map));
2368c2ecf20Sopenharmony_ci		return PTR_ERR(map);
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return devm_snd_soc_register_component(dev,
2408c2ecf20Sopenharmony_ci			&g12a_toacodec_component_drv, g12a_toacodec_dai_drv,
2418c2ecf20Sopenharmony_ci			ARRAY_SIZE(g12a_toacodec_dai_drv));
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic struct platform_driver g12a_toacodec_pdrv = {
2458c2ecf20Sopenharmony_ci	.driver = {
2468c2ecf20Sopenharmony_ci		.name = G12A_TOACODEC_DRV_NAME,
2478c2ecf20Sopenharmony_ci		.of_match_table = g12a_toacodec_of_match,
2488c2ecf20Sopenharmony_ci	},
2498c2ecf20Sopenharmony_ci	.probe = g12a_toacodec_probe,
2508c2ecf20Sopenharmony_ci};
2518c2ecf20Sopenharmony_cimodule_platform_driver(g12a_toacodec_pdrv);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
2548c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Amlogic G12a To Internal DAC Codec Driver");
2558c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
256