// SPDX-License-Identifier: GPL-2.0
//
// Audio driver for AK5558 ADC
//
// Copyright (C) 2015 Asahi Kasei Microdevices Corporation
// Copyright 2018 NXP

#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>

#include <sound/initval.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/tlv.h>

#include "ak5558.h"

enum ak555x_type {
	AK5558,
	AK5552,
};

#define AK5558_NUM_SUPPLIES 2
static const char *ak5558_supply_names[AK5558_NUM_SUPPLIES] = {
	"DVDD",
	"AVDD",
};

/* AK5558 Codec Private Data */
struct ak5558_priv {
	struct regulator_bulk_data supplies[AK5558_NUM_SUPPLIES];
	struct snd_soc_component component;
	struct regmap *regmap;
	struct i2c_client *i2c;
	struct gpio_desc *reset_gpiod; /* Reset & Power down GPIO */
	int slots;
	int slot_width;
};

/* ak5558 register cache & default register settings */
static const struct reg_default ak5558_reg[] = {
	{ 0x0, 0xFF },	/*	0x00	AK5558_00_POWER_MANAGEMENT1	*/
	{ 0x1, 0x01 },	/*	0x01	AK5558_01_POWER_MANAGEMENT2	*/
	{ 0x2, 0x01 },	/*	0x02	AK5558_02_CONTROL1		*/
	{ 0x3, 0x00 },	/*	0x03	AK5558_03_CONTROL2		*/
	{ 0x4, 0x00 },	/*	0x04	AK5558_04_CONTROL3		*/
	{ 0x5, 0x00 }	/*	0x05	AK5558_05_DSD			*/
};

static const char * const mono_texts[] = {
	"8 Slot", "2 Slot", "4 Slot", "1 Slot",
};

static const struct soc_enum ak5558_mono_enum[] = {
	SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
			ARRAY_SIZE(mono_texts), mono_texts),
};

static const char * const mono_5552_texts[] = {
	"2 Slot", "1 Slot (Fixed)", "2 Slot", "1 Slot (Optimal)",
};

static const struct soc_enum ak5552_mono_enum[] = {
	SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
			ARRAY_SIZE(mono_5552_texts), mono_5552_texts),
};

static const char * const digfil_texts[] = {
	"Sharp Roll-Off", "Slow Roll-Off",
	"Short Delay Sharp Roll-Off", "Short Delay Slow Roll-Off",
};

static const struct soc_enum ak5558_adcset_enum[] = {
	SOC_ENUM_SINGLE(AK5558_04_CONTROL3, 0,
			ARRAY_SIZE(digfil_texts), digfil_texts),
};

static const struct snd_kcontrol_new ak5558_snd_controls[] = {
	SOC_ENUM("Monaural Mode", ak5558_mono_enum[0]),
	SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]),
};

static const struct snd_kcontrol_new ak5552_snd_controls[] = {
	SOC_ENUM("Monaural Mode", ak5552_mono_enum[0]),
	SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]),
};

static const struct snd_soc_dapm_widget ak5558_dapm_widgets[] = {
	/* Analog Input */
	SND_SOC_DAPM_INPUT("AIN1"),
	SND_SOC_DAPM_INPUT("AIN2"),
	SND_SOC_DAPM_INPUT("AIN3"),
	SND_SOC_DAPM_INPUT("AIN4"),
	SND_SOC_DAPM_INPUT("AIN5"),
	SND_SOC_DAPM_INPUT("AIN6"),
	SND_SOC_DAPM_INPUT("AIN7"),
	SND_SOC_DAPM_INPUT("AIN8"),

	SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
	SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
	SND_SOC_DAPM_ADC("ADC Ch3", NULL, AK5558_00_POWER_MANAGEMENT1, 2, 0),
	SND_SOC_DAPM_ADC("ADC Ch4", NULL, AK5558_00_POWER_MANAGEMENT1, 3, 0),
	SND_SOC_DAPM_ADC("ADC Ch5", NULL, AK5558_00_POWER_MANAGEMENT1, 4, 0),
	SND_SOC_DAPM_ADC("ADC Ch6", NULL, AK5558_00_POWER_MANAGEMENT1, 5, 0),
	SND_SOC_DAPM_ADC("ADC Ch7", NULL, AK5558_00_POWER_MANAGEMENT1, 6, 0),
	SND_SOC_DAPM_ADC("ADC Ch8", NULL, AK5558_00_POWER_MANAGEMENT1, 7, 0),

	SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
};

static const struct snd_soc_dapm_widget ak5552_dapm_widgets[] = {
	/* Analog Input */
	SND_SOC_DAPM_INPUT("AIN1"),
	SND_SOC_DAPM_INPUT("AIN2"),

	SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
	SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),

	SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
};

static const struct snd_soc_dapm_route ak5558_intercon[] = {
	{"ADC Ch1", NULL, "AIN1"},
	{"SDTO", NULL, "ADC Ch1"},

	{"ADC Ch2", NULL, "AIN2"},
	{"SDTO", NULL, "ADC Ch2"},

	{"ADC Ch3", NULL, "AIN3"},
	{"SDTO", NULL, "ADC Ch3"},

	{"ADC Ch4", NULL, "AIN4"},
	{"SDTO", NULL, "ADC Ch4"},

	{"ADC Ch5", NULL, "AIN5"},
	{"SDTO", NULL, "ADC Ch5"},

	{"ADC Ch6", NULL, "AIN6"},
	{"SDTO", NULL, "ADC Ch6"},

	{"ADC Ch7", NULL, "AIN7"},
	{"SDTO", NULL, "ADC Ch7"},

	{"ADC Ch8", NULL, "AIN8"},
	{"SDTO", NULL, "ADC Ch8"},
};

static const struct snd_soc_dapm_route ak5552_intercon[] = {
	{"ADC Ch1", NULL, "AIN1"},
	{"SDTO", NULL, "ADC Ch1"},

	{"ADC Ch2", NULL, "AIN2"},
	{"SDTO", NULL, "ADC Ch2"},
};

static int ak5558_set_mcki(struct snd_soc_component *component)
{
	return snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_CKS,
				   AK5558_CKS_AUTO);
}

static int ak5558_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
{
	struct snd_soc_component *component = dai->component;
	struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
	u8 bits;
	int pcm_width = max(params_physical_width(params), ak5558->slot_width);

	switch (pcm_width) {
	case 16:
		bits = AK5558_DIF_24BIT_MODE;
		break;
	case 32:
		bits = AK5558_DIF_32BIT_MODE;
		break;
	default:
		return -EINVAL;
	}

	snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits);

	return 0;
}

static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	struct snd_soc_component *component = dai->component;
	u8 format;

	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
	case SND_SOC_DAIFMT_CBC_CFC:
		break;
	case SND_SOC_DAIFMT_CBP_CFP:
		break;
	case SND_SOC_DAIFMT_CBC_CFP:
	case SND_SOC_DAIFMT_CBP_CFC:
	default:
		dev_err(dai->dev, "Clock mode unsupported");
		return -EINVAL;
	}

	/* set master/slave audio interface */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		format = AK5558_DIF_I2S_MODE;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		format = AK5558_DIF_MSB_MODE;
		break;
	case SND_SOC_DAIFMT_DSP_B:
		format = AK5558_DIF_MSB_MODE;
		break;
	default:
		return -EINVAL;
	}

	snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format);

	return 0;
}

static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
			       unsigned int rx_mask, int slots,
			       int slot_width)
{
	struct snd_soc_component *component = dai->component;
	struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
	int tdm_mode;

	ak5558->slots = slots;
	ak5558->slot_width = slot_width;

	switch (slots * slot_width) {
	case 128:
		tdm_mode = AK5558_MODE_TDM128;
		break;
	case 256:
		tdm_mode = AK5558_MODE_TDM256;
		break;
	case 512:
		tdm_mode = AK5558_MODE_TDM512;
		break;
	default:
		tdm_mode = AK5558_MODE_NORMAL;
		break;
	}

	snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS,
			    tdm_mode);
	return 0;
}

#define AK5558_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE |\
			 SNDRV_PCM_FMTBIT_S24_LE |\
			 SNDRV_PCM_FMTBIT_S32_LE)

static const unsigned int ak5558_rates[] = {
	8000, 11025,  16000, 22050,
	32000, 44100, 48000, 88200,
	96000, 176400, 192000, 352800,
	384000, 705600, 768000, 1411200,
	2822400,
};

static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = {
	.count = ARRAY_SIZE(ak5558_rates),
	.list = ak5558_rates,
};

static int ak5558_startup(struct snd_pcm_substream *substream,
			  struct snd_soc_dai *dai)
{
	return snd_pcm_hw_constraint_list(substream->runtime, 0,
					  SNDRV_PCM_HW_PARAM_RATE,
					  &ak5558_rate_constraints);
}

static const struct snd_soc_dai_ops ak5558_dai_ops = {
	.startup        = ak5558_startup,
	.hw_params	= ak5558_hw_params,

	.set_fmt	= ak5558_set_dai_fmt,
	.set_tdm_slot   = ak5558_set_tdm_slot,
};

static struct snd_soc_dai_driver ak5558_dai = {
	.name = "ak5558-aif",
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 8,
		.rates = SNDRV_PCM_RATE_KNOT,
		.formats = AK5558_FORMATS,
	},
	.ops = &ak5558_dai_ops,
};

static struct snd_soc_dai_driver ak5552_dai = {
	.name = "ak5552-aif",
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = SNDRV_PCM_RATE_KNOT,
		.formats = AK5558_FORMATS,
	},
	.ops = &ak5558_dai_ops,
};

static void ak5558_reset(struct ak5558_priv *ak5558, bool active)
{
	if (!ak5558->reset_gpiod)
		return;

	gpiod_set_value_cansleep(ak5558->reset_gpiod, active);
	usleep_range(1000, 2000);
}

static int ak5558_probe(struct snd_soc_component *component)
{
	struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);

	ak5558_reset(ak5558, false);
	return ak5558_set_mcki(component);
}

static void ak5558_remove(struct snd_soc_component *component)
{
	struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);

	ak5558_reset(ak5558, true);
}

static int __maybe_unused ak5558_runtime_suspend(struct device *dev)
{
	struct ak5558_priv *ak5558 = dev_get_drvdata(dev);

	regcache_cache_only(ak5558->regmap, true);
	ak5558_reset(ak5558, true);

	regulator_bulk_disable(ARRAY_SIZE(ak5558->supplies),
			       ak5558->supplies);
	return 0;
}

static int __maybe_unused ak5558_runtime_resume(struct device *dev)
{
	struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
	int ret;

	ret = regulator_bulk_enable(ARRAY_SIZE(ak5558->supplies),
				    ak5558->supplies);
	if (ret != 0) {
		dev_err(dev, "Failed to enable supplies: %d\n", ret);
		return ret;
	}

	ak5558_reset(ak5558, true);
	ak5558_reset(ak5558, false);

	regcache_cache_only(ak5558->regmap, false);
	regcache_mark_dirty(ak5558->regmap);

	return regcache_sync(ak5558->regmap);
}

static const struct dev_pm_ops ak5558_pm = {
	SET_RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL)
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				pm_runtime_force_resume)
};

static const struct snd_soc_component_driver soc_codec_dev_ak5558 = {
	.probe			= ak5558_probe,
	.remove			= ak5558_remove,
	.controls		= ak5558_snd_controls,
	.num_controls		= ARRAY_SIZE(ak5558_snd_controls),
	.dapm_widgets		= ak5558_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(ak5558_dapm_widgets),
	.dapm_routes		= ak5558_intercon,
	.num_dapm_routes	= ARRAY_SIZE(ak5558_intercon),
	.idle_bias_on		= 1,
	.use_pmdown_time	= 1,
	.endianness		= 1,
};

static const struct snd_soc_component_driver soc_codec_dev_ak5552 = {
	.probe			= ak5558_probe,
	.remove			= ak5558_remove,
	.controls		= ak5552_snd_controls,
	.num_controls		= ARRAY_SIZE(ak5552_snd_controls),
	.dapm_widgets		= ak5552_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(ak5552_dapm_widgets),
	.dapm_routes		= ak5552_intercon,
	.num_dapm_routes	= ARRAY_SIZE(ak5552_intercon),
	.idle_bias_on		= 1,
	.use_pmdown_time	= 1,
	.endianness		= 1,
};

static const struct regmap_config ak5558_regmap = {
	.reg_bits = 8,
	.val_bits = 8,

	.max_register = AK5558_05_DSD,
	.reg_defaults = ak5558_reg,
	.num_reg_defaults = ARRAY_SIZE(ak5558_reg),
	.cache_type = REGCACHE_RBTREE,
};

static int ak5558_i2c_probe(struct i2c_client *i2c)
{
	struct ak5558_priv *ak5558;
	int ret = 0;
	int dev_id;
	int i;

	ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL);
	if (!ak5558)
		return -ENOMEM;

	ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap);
	if (IS_ERR(ak5558->regmap))
		return PTR_ERR(ak5558->regmap);

	i2c_set_clientdata(i2c, ak5558);
	ak5558->i2c = i2c;

	ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset",
						      GPIOD_OUT_LOW);
	if (IS_ERR(ak5558->reset_gpiod))
		return PTR_ERR(ak5558->reset_gpiod);

	for (i = 0; i < ARRAY_SIZE(ak5558->supplies); i++)
		ak5558->supplies[i].supply = ak5558_supply_names[i];

	ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(ak5558->supplies),
				      ak5558->supplies);
	if (ret != 0) {
		dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
		return ret;
	}

	dev_id = (uintptr_t)of_device_get_match_data(&i2c->dev);
	switch (dev_id) {
	case AK5552:
		ret = devm_snd_soc_register_component(&i2c->dev,
						      &soc_codec_dev_ak5552,
						      &ak5552_dai, 1);
		break;
	case AK5558:
		ret = devm_snd_soc_register_component(&i2c->dev,
						      &soc_codec_dev_ak5558,
						      &ak5558_dai, 1);
		break;
	default:
		dev_err(&i2c->dev, "unexpected device type\n");
		return -EINVAL;
	}
	if (ret < 0) {
		dev_err(&i2c->dev, "failed to register component: %d\n", ret);
		return ret;
	}

	pm_runtime_enable(&i2c->dev);
	regcache_cache_only(ak5558->regmap, true);

	return 0;
}

static void ak5558_i2c_remove(struct i2c_client *i2c)
{
	pm_runtime_disable(&i2c->dev);
}

static const struct of_device_id ak5558_i2c_dt_ids[] __maybe_unused = {
	{ .compatible = "asahi-kasei,ak5558", .data = (void *) AK5558 },
	{ .compatible = "asahi-kasei,ak5552", .data = (void *) AK5552 },
	{ }
};
MODULE_DEVICE_TABLE(of, ak5558_i2c_dt_ids);

static struct i2c_driver ak5558_i2c_driver = {
	.driver = {
		.name = "ak5558",
		.of_match_table = of_match_ptr(ak5558_i2c_dt_ids),
		.pm = &ak5558_pm,
	},
	.probe = ak5558_i2c_probe,
	.remove = ak5558_i2c_remove,
};

module_i2c_driver(ak5558_i2c_driver);

MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
MODULE_LICENSE("GPL v2");
